#!/usr/bin/perl -I/usr/lib/cmu
#
# $Id: cmuImport,v 1.25.4.10 2000/10/09 16:45:21 jeffb Exp $
# Written by: Jeff Bilicki
#
# Parses opts and get type of import via calling program .
# Then call and handles the core importing 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_d $opt_h $opt_l $opt_n $opt_v );
getopts('cd:l:n:vh');

printUsage() if ($opt_h || !$opt_d);

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

#################
# Check the build 
my $buildOS = CMU::Config::getBuild();
unless(defined($validImport{$buildOS})) {
	die "Not a valid build to import on $buildOS\n";
}

###################################################
# First thing create the global cObj to read in the 
# global config dirtectives
$cObj = new CMU::Migrate(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 $dir = CMU::Config::parseD($opt_d);	
if(-d $dir) {
	$${ $cObj->{_glbConf} }{'destDir'} = $dir;
} elsif (-d $cObj->cfg("destDir")."/".$dir) {
	$${ $cObj->{_glbConf} }{'destDir'} = $cObj->cfg("destDir")."/".$dir;
} else {
	die "Cannot find directory $opt_d\n";
}
$cObj->logIt("INFO: importDir is ".$cObj->cfg("destDir"));
$${ $cObj->{_glbConf} }{'confOnly'} = 1 if ($opt_c);

$opt_c ?
	($${ $cObj->{_glbConf} }{'confOnly'} = "on") :
	($${ $cObj->{_glbConf} }{'confOnly'} = "off");

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

# Build the objects form the XML file
$fileName = $cObj->cfg("destDir")."/".$cObj->cfg("cmuXML");
$cObj->logIt("INFO: using archive file: $fileName");
$cObj->imptState($fileName);

if($cObj->{buildOS} eq 'RaQ1') {
	$${ $cObj->{_glbConf} }{'confOnly'} = "on";
}


# export the current state
my $actobj = CMU::Migrate->new(name => "export");
$actobj->curState;

# merge the current state
my $imptobj = $cObj->merge($actobj);

# Free up some memory
$actobj = '';
$cObj =  '';


if ($vsiteName) {
	importVsite($vsiteName);
} else {	
	importAll();
}

importCleanUp();

exit 0;

sub importAll 
# imports all vsite, users, mailing lists
# Side Effects: creates out file in tmpdir filename
{
	$imptobj->verify;
	$imptobj->add;
	if ($imptobj->cfg("confOnly")  =~ /(f|false|no|off|0)/oi ) {
		 $imptobj->restoreArch();
	}
	#CMU::XML::writeXML($imptobj,"/tmp/testout.xml");
}

sub importVsite
# imports only one vsite
# site Effects: adds the vsite to the system
{
	my $siteName = shift;

	my $state = "import";
	my @rens = @{ $$imptobj{children} };
	for my $child (@rens) {
		next if($child->{name} eq $siteName);
		if($child->{_state} eq $state) {
			$imptobj->destChild($child->{name},$state);
		}
	}
	$imptobj->verify;
	$imptobj->add;
	if ($imptobj->cfg("confOnly")  =~ /(f|false|no|off|0)/oi ) {
		 $imptobj->restoreArch();
	}
	#CMU::XML::writeXML($imptobj,"/tmp/testout.xml");
}

sub importCleanUp
# resets Fpx and hups the servers
{
	$imptobj->resetFpx;
	$imptobj->hup;
}

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

	exit 1;
}
