#!/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 
# @(#)28   1.13   src/rsct/rm/SensorRM/cli/bin/lssensor.perl, sensorcli, rsct_relgh, relghs001a 1/17/07 13:08:33

# Lists resources from the RMC IBM.Sensor class

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::Std;
use CT_cli_utils qw(printIMsg printEMsg);
use CT_cli_display_utils qw(set_display);

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

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


# Parse the cmd line args and check them
if (! getopts('AadD:ln:htvVx') ) { &usage; }
if ($::opt_h) { &usage(0); }
if ((scalar(@ARGV)>0 && $::opt_A) ||
    ($::opt_a && $::opt_n))
{
    &usage;
}

if ($::opt_V) { $::opt_v = 1; }

my $options = '';
my $selectstr = '';
my $attr = '';
my $format = "column";
my $delim= "";
my $No_HDR= 0;

if ($::opt_A)       # show everything
{
    $options = "-D ':|:' -n -i";
    $attr = "'*b0x0020'";
    $format = "old_long";
}
elsif (scalar(@ARGV) >= 1)       # show all attributes of this sensor
{
    $options = "-D ':|:' -n";
    $attr = "'*b0x0020'";
    $selectstr = q/Name IN ('/ . join("','",@ARGV) . q/')/;
    $format = "old_long";
}
else           # show all sensor names
{
    $options = "-D ':|:' -n -i";
    $attr = 'Name';
    if ($::opt_a || defined($::opt_n)) { $attr .= '::NodeNameList'; }
}

if ($::opt_l)
{   
#   if (!$No_HDR)
#   {
        $format= "long";
#   }
}
elsif ($::opt_t)
{
    $format = "column";
    $delim= ":|:";
}
elsif($::opt_d)
{
    $format = "delim";
    $delim= ":";
}
elsif ($::opt_D)
{
    $format= "delim"; 
    $delim= "$::opt_D";
}

if ($::opt_x)
{
    $No_HDR= 1;
}


if ($::opt_a || defined($::opt_n))
{
    if ($ENV{CT_MANAGEMENT_SCOPE}!=2 && $ENV{CT_MANAGEMENT_SCOPE}!=3)
    {
        $ENV{CT_MANAGEMENT_SCOPE}=4;
    }
}

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"); }

my $cmd = qq(/usr/bin/lsrsrc-api $options -s IBM.Sensor::${selectstr}::$attr 2>&1);
if ($::opt_v) { printIMsg('IMsgRmcCmd', $cmd); }

my @output = `$cmd`;

my $rc = $? >> 8;
if ($rc)
{
    if (!$::opt_v) { $output[0] =~ s/.*:\|:.*:\|:.*:\|:.*:\|:.*:\|://; }
    print @output;
    exit $rc;
}
chop(@output);

# Display output
if ($format eq "old_long") 
{ 
    &displayLong;
}
else
{
    my $divider= `$main::LSMSG $main::PROGNAME $main::MSGCAT IMsgLssensorDivider`;
    my $line;
    my $i;
    my $j= 1;
    my @Lof_conds=();

    # Create a Header
    $Lof_conds[0][0] = `$main::LSMSG $main::PROGNAME $main::MSGCAT IMsgLssensorOut`;
    if ($format eq "delim")
    {
        $Lof_conds[0][0]= $Lof_conds[0][0] . "\n";
    }

    $line = $output[0];
    my %attrnames = split(/:\|:/, $line);
    $j = 1;
    $Lof_conds[0][$j]= "Name";  
    delete $attrnames{Name};

    foreach my $attrname (sort keys %attrnames)
    {
        $j++;
        $Lof_conds[0][$j]= $attrname;
    }
    
    $j = 1;

    foreach $line (@output)
    {
      
     if ($j == 1) { $Lof_conds[$j][0] = "$divider $j:"; }
     else  { $Lof_conds[$j][0] = "\n$divider $j:"; }
      
      my %attrs = split(/:\|:/, $line);
       
      # Convert the value of TimeCommandRun to a user friendly one
      if (defined($attrs{TimeCommandRun}))
      {
          $attrs{TimeCommandRun}= localtime($attrs{TimeCommandRun});
      }

      # Make sure the hostname is displayed 1st
      if (defined($attrs{Name}))
      {
          $Lof_conds[$j][1]= $attrs{Name};
          $i= 2;
          delete $attrs{Name};     # so we do not repeat it in the following loop
      }
      else
      {
          $i= 1;
      }

      foreach my $k (sort keys %attrs)
      {
        # Fill the Attributes values
        $Lof_conds[$j][$i]= $attrs{$k};
        $i++;
      }
      $j++;
    }
    
    my $row_count = $#Lof_conds;
    my $col_count = scalar(@{$Lof_conds[0]});
    $rc = set_display($format, $No_HDR, $row_count, $col_count, \@Lof_conds, $delim);
}



sub displayLong
{
my $firsttime = 1;
foreach my $l (@output)
  {
	if (!$firsttime) { print "-------------------\n"; }   # separate this resource from the previous
	else { $firsttime = 0; }

	# takes the alternating attrs and values and puts them in the hash correctly
	my %attrs = split(/:\|:/, $l);


	# Make sure the hostname is displayed 1st
	if (defined($attrs{Name}))
	  {
		print " Name = $attrs{Name}\n";
		delete $attrs{Name};          # so we do not repeat it in the following loop
	  }

        # Convert the value of TimeCommandRun to a user friendly one
        $attrs{TimeCommandRun}= localtime($attrs{TimeCommandRun});

	# Now display the rest of the attributes, sorted
	foreach my $k (sort keys %attrs)
        {
  	  print " $k = $attrs{$k}\n"; 
        }
  }
}
