#!/usr/bin/perl -I/usr/lib/cmu
#
# $Id: cmuExport,v 1.27.4.12 2000/09/25 21:48:54 jeffb Exp $
# Written By: Jeff BIlicki
#
# Parses opts and get type of export via calling program .
# Then call and handles the core exporting functions.
#

# If you are not toor, go away :)
die "You must run this script as root\n" if ($< != 0);

###############
# local vars
$fqdn = `uname -n`;
chomp($fqdn);

################
# Parse the args
use Getopt::Std;
use vars qw ( $opt_c $opt_h $opt_d $opt_l $opt_n $opt_v );
getopts('chd:l:n:v');

printUsage() if ($opt_h);

##################
# require the libs
use CMU::Migrate;
use CMU::XML;
use CMU::Config;
require CMU::Global;
import CMU::Global qw( %validExport );

#################
# Check the build 
my $buildOS = CMU::Config::getBuild();
unless(defined($validExport{$buildOS})) {
    print "Not a valid build to export on\n";
    exit 1;
}


###################################################
# First thing create the global cObj to read in the 
# global config dirtectives
$cObj = CMU::Migrate->new(name => $buildOS);

if($opt_l) {
	$${ $cObj->{_glbConf} }{'logFile'} = $opt_l;
}
warn "LogFile location is ", $${ $cObj->{_glbConf} }{'logFile'}, "\n";

# check if $self->logIt messages are printed to stdout
$opt_v ?
	($${ $cObj->{_glbConf} }{'stdDebug'} = 1) :
	($${ $cObj->{_glbConf} }{'stdDebug'} = 0); 

# Put some basic info to the log file
$cObj->logIt("\nINFO: $0 running on $fqdn");
$cObj->logIt("INFO: uid $< called $0");

my $vsiteName;
if($opt_n) {
	$vsiteName = $opt_n;
}

my $buildDir;
if($opt_d) {
	$buildDir = CMU::Config::parseD($opt_d);
} elsif ($vsiteName) {
	$buildDir = CMU::Config::parseD($cObj->cfg("destDir")."/".$vsiteName);
} else {
	$buildDir = CMU::Config::parseD($cObj->cfg("destDir")."/".$fqdn);
}
$${ $cObj->{_glbConf} }{'destDir'} = $buildDir;
$cObj->logIt("INFO: build directory is $buildDir");
$opt_c ? 
	($${ $cObj->{_glbConf} }{'confOnly'} = "on") :
	($${ $cObj->{_glbConf} }{'confOnly'} = "off");	

$opt_v ?
	($${ $cObj->{_glbConf} }{'stdDebug'} = 1) :
	($${ $cObj->{_glbConf} }{'stdDebug'} = 0); 
	

################
# run prebuild
&preBuild;

$fileName = $cObj->cfg("destDir")."/".$cObj->cfg("cmuXML");

if ($vsiteName) {
	exportVsite($vsiteName);
} else {
	exportAll();
}
&postBuild;
	
sub exportAll 
# exports all vsite, users, mailing lists
# Side Effects: creates out file in tmpdir filename
{
	$cObj->curState();

	if ($cObj->cfg("confOnly") =~ /(f|false|no|off|0)/oi ) {
		$cObj->createArch();
	}
	$cObj->logIt("INFO: xml file name is $fileName");
	CMU::XML::writeXML($cObj,$fileName);
}

sub exportVsite 
# exports listed vsites and their users and, mailing lists
# Side Effects: creates out file in tmpdir filename
{
	require CMU::Migrate::vsite;

	my $siteName = shift;	

	my $vobj = CMU::Migrate::vsite->new(name => $siteName);
	$vobj->populate;

	$cObj->addChild($vobj);

	if ($cObj->cfg("confOnly") =~ /(f|false|no|off|0)/oi ) {
		$cObj->createArch();
	}
	$cObj->logIt("INFO: xml file name is $fileName");
	CMU::XML::writeXML($cObj,$fileName);
}


sub preBuild
# removes and/or creates the dest directory
{
	my $destDir = $cObj->cfg("destDir");
	die "No Destination directory: $destDir\n" unless ($destDir);
	if (-d $destDir) {
		system("rm -rf $destDir");
	}
	system("mkdir -p $destDir");
	chmod(0700, $destDir);
	return;
}

sub postBuild
{
	my $destDir = $cObj->cfg("destDir");
	$cObj->logIT("No Destination directory: $destDir") unless ($destDir);
	
	system("chmod 600 $destDir/*");
	return;

}
sub printUsage
# Print the help message
# Side Effect: exists the program in an error state
{
print <<EOF;
usage:   cmuExport [OPTIONS] 
         -c export configuration only
         -d build directory, this is where export will place all exported files, the default is /home/cmu/FQDN
         -l [fileName] log all messages to file (default: /home/cmu/cmuLog)
         -n [fqdn] Fully qualified domain name of the vsite to export
         -v verbose, print all messages to stdout
         -h help, this help text
EOF

	exit 1;
}
