#!/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 
# @(#)49   1.6   src/csm/install/predefined-lpcmds.perl, setup, csm_rfish, rfishs001b 7/18/05 12:46:20

use strict;
use locale;
BEGIN
  {
	# this enables us to redirect where it looks for other CSM files during development
	$::csmroot = $ENV{'CSM_ROOT'} ? $ENV{'CSM_ROOT'} : '/opt/csm';
	$::csmpm = "$::csmroot/pm";
	$::csmbin = "$::csmroot/bin";
  }
use lib $::csmpm;
require NodeUtils;
use Getopt::Std;

$ENV{'CT_MANAGEMENT_SCOPE'}=1;

#TODO: if this is used beyond HMC
#   --NLS Messages

sub usage {
  
  exit (scalar(@_) ? $_[0] : 1); 
}

# Parse the cmd line args and check them
if (! getopts('hdfmrvV') ) { &usage; }
if ($::opt_h) { &usage(0); }
if (scalar(@ARGV) > 0 || ($::opt_m && ($::opt_r||$::opt_d||$::opt_f))) { &usage; }

if (!$::opt_r && !$::opt_m && !$::opt_f) { $::opt_d = 1; } #d is the default
$::VERBOSE = $::opt_v || $::opt_V;
$::maxrc = 0;

# These are the definitions of the LPRM commands that will be created.  This is a hash in
# which the key is the name of the command
# Using the mklpcmd instead of mkrsrc-api because of the ACL support
my %commands = (
	mgmtsvr => '-R mgmtsvr mgmtsvr /opt/csm/bin/mgmtsvr hscroot@LOCALHOST rx root@LOCALHOST rx',
	preprpnode => '-R preprpnode preprpnode /usr/sbin/rsct/bin/preprpnode hscroot@LOCALHOST rx root@LOCALHOST rx',
        addrpnode => '-R addrpnode addrpnode /usr/sbin/rsct/bin/addrpnode hscroot@LOCALHOST rx root@LOCALHOST rx',
        rmrpnode => '-R rmrpnode rmrpnode /usr/sbin/rsct/bin/rmrpnode hscroot@LOCALHOST rx root@LOCALHOST rx',
        startrpdomain => '-R startrpdomain startrpdomain /usr/sbin/rsct/bin/startrpdomain hscroot@LOCALHOST rx root@LOCALHOST rx',
        startrpnode => '-R startrpnode startrpnode /usr/sbin/rsct/bin/startrpnode hscroot@LOCALHOST rx root@LOCALHOST rx',
	stoprpdomain => '-R stoprpdomain stoprpdomain /usr/sbin/rsct/bin/stoprpdomain hscroot@LOCALHOST rx root@LOCALHOST rx',
        stoprpnode => '-R stoprpnode stoprpnode /usr/sbin/rsct/bin/stoprpnode hscroot@LOCALHOST rx root@LOCALHOST rx',
        mkrpdomain => '-R mkrpdomain -s /opt/csm/bin/HMCCmdFilter -A "ClusterPeerDomain" mkrpdomain /usr/sbin/rsct/bin/mkrpdomain hscroot@LOCALHOST rx root@LOCALHOST rx',
        rmrpdomain => '-R rmrpdomain -s /opt/csm/bin/HMCCmdFilter -A "ClusterPeerDomain" rmrpdomain /usr/sbin/rsct/bin/rmrpdomain hscroot@LOCALHOST rx root@LOCALHOST rx',
        lsrpdomain => '-R lsrpdomain lsrpdomain /usr/sbin/rsct/bin/lsrpdomain hscroot@LOCALHOST rx root@LOCALHOST rx',
        licutil => '-R licutil licutil /opt/csm/csmbin/licutil hscroot@LOCALHOST rx root@LOCALHOST rx',
        lsrpnode => '-R lsrpnode lsrpnode /usr/sbin/rsct/bin/lsrpnode hscroot@LOCALHOST rx root@LOCALHOST rx',
);

