#!/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 = "@(#)68   1.8   src/rsct/rm/LPRM/cli/bin/rmlpcmd.perl, LPRM, rsct_rady, rady2035a 11/12/15 16:38:32"

# Removes an RMC IBM.LPCommand resource

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 = 'rmlpcmd';
$main::MSGCAT = 'lprmcli.cat';
$main::LSMSG = '/opt/rsct/bin/ctdspmsg';

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

# Parse the cmd line args and check them
#if (! getopts('an:hvV') ) { &usage; }
if (! getopts('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 $selectstr = q/Name IN ('/ . join("','",@ARGV) . q/')/;

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 ($::opt_T) {print STDERR "$main::PROGNAME: calling rmrsrc-api to remove LP resource\n ";}

my $cmd = qq(/usr/bin/rmrsrc-api -I $DELIMITERI -D $DELIMITERO -s IBM.LPCommands${DELIMITERI}$selectstr 2>&1);
if ($::opt_V) { $main::PROGNAME = 'lslpcmd'; printIMsg('IMsgRmcCmd', $cmd); $main::PROGNAME = 'rmlpcmd'; }

#if ($::opt_v) { $main::PROGNAME = 'lslpcmd'; printIMsg('IMsgRmcCmd', $cmd); $main::PROGNAME = 'rmlpcmd'; }

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

exit $rc;