#!/usr/bin/perl
# @(#)22   1.2   src/csm/install/csmfirstbootstrap.perl, setup, csm_rgar2h, rgar2hs001a 7/13/07 03:06:01

#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------

=head1	csm_first_boot_strap

	Description: This script will be executed after the first boot ok. It will be invoked from
	/etc/inittab. This scirpt will mount on the csm server and execute the osfirstboot script 
	on the csm server.

	Exit codes:
		0 - success
		1 - error
=cut

#-------------------------------------------------------------------------------

use strict;



{    # main

    # initialize the global data
    &initialize;

    # start logging
    &start_log;

    &mount_csminstall($::CSM_SERVER, $::CSM_SERVER_INS_DIR, $::LOCALCSMMNTDIR);

    #Performing the osfirstboot script
    my $cmd = "$::LOCALCSMMNTDIR/osfirstboot -f "."$::LOCALCSMMNTDIR/config/"."$::CONFIG_INFO_NAME";
	print $::LOG_FILE_HANDLE &get_current_time();
    print $::LOG_FILE_HANDLE "Running command: $cmd.\n";
    my $rc = system("$cmd");
    if ($rc >> 8)
    {
		print $::LOG_FILE_HANDLE &get_current_time();
        print $::LOG_FILE_HANDLE "Performing the osfirstboot script failed.\n"
    }

    &umount_csminstall($::LOCALCSMMNTDIR);

    #Clean the csmfirstbootstrap script
    system("/bin/rm /var/csmfirstbootstrap");

    &stop_log;
}


sub initialize
{
    $::MKDIR           = "/bin/mkdir";
    if ($^O =~ /^aix/i)
    {
        $::MOUNT  = "/usr/sbin/mount";
        $::UMOUNT = "/usr/sbin/umount";
    }
    else
    {
        $::MOUNT  = "/bin/mount";
        $::UMOUNT = "/bin/umount";
    }

    $::CSMCLIENTMNTDIR = "/var/opt/csm/mnt";                     
    $::LOCALCSMMNTDIR = "$::CSMCLIENTMNTDIR/csm";


#Following attribute will be inserted by osprereboot
#    $::CSM_SERVER = "";								
#    $::CSM_SERVER_INS_DIR = "/csmserver/csm";                                
#    $::CSM_SERVER_LINUX_DIR = "/csmserver/linux";
#    $::CSM_LOG_PATH = "/var/log/csm";
#    $::CSM_INSTALL_LOG     = "/var/log/csm/install.log";

#    $::CONFIG_INFO_NAME = "";
#    $::CSM_MOUNT_POINT_ON_NODE = "";
#    $::ENV{'CSM_MOUNT_POINT_ON_NODE'} = "";

# INSERT_NODE_SPECIFIED_VAR
    
}


sub start_log
{
	my ($cmd,     $rc);

	# create the log directory if it's not already there
	if (!-d $::CSM_LOG_PATH)
	{
		$cmd = "$::MKDIR -m 644 -p $::CSM_LOG_PATH";
		$rc  = system("$cmd");
		if ($rc >> 8)
		{
			print "$::progname: 2653-158 Could not create \"$::CSM_LOG_PATH\" directory.\n";
			return 1;
		}
	}

	#
	#  get log file ready
	#
	if (!-e $::CSM_INSTALL_LOG)
	{    #  create the log file if not already there
		    # open the log file
		unless (open(LOGFILE, ">$::CSM_INSTALL_LOG"))
		{
			# Cannot open file
			print "$::progname: 2653-141 Cannot open file \"$::CSM_INSTALL_LOG\" for writing.\n";
			return 1;
		}
	}
	else
	{       # it's there so just append
		unless (open(LOGFILE, ">>$::CSM_INSTALL_LOG"))
		{
			print "$::progname: 2653-036 Could not update  \"$::CSM_INSTALL_LOG\".\n";
			return 1;
		}
	}

	$::LOG_FILE_HANDLE = \*LOGFILE;

	# Print the date to the top of the logfile
	my $sdate = `/bin/date`;
	chomp $sdate;
	print "First boot strap log is being written to \"$::CSM_INSTALL_LOG\".\n";
	print $::LOG_FILE_HANDLE
	  "---------------------------------------------------------------------\n";
	print $::LOG_FILE_HANDLE "First boot strap: Logging started $sdate.\n";
	print $::LOG_FILE_HANDLE
	  "---------------------------------------------------------------------\n";

	return ($::LOG_FILE_HANDLE);
}

