#!/usr/sbin/rsct/perl5/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,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 
# @(#)29   1.22   src/rsct/rm/SensorRM/cli/bin/mksensor.perl, sensorcli, rsct_reneh, renehs001b 5/21/07 00:53:43

# Creates an RMC sensor

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

use lib $::rsctpm;
use Getopt::Long;
use CT_cli_utils qw(printIMsg printEMsg);
use Socket;
use CT_cli_input_utils qw(
    check_input_file
);
use MC_cli_utils qw(
     read_from_Stdin
);

$main::PROGNAME = 'mksensor';
$main::MSGCAT = 'sensorcli.cat';
$main::LSMSG = '/usr/sbin/rsct/bin/ctdspmsg';

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

Getopt::Long::Configure ("bundling", "no_auto_abbrev",
                         "no_ignore_case", "require_order",
                         "prefix_pattern=(--|-)");
                         
# Parse the cmd line args and check them
if(!GetOptions( 'h|help|version' ,
                'v' ,
                'V',
                'c=s' ,
                'u=s',
                'i=s',
                'n=s',
                'N=s',
                'e=s', ))
{
    &usage;
}
if ($::opt_h) { &usage(0); }
if (scalar(@ARGV) < 2) { &usage; }

if(defined($::opt_n) && defined($::opt_N))
{
    &usage;
}
if ($::opt_V) { $::opt_v = 1; }

my $name = shift @ARGV;
my $sscmd = join(' ', @ARGV);
my $username;
my $node_file_flag = 0;                           # node_file_flag =1         if -N was specified.
my $node_file_name = "";                          # node_file_name
my $node_file_from_stdin = 0;                     # node_file_from_stdin = 1  if -N '-' 

if (defined($::opt_c))
{
    $::opt_c= "::ControlFlags::$::opt_c";
}

if (defined($::opt_u))
{
    $username = $::opt_u;
}
else
{
    # we use whoami instead of $USER or $LOGNAME so it will work with su
    $username = `whoami`;
    chop $username;
}

if (!defined($::opt_i)) 
{ 
    $::opt_i= 60;
}
elsif ($::opt_i < 10 && $::opt_i != 0)
{
    printEMsg('EMsgMksensorInvInterval', $::opt_i);
    exit(1);
}

if (defined($::opt_n)||defined($::opt_N))
{
    if ($ENV{CT_MANAGEMENT_SCOPE}!=2 && $ENV{CT_MANAGEMENT_SCOPE}!=3)
    {
        $ENV{CT_MANAGEMENT_SCOPE}=4;
    }
    # Only 1 node is allowed with mksensor
    #$::opt_n = "::NodeNameList::{'$::opt_n'}";
}

if(defined($::opt_n))
{
  #  $::opt_n = "::NodeNameList::{'$::opt_n'}";
    my @nodes = split(/[, ]+/,$::opt_n);    
    $::opt_n = q/::NodeNameList::'/ . join("','", @nodes) . q/'/;
}

if (defined($::opt_N))
{
    $node_file_name = $::opt_N;
    $node_file_flag = 1;

    if ( $node_file_name eq "-" )
    {
        $node_file_from_stdin = 1;
    } else
    {
        if ( check_input_file($node_file_name) )
        {
            exit(1);
        }
    }
}
if (defined($::opt_e))
   {
    $::opt_e = "::ErrorExitValue::$::opt_e";
   }

# escape double quotes
$sscmd =~ s/"/\\"/sg;
# escape $ characters
$sscmd =~ s/\$/\\\$/sg;

my $cmd="";

if($node_file_flag == 0)
{
    $cmd = qq(/usr/bin/mkrsrc-api IBM.Sensor::Name::"${name}"::Command::"${sscmd}"::UserName::"${username}"::RefreshInterval::$::opt_i$::opt_c$::opt_n$::opt_e 2>&1);
}
else
{
    if($node_file_from_stdin != 0)
    {
        $node_file_name = read_from_Stdin();
    }
    $cmd = qq(/usr/bin/mkrsrc-api -N $node_file_name IBM.Sensor::Name::"${name}"::Command::"${sscmd}"::UserName::"${username}"::RefreshInterval::$::opt_i$::opt_c$::opt_n$::opt_e 2>&1);
}
if ($::opt_v) { $main::PROGNAME = 'lssensor'; printIMsg('IMsgRmcCmd', $cmd); $main::PROGNAME = 'mksensor'; }

my @output = `$cmd`;
my $rc= $?;
# remove temp node file if from STDIN
if ($node_file_from_stdin != 0)
{
    unlink($node_file_name);
}

if ($rc)
{
    if ($rc & 255)
    {
        printEMsg('EMsgMksensorSignalError', "mkrsrc-api", $rc);   
    }
    else
    {
        $rc = $rc >> 8;
        if ($output[0] =~ /^ERROR.*/)
        {
            if (!$::opt_v) 
            { 
                $output[0] =~ s/^ERROR.*:://; 
            }
        }
        else
        {
            printEMsg('EMsgMksensorNonAPIError', "mkrsrc-api", $rc);
        }
        print @output;
    }
}

exit $rc;
