#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2004,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 
# sccsid = "@(#)98   1.15   src/rsct/rm/LPRM/cli/bin/chlpcmd.perl, LPRM, rsct_rady, rady2035a 11/12/15 16:38:39"

# Changes 1 or more attributes of a resource from the RMC IBM.LPCommands class
# Change attribute values or refresh CheckSum attribute

use strict;
use locale;
BEGIN
  {
	# this enables us to redirect where it looks for other RSCT files during development
	$::rsctroot = $ENV{'RSCT_ROOT'} || '/opt/rsct';
	$::rsctpm = "$::rsctroot/pm";
	$::rsctmsgmaps = "$::rsctroot/msgmaps";
  }

use lib $::rsctpm;
use Getopt::Std;
use CT_cli_utils qw(printIMsg printEMsg);
use LPRM_cli_utils qw(process_api_error process_exit_code);
use LPRM_cli_include qw($DELIMITERI $DELIMITERO);
use Socket;

$main::PROGNAME = 'chlpcmd';
$main::MSGCAT = 'lprmcli.cat';
$main::LSMSG = '/opt/rsct/bin/ctdspmsg';

# For the usage, see lprmcli.msg
sub usage { printIMsg('IMsgChlpcmdUsageNew2');  exit (scalar(@_) ? $_[0] : 1); }

# Parse the cmd line args and check them
#if (! getopts('ran:hvV') ) { &usage; }
if (! getopts('rl:c:an:hTV') ) { &usage(3); }
#if (scalar(@ARGV) < 1 || $::opt_h) { &usage; }
if ($::opt_h) { &usage(0); }
if (scalar(@ARGV) < 1) { &usage(4); }
if ($::opt_a && $::opt_n) { &usage(5); }
#if ($::opt_V) { $::opt_v = 1; }

my $name = shift @ARGV;
my $selectstr = "Name='$name'";

if ($::opt_a || defined($::opt_n))
  {
 #todo: support symbolic names for values 2 and 3
 if ($ENV{CT_MANAGEMENT_SCOPE}!=2 && $ENV{CT_MANAGEMENT_SCOPE}!=3) { $ENV{CT_MANAGEMENT_SCOPE}=4; }
  }
else { $ENV{CT_MANAGEMENT_SCOPE}=1; }

if (defined($::opt_n))
  {
	my @nodes = split(/[, ]+/,$::opt_n);
	if (length($selectstr)) { $selectstr .= ' && '; }
	$selectstr .= q/NodeNameList IN ('/ . join("','",@nodes) . q/')/;
  }
if (length($selectstr)) { $selectstr = qq("$selectstr"); }
if (defined($::opt_l))
  {
        $::opt_l = "${DELIMITERI}Lock${DELIMITERI}$::opt_l"; 
  }
if (defined($::opt_c))
  {
        $::opt_c = "${DELIMITERI}ControlFlags${DELIMITERI}$::opt_c"; 
  }

# We have to parse the attr=val pairs so we can requote the values and replace the equals sign
foreach my $a (@ARGV)
  {
        if (defined($::opt_r))
           {
            # -r flag cannot be used with attr=value pairs
            printEMsg('EMsgChlpcmdImproperFlagUsageCombination');
            exit(5) ;
           }
	# Note: we allow a null value
	my ($at, $val) = $a =~ /^\s*(\S+?)\s*=\s*(\S*.*)$/;
	if (!length($val)) { $val = "\\\"\\\""; goto JOINNOW;}     # chrsrc-api needs something for the value
	# Put quotes around the value to protect spaces or weird chars
	# (if the value already has quotes around it, do not quote it)
	if (!( $val =~ /^(".*"|'.*')$/ ))
	  {
		# if the value has imbedded double quotes, use single quotes
		if ($val =~ /\"/) { $val = qq('$a'); }   # use the other quotes
		else 		# use double quotes
		  {
			$val =~ s/\"/\\\"/sg;     # escape embedded double quotes
			$val = qq("$val");
		  }
	  }
JOINNOW:
	$a = "${at}${DELIMITERI}$val";
  }
my $attrvals = join($DELIMITERI, @ARGV);
$attrvals = ${DELIMITERI}.$attrvals;
my $cmd = "";
my $whichcmd = "";

# decide what to runact or chrsrc ... no args to ReCalCheckSum, else change attrs.
if (scalar(@ARGV) < 1 )
   {
     if (defined($::opt_l) || defined($::opt_c))
        {
          if (defined($::opt_r))
             {
              #-r flag cannot be used with -l/-c
              printEMsg('EMsgChlpcmdImproperFlagUsageCombination');
              exit(5);
             }
          else
             {
              #if user specified -l or -c without any attr=value, its a valid
              #input, goto chrsrc
              $attrvals="";
              goto CHGATTRS;
             }
        }
      elsif (defined($::opt_r))
        {
          if ($::opt_T) {print STDERR "$main::PROGNAME: calling runact-api to recalculate check sum\n ";}
          # no args so run ReCalCheckSum action
          $cmd = qq(/usr/bin/runact-api -I $DELIMITERI -D $DELIMITERO -s IBM.LPCommands${DELIMITERI}${selectstr}${DELIMITERI}ReCalCheckSum 2>&1);
          $whichcmd = "runact-api";
          goto RECALCHKSUM;
        }
      else
        {
          &usage(5);
        }
   }
CHGATTRS:
#this is the else part - attr=val pairs were defined or -l/-c were specified
# change attr vals
if ($::opt_T) {print STDERR "$main::PROGNAME: calling chrsrc-api to change attribute value\n ";}
$cmd = qq(/usr/bin/chrsrc-api -I $DELIMITERI -D $DELIMITERO -s IBM.LPCommands${DELIMITERI}${selectstr}$attrvals$::opt_l$::opt_c 2>&1);
$whichcmd = "chrsrc-api";
RECALCHKSUM:
if ($::opt_V) { $main::PROGNAME = 'lslpcmd'; printIMsg('IMsgRmcCmd', $cmd); $main::PROGNAME = 'chlpcmd'; }
#if ($::opt_v) { $main::PROGNAME = 'lslpcmd'; printIMsg('IMsgRmcCmd', $cmd); $main::PROGNAME = 'chlpcmd'; }

my @output = `$cmd`;
#my $rc = $? >> 8;
my $rc = $?;
$rc = process_exit_code($rc);
if ($rc)
  {
	#if (!$::opt_v) { $output[0] =~ s/.*::.*::.*::.*::.*:://; }
	#if (!$::opt_V) { $output[0] =~ s/.*::.*::.*::.*::.*:://; }
        if ($::opt_T) {print STDERR "$main::PROGNAME: $whichcmd returned $rc\n ";}
	#print @output;
        process_api_error($DELIMITERO,$rc,@output);
	exit $rc;
  }

exit $rc;