#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2010,2019 
# 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 

################################################################################
# Arguments are:
#
# ARGV[0] - what action is taking place - [add|delete|activate|deactivate]
# ARGV[1] - what object are we talking about? [cec|partition]
# ARGV[2] - what specific object are we talking about? - [mt*ms|lparid*mt*ms]
# 
################################################################################

$RUNACT_API = "unset CT_MANAGEMENT_SCOPE;/opt/rsct/bin/runact-api";
$RMRSRC_API = "unset CT_MANAGEMENT_SCOPE;/opt/rsct/bin/rmrsrc-api";
$REFRSRC = "/opt/rsct/bin/refrsrc";
$CSM_CMD = "/opt/csm/csmbin/partitionStatusChangeEvent.pl";
$CUHA_HMC_TYPE_FILE = "/opt/hsc/data/hmcType.properties";

$program_name = `basename $0`; chomp $program_name;
$log_file = "/tmp/$program_name" . ".log";
$time = localtime();

if ( $#ARGV != 2 ) {
	 usage();
	 exit 1;
}

# look if the HMC type file is specified via an environment variable (for override)
$fname = $ENV{'CUHA_HMC_TYPE_FILE'};
if ($fname eq '') {
    $fname = $CUHA_HMC_TYPE_FILE;
}

# open the hmcType.properties file
$rc = open(HMC_FILE, $fname);
if ($rc == 0) {
    logMessage("error: cannot open $fname");
    exit 1;
}
 
@lines = <HMC_FILE>;
foreach $line (@lines) {
    # check whether on a MM system 
    if ($line =~ /HMCTYPE=essHMC/) {
        $CSM_CMD = "$CSM_CMD $ARGV[0] $ARGV[1] $ARGV[2]";
        logMessage("Invoking $CSM_CMD");
        exec($CSM_CMD) || logMessage("error: could not exec");    # no NMD support for now on DS/8K HMC
        exit(1);
    }
}
    
close(fp);

$Action = $ARGV[0];
$Object = $ARGV[1];
$ObjectSpecifier = $ARGV[2];
$Refresh = 0;
logMessage("$Action $Object $ObjectSpecifier");

$Task = $Action."_".$Object;
($s1, $s2, $s3) = split(/\*/, $ObjectSpecifier);

if ($Task eq "add_partition") {
    $command = "$RUNACT_API -c IBM.MngNode::::AddLPAR::LPARName::\"$ObjectSpecifier\"";
    $Refresh = 1;
}
elsif ($Task eq "activate_partition") {
    $command = "$RUNACT_API -s IBM.MngNode::'LParID=\"$s1\" and HWModel=\"$s2\" and HWSerialNum=\"$s3\"'::UpdatePowerStatus::PowerStatus::1";
    $Refresh = 1;
}
elsif ($Task eq "deactivate_partition") {
    $command = "$RUNACT_API -s IBM.MngNode::'LParID=\"$s1\" and HWModel=\"$s2\" and HWSerialNum=\"$s3\"'::UpdatePowerStatus::PowerStatus::0";
}
elsif ($Task eq "delete_partition") {
    $Refresh = 1;
}
elsif ($Task eq "delete_cec") {
    $command = "$RMRSRC_API -s IBM.MngNode::'HWModel=\"$s1\" and HWSerialNum=\"$s2\"'";
}
elsif ($Task eq "activate_cec") {
    $command = "$REFRSRC IBM.MngNode";
}
elsif ($Task eq "add_cec") {
    exit 0;
}
else {
    logMessage("error: $Task is not implemented");
    exit 1;
}

if ($Refresh == 1) {
    $refcommand = "$REFRSRC IBM.MngNode";
    $rc = system($refcommand);

    if ($rc == -1) {
        logMessage("error: $refcommand failed to execute");
    }
    elsif ($rc= $rc >> 8) {
	logMessage("error: $refcommand rc= $rc");
    }
}

$rc = system($command);

if ($rc == -1) {
    logMessage("error: $command failed to execute");
}
elsif ($rc= $rc >> 8) {
	logMessage("error: $command rc= $rc");
}

exit $rc;




################################################################################
sub usage() {
     print "\n";
     print "purpose: Notify IBM.MgmtDomainRM or IBM.DMSRM about changes to status for certain CIMOM managed resources.\n";
     print "\n";
     print "usage: $me <action> <object type> <object specifiers>\n";
     print "\n";
     print "   <action> - one of [add|delete|activate|deactivate]\n";
     print "   <object> - one of [cec|partition]\n";
     print "   <object specifiers>:\n";
     print "       for 'cec' objects, machine type and serial number: 'mt*sn'\n";
     print "       for 'partition' objects, LPARid, machine type and serial number: 'lparid*mt*sn'\n";
     print "\n";
}




################################################################################
sub logMessage() {

	my $message = shift;
	$message =~ s/'"//g;

	$cmd = "echo '$time $message' >> $log_file";

	system($cmd);
}
