#!/usr/bin/env perl
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
#####################################################
#
#   Generic xCAT post script for diskless nodes
#
#####################################################

use File::Path;

# since we don't have syslog set up yet we'll
# just save msgs in a local log file
$logdir = "/var/log/xcat";

if (!-d $logdir) {
	mkpath($logdir);
}

$::sdate = `/bin/date`;
chomp $sdate;
my $logfile = $logdir . "/xcat.log";

# this log should not contain much so it might be ok to let it grow?
# at least we'll have the errors preserved
open(LOGFILE,">>",$logfile);
$::LOG_FILE = \*LOGFILE;

# get hostname
$::shorthost = `hostname -s`;
chomp $::shorthost;

# get platform
my $osname = `uname`;
chomp $osname;

# get the postscript tar file from the server and unwrap it
if ($osname eq "Linux") {
	if (&getLinuxpostfile() != 0 ) {
		close($::LOG_FILE_HANDLE);
        	exit 1;
    	}
} else {
	if (&getAIXpostfile() != 0 ) {
		close($::LOG_FILE_HANDLE);
		exit 1;
	}
}

# check & run the postscript
my $scriptname = "/xcatpost/".$shorthost;
if ($osname eq "Linux") {
   $scriptname = "/tmp/mypostscript";
}
if (-f $scriptname)
{
	my $rc = system("$scriptname");
    if ($rc >> 8)
    {
		print $::LOG_FILE "$::sdate  xcatdsklspost: Could not run $scriptname.\n";
    }
} else {
	print $::LOG_FILE "$::sdate  xcatdsklspost: Could not find post script for $::shorthost.\n";
}

close($::LOG_FILE_HANDLE);

exit 0;

#####################################################
#
#   getLinuxpostfile
#		Get the linux post scripts from the server
#
#####################################################
sub  getLinuxpostfile {

	# look in the dhcp leases file for the name of the server
	my $dhclientfile="/var/lib/dhclient/dhclient-eth0.leases";

	my @lines;
	if (open(DHCLIENT, "<$dhclientfile")) {
    	@lines = <DHCLIENT>;
    	close(DHCLIENT);
	} else {
		print $::LOG_FILE "$::sdate  xcatdsklspost: Could not open $dhclientfile.\n";
    	return 1;
	}

	my $ip;  # IP of server
	foreach my $l (@lines)
	{
    	if ($l =~ /dhcp-server-identifier/) {
        	my ($junk, $junk2, $s) = split(" ", $l);
        	($ip, $junk) =  split("\;", $s);
    	}
	}

	# get postscripts tar file from the server
   my $stunconf;
   mkpath "/etc/stunnel";
   open($stunconf,">","/etc/stunnel/stunnel.conf");
   print $stunconf "client=yes\n";
   print $stunconf "foreground=no\n";
   print $stunconf "output=/dev/null\n";
   print $stunconf "verify=0\n";
   print $stunconf "[xcatd]\n";
   print $stunconf "accept=400\n";
   print $stunconf "connect=$ip:3001\n";
   close($stunconf);
	my $getcmd = "stunnel; sleep 1; mkdir -p /xcatpost; cd /xcatpost; wget -l inf -N -r --waitretry=10 --random-wait --retry-connrefused -t 0 -T 60 ftp://$ip/install/postscripts; mv $ip/install/postscripts/* .; chmod +x /xcatpost/*; /xcatpost/getpostscript.awk | sed  -e 's/<[^>]*>//g'|egrep -v '^ *$'|sed -e 's/^ *//' >  /tmp/mypostscript; chmod +x /tmp/mypostscript";

	if (&runcmd($getcmd) != 0)
   {
		print $::LOG_FILE "$::sdate  xcatdsklspost: Could not get xcatpost.tar.gz.\n";
		return 1;
	}
	return 0;
}

#####################################################
#
#   getAIXpostfile
#               Get the AIX post scripts from the server
#
#####################################################
sub  getAIXpostfile {

	# get the name of my service node/NIM master from the /etc/niminfo file
	if (-f "/etc/niminfo") {
		my $cmd = "cat /etc/niminfo | grep 'NIM_NAME'";
		&runcmd($cmd);
		my $hnline = $::outref;
		my $junk;
		($junk, $::shorthost) = split(/=/, $hnline);
		$::shorthost =~ s/^\s*//;    # delete leading white space
		chomp $::shorthost;

		$cmd = "cat /etc/niminfo | grep 'NIM_MASTER_HOSTNAME'";
		&runcmd($cmd);
		my $SNline = $::outref;
		($junk, $sn) =  split(/=/, $SNline);
		$sn =~ s/^\s*//;    # delete leading white space
		chomp $sn;

	} else {
		print $::LOG_FILE "$::sdate  xcatdsklspost: Could not find /etc/niminfo file.\n";
		return 1;	
	}

	# use tftp to get the tar file
	my $getcmd="mkdir -p /xcatpost; cd /xcatpost; tftp -o xcatpost.tar.gz $sn /install/autoinst/xcatpost.tar.gz";
	if (&runcmd($getcmd) != 0)
	{
		print $::LOG_FILE "$::sdate  xcatdsklspost: Could not get xcatpost.tar.gz.\n";
		return 1;
	}

	# unwrap the tar file
	my $uncmd = "cd /xcatpost; gunzip xcatpost.tar.gz; tar -xvf xcatpost.tar";
	if (&runcmd($uncmd) != 0)
	{
		print $::LOG_FILE "$::sdate  xcatdsklspost: Could not extract from /xcatpost/xcatpost.tar.gz.\n";
		return 1;
	}
	return 0;
}

#
# run the command
#
sub runcmd
{
    my ($cmd) = @_;
	my $rc=0;
    $cmd .= ' 2>&1' ;
    $::outref = [];
    $::outref = `$cmd`;
    if ($?)
    {
        $rc = $? >> 8;
        if ($rc > 0)
        {
			print $::LOG_FILE "$::sdate  xcatdsklspost: $::outref\n";
        }
    }
 return $rc;
}