#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2005,2007 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

#####################################################################
#                                                                   #
# Command: updateKernel                                             #
#                                                                   #
#-------------------------------------------------------------------#
#                                                                   #
#      This CSM script will update kernel rpm packages on a         #
#      Linux cluster node during a node install.                    #
#                                                                   #
#      See the updateKernel.README file for details on              #
#      how to use this customization scripts.                       #
#                                                                   #
# Exit codes:                                                       #
#     0 - All was successful.                                       #
#     1 - An error occured.                                         #
#                                                                   #
#                                                                   #
#####################################################################
# Update kernel
use strict;
$::progname = "updateKernel";
#
#CSM log directory/file
#
$::CSMLOG      = "/var/log/csm";
$::INSTALL_LOG = "/var/log/csm/install.log";
$::GLOBAL_EXIT = 0;
$::NOK         = 1;
$::OK          = 0;


#
# Append logging information to the install.log file on the node that was
#     started by osprereboot
#
&append_logging($::INSTALL_LOG);
$::logging++;

#
# check the platform
#
my $cmd_res = `uname -s -r`;
chomp $cmd_res;
($::PLTFRM, $::KERNEL_RELEASE) = ($cmd_res =~ /^(\w+)\s([\d\.]+)/);
if ($::PLTFRM ne "Linux")
{
	print $::LOG_FILE_HANDLE
	  "$::progname:  This script may only be run on a Linux system.\n";
	$::GLOBAL_EXIT = 1;
	exit;
}

my @RPMS;
my $directory = "$ENV{'SCRIPTDATAPATH'}/kernel";
unless (opendir(KERNEL, $directory))
{
	print $::LOG_FILE_HANDLE "Cannot open $directory: $!\n";
	$::GLOBAL_EXIT = 1;
	exit;
}
while(my $file=readdir(KERNEL)){
	if($file ne "." && $file ne ".."){
		my $filename = "$directory/$file";
		push @RPMS, $filename;
	}
}
unless (closedir KERNEL)
{
	print $::LOG_FILE_HANDLE "Cannot close $directory: $!\n";
	$::GLOBAL_EXIT = 1;
	exit;
}
if(@RPMS)
{
	my @output=`rpm -Uv @RPMS 2>&1`;
	if($?)
	{
		print $::LOG_FILE_HANDLE "Update kernel failed: @output\n";
		$::GLOBAL_EXIT = 1;
		exit;
	}	
}
else
{
	print $::LOG_FILE_HANDLE "No kernel RPMS to update\n";
}
exit;
#
# finish up and exit
#
END
{

	if ($::logging)
	{
		&stop_logging();
	}

	#Determine exit code
	if ($::GLOBAL_EXIT > $?)
	{
		$? = $::GLOBAL_EXIT;
	}
}

#--------------- subroutines ----------------------------------

#-------------------------------------------------------------------------#
#                                                                         #
#    append_logging                                                       #
#                                                                         #
#         Append logging messages to a logfile. Return the log file       #
#           handle so it can be used to close the file when done logging. #
#                                                                         #
#----------------------------------------#--------------------------------#
sub append_logging
{
	my ($logfile, $stat) = @_;
	my ($cmd,     $rc);

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

	#
	#  get log file ready
	#
	if (!-e $logfile)
	{

		#  create the log file if not already there
		# open the log file
		unless (open(LOGFILE, ">$logfile"))
		{

			# Cannot open file
			print
			  "$::progname: 2653-141 Cannot open file \"$logfile\" for writing.\n";
			return $::NOK;
		}
	}
	else
	{

		# it's there so just append
		unless (open(LOGFILE, ">>$logfile"))
		{
			print "$::progname: 2653-036 Could not update  \"$logfile\".\n";
			return $::NOK;
		}
	}

	$::LOG_FILE_HANDLE = \*LOGFILE;

	if ((defined $stat) && ($stat == 1))
	{
		return ($::LOG_FILE_HANDLE);
	}

	# Print the date to the top of the logfile
	my $sdate = `/bin/date`;
	chomp $sdate;
	print "Output log is being written to \"$logfile\".\n";

	print $::LOG_FILE_HANDLE
	  "---------------------------------------------------------------------\n";
	print $::LOG_FILE_HANDLE "$::progname: Logging started $sdate.\n";
	print $::LOG_FILE_HANDLE
	  "---------------------------------------------------------------------\n";

	return ($::LOG_FILE_HANDLE);
}

#--------------- subroutines ----------------------------------

#-------------------------------------------------------------------------#
#                                                                         #
#    stop_logging                                                         #
#                                                                         #
#                  Turn off message logging.  (Expects to have a file     #
#                   handle passed in. )                                   #
#                                                                         #
#--------------------------------------#----------------------------------#

sub stop_logging
{

	# Print the date at the bottom of the logfile
	my $sdate = `/bin/date`;
	chomp $sdate;
	print $::LOG_FILE_HANDLE
	  "---------------------------------------------------------------------\n";
	print $::LOG_FILE_HANDLE "$::progname: Logging stopped $sdate.\n";
	print $::LOG_FILE_HANDLE
	  "---------------------------------------------------------------------\n";

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

	return $::OK;
}

