#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2002,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 
# @(#)92   1.10   src/csm/install/write_status.perl, setup, csm_rfish, rfishs001b 11/10/05 04:21:41

# Tool to write a line to the status file remotely using atftp.

use strict;
BEGIN { $::csmpm = $ENV{'CSM_PM'} ? $ENV{'CSM_PM'} : '/opt/csm/pm'; }
use lib $::csmpm;
use Getopt::Std;
use CSMDefs;
require MessageUtils;

$::MSGCAT     = 'csmInstall.cat';
$::MSGMAPPATH = $ENV{'CSM_MSGMAPS'} ? $ENV{'CSM_MSGMAPS'} : '/opt/csm/msgmaps';
$::MSGSET     = 'csminstall';

$::progname = "write_status";

sub usage
{
	print <<ENDOFUSAGETEXT;
Usage:  write_status [-h]
	write_status [-v | -V] -p <atftp_path> -S <atftp_server_hostname> 
	             -n <node_hostname> [-s <status>] <msg>
ENDOFUSAGETEXT
	exit 1;
}

sub getArgs
{
	if (!getopts('hn:p:s:S:vV')) { &usage; }
	if ($::opt_h) { &usage }

	$::VERBOSE = $::opt_v || $::opt_V;

	# atftp path
	if ($::opt_p)
	{
		$::ATFTP = $::opt_p;
	}

	# atftp Server hostname
	if ($::opt_S)
	{
		$::TFTP_HOST = $::opt_S;
	}

	# Node hostname
	if ($::opt_n)
	{
		$::NODE_HOSTNAME = $::opt_n;
	}

	# Status
	if ($::opt_s)
	{
		$::STATUS = $::opt_s;
	}

	# The rest of the parameters are taken as the message
	if (scalar(@ARGV) > 0)    # catenate the rest into the message
	{
		foreach my $m (@ARGV)
		{
			if ($::MSG) { $::MSG .= " " . $m; }
			else { $::MSG = $m; }
		}
	}

	#
	# check for usage errors
	#
	unless ($::NODE_HOSTNAME and $::MSG)
	{
		&usage;
	}
	$::STATUS = "" if (!$::STATUS);
}

sub open_logfile
{
	my ($logfile) = @_;

	system('mkdir -p /var/log/csm');
	unless (open(LOGFILE, ">>$logfile"))
	{

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

	$::LOG_FILE_HANDLE = \*LOGFILE;
	return 0;
}

sub close_logfile
{
	close($::LOG_FILE_HANDLE);
}

#
# Function to add some text to the kickstart.log file on the node.
#
sub write_log
{
	my ($output) = @_;
	my ($date)   = `date`;
	chomp $date;
	print $::LOG_FILE_HANDLE "$date: " . $output;
}    # END write_log

#
# Function to add a line to the status file on the management server.
# The status file is in the /csminstall/csm/status directory on the
# management server.
#
# The first argument to this function is the message.  It should be quoted
# if it contains spaces.  The second optional argument is a status.  This is
# typically used to send the exit code of a command to the status log.
#
# The format of the output message in the status file is:
#    <date>: <message>: <status>
# If <status> is not provided, the format of the message in the status file is:
#    <date>: <message>
#
# This function updates the corresponding node's status file
sub write_status
{
	my ($msg, $status) = @_;

	chomp $msg;

	#my $statusFile = "/tftpboot/status/$::NODE_HOSTNAME";
	my $statusFile = $::CSMSTATUSDIR . "/" . $::NODE_HOSTNAME;

	open(STATUS_FILE, ">>$statusFile")
	  || die
	  MessageUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH, 
                                   'csminstall',     'E1', 
                                   'EMsgCANT_WRITE_FILE', $statusFile
                                   );

	# Add a line to the status file
	my $date = `date`;
	chomp $date;
	if ("$status")
	{
		print STATUS_FILE $date . ": $msg: status=$status\n";
	}
	else
	{
		print STATUS_FILE $date . ": $msg\n";
	}
	close(STATUS_FILE);

}    # END write_status

#
# MAIN Main main
#
&getArgs;

$::INSTALL_LOG = "/var/log/csm/install.log";
my $rc = &open_logfile($::INSTALL_LOG);
($rc) && die
MessageUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH, 
                             'csminstall',     'E1', 
                             'EMsgCANT_OPEN', $::INSTALL_LOG
                             );
&write_status($::MSG, $::STATUS);
&close_logfile;