sub stop_log
{
	# Print the date at the bottom of the logfile
	my $sdate = `/bin/date`;
	chomp $sdate;

	print $::LOG_FILE_HANDLE
	"---------------------------------------------------------------------\n";
	print $::LOG_FILE_HANDLE "First boot strap: Logging stopped $sdate.\n";
	print $::LOG_FILE_HANDLE
	 "---------------------------------------------------------------------\n";

	close($::LOG_FILE_HANDLE);
	$::LOG_FILE_HANDLE = undef;

	return 0;
}

#-------------------------------------------------------------------------------

=head3	mount_csminstall

	mount on the csm install directory on the csm server

	returns 0 - ok or 1 - failed 

=cut

#-------------------------------------------------------------------------------

sub mount_csminstall
{
    my ($server, $src_dir, $dest_dir) = @_;

    my ($cmd, $rc);
	if (!-d $dest_dir)
	{

		# create a local directory
		$cmd = "$::MKDIR -p $dest_dir";
		$rc  = system("$cmd");
		if ($rc >> 8)
		{
			# Could not create $::CSMINSTDIR directory.
			print $::LOG_FILE_HANDLE &get_current_time();
			print $::LOG_FILE_HANDLE "Create $dest_dir directory failed.\n";
			return 1;
		}
	}

	#  Mount the filesystem
	$cmd = "$::MOUNT -o ro $server:$src_dir $dest_dir";
	print $::LOG_FILE_HANDLE &get_current_time();
	print $::LOG_FILE_HANDLE "Running command: $cmd\n";

	#The NIC may not be so stable after the node just boot up.
	#So, here, try 3 times, and sleep 30 in between each time;
	my $times      = 3;
	my $sleep_time = 30;
	my $mounted    = 0;
	my $failed     = 0;

	while (($times > 0) && ($mounted == 0))
	{
		$times--;
		if ($failed)
		{
			sleep $sleep_time;
		}
		$rc = system("$cmd");
		if ($rc >> 8)
		{
			$failed = 1;
		}
		else
		{
			$mounted = 1;    #
		}
	}
	if (!$mounted)
	{

		#  Could not mount $srcdir.
		print $::LOG_FILE_HANDLE &get_current_time();
		print $::LOG_FILE_HANDLE "Cannot mount on the $server:$src_dir.\n";
		return 1;
	}
	return 0;
}
#-------------------------------------------------------------------------------

=head3	unmount_csminstall

	unmount the /csminstall/csm directory                #

	returns 0 - ok or 1 - failed 

=cut

#-------------------------------------------------------------------------------

sub umount_csminstall
{

	my ($unmnt_dir, undef) = @_;
	my ($rc, $cmd);

	$cmd = "$::UMOUNT $unmnt_dir";
	print $::LOG_FILE_HANDLE &get_current_time();
	print $::LOG_FILE_HANDLE "Running command: $cmd.\n";
	$rc  = system("$cmd");
	if ($rc >> 8)
	{
		# Internal call to unmount was not successful.
		print $::LOG_FILE_HANDLE &get_current_time();
		print $::LOG_FILE_HANDLE "Cannot umount $unmnt_dir.\n";
		return 1;
	}
	return 0;
}

#-------------------------------------------------------------------------------

=head3 get_current_time    

    Get the current time with time, 24-hour (hh:mm:ss) format

    returns $time

=cut

#-------------------------------------------------------------------------------
sub get_current_time
{
    #my $time = `/bin/date +%T`;
    my ($sec, $min, $hour, $mon, $year, $wday, $yday, $isdst) =
            localtime(time);
    my $time = $hour . ":" . $min . ":" . $sec ." ";
    chomp $time;
    return "$time";    
}