&waitForRSCT();
if ($::opt_r || $::opt_f) {  &removeall; }
elsif ($::opt_m) { &queryall; }
if ($::opt_d){ &queryall; }
if ($::opt_d || $::opt_m || $::opt_f) { &defineall; }

exit $::maxrc;


# Removes all the predefined conditions and responses
sub removeall
  {
    &remove(\%commands, "LPCommands");
  }

sub remove
  {
	my ($hashref, $resclass) = @_;
	# Use the list of keys in the above hash to delete all the predefined resources
	my $where = q/"Name IN ('/ . join("','",keys(%$hashref)) . q/')"/;
	&run('rmrsrc-api', '-i', "-s IBM.${resclass}::$where");
  }

# See what resources are already defined
sub queryall
  {
    &query(\%commands, "LPCommands");
  }

sub query
  {
	my ($hashref, $resclass) = @_;
	my $outref = &run('lsrsrc-api', "-i -D ':|:'", "-s IBM.${resclass}::::Name");   # Get the current list of resources
	foreach my $g (@$outref) { delete $$hashref{$g}; }    # Remove all these entries from our hash
  }


# Define the resources
sub defineall
  {
    &define(\%commands);
  }

sub define
  {
	my ($hashref) = @_;
	if (!scalar(%$hashref)) { return; }      # query() must have deleted them all

	# Build the mkrsrc-api argument string with all the conditions in it
	my $s = '';
	foreach my $k (keys %$hashref)
	  {
		my $args = $$hashref{$k};
		if($::VERBOSE){ print "Running: /usr/sbin/rsct/bin/mklpcmd $args\n"; }
		system("/usr/sbin/rsct/bin/mklpcmd $args");
	  }

  }

# Run a cmd
sub run
  {
	my ($rmccmd, $options, $resources) = @_;
	my $outref = NodeUtils->runrmccmd($rmccmd, $options, $resources);
	my $rc = $::RUNCMD_RC;
	$::maxrc = ($rc>$::maxrc ? $rc : $::maxrc);
	return $outref;
  }

sub waitForRSCT{
  my $i;
  my $rmc_ok=0;
  
  for ($i=1; $i<=15; $i++) {
    # this should tell us if the RMC subsystem is available
    my @output = NodeUtils->runcmd("/usr/bin/lsrsrc", -1);
    if(!$::RUNCMD_RC){
      # ok - we're good to go!
      $rmc_ok=1;
      # The RMC subsystem has responded.
      last;
    } else {
      sleep 2;
    }
  }
  if ($rmc_ok) {
    #  now check for ERRM, AuditRM, and ctrmc
    &waitForLPRM();
  } else {
    # new msg needed - RMC not responding
    # The RMC subsystem has not started.
    return 1;
  }
  return 0;
}


sub isLPRMrunning{
  my @output = NodeUtils->runcmd("LANG=C /usr/bin/lssrc -s IBM.LPRM", -1);
  if ($::RUNCMD_RC) { return 0; }   # maybe we should try to catch real errors here
  my ($subsys, $group, $pid, $status) = split(' ', $output[1]);
  if (defined($status) && $status eq 'active') { 
    #now check to see if IBM.AuditRM is up
    return 1;
  }
  return 0;
}

sub waitForLPRM{
  # The ERRM resource mgr is slow in starting.  Wait until it starts before
  # continuing or else it can cause problems later on
  my $i;
  for ($i=1; $i<=15; $i++)
    {
      if (&isLPRMrunning())
	{
	  my $ctrmc_up = 0;
	  #now check if ctrmc is up 
	  my @output = NodeUtils->runcmd("LANG=C /usr/bin/lssrc -s ctrmc", -1);
	  if(!$::RUNCMD_RC){
	    my ($subsys, $group, $pid, $status) = split(' ', $output[1]);
	    if (defined($status) && $status eq 'active'){
	      $ctrmc_up =1;
	    }
	    }
	  if($ctrmc_up==1){
	    last;
	  }
	}
      sleep 2;
    }
}


