#!/usr/bin/perl

# --------------------------------------------------------------------------------
# -- This is an Inventory Scout SIMPLE module --
# -- Purpose : Survey the (HMC) Hardware Management Console's hardware VPD information
# --           using "lshsc -v -i -x -c MT-M_S" command
# --
# -- INPUT   : "-c MT-M_S" parameter MUST be supplied
# -- OUTPUT  : 
# --   Result  = an XML file format of the VPD Survey 
# --   STDOUT  = File Name of the XML result file.
# -- 
# --------------------------------------------------------------------------------


# --------------------------------------------------------------------------------
$cInvScout_DataDirectory = "/var/adm/invscout/";
$cInvScout_VPD_Directory = "VPD/";
$cCommand_lshsc          = "/opt/hsc/bin/lshsc";

$cModuleName            = $0;
$cModuleName            =~ s/.+?\///g;  # -- Program Name only, No Path --

# ----------------------------------------
# -- Get Input Parameters --
my ( %inArgv ) = ();
&get_Parameters ( \%inArgv );

# ----------------------------------------
# -- Verify Input : This module requires "-c MT-M_S" --
my ( $mtms_String ) = $inArgv{"-c"};
if ( length ($mtms_String) == 0 )
{
  print STDERR "USAGE: $cModuleName -c MT-M_S\n\n";
  exit ( 255 );
}


# ----------------------------------------
# -- Get the Hardware Management Console's hardware VPD information
my ( $data ) = &get_HMC_VPD_XML ( $mtms_String );
unless ( $data )
{
  print STDERR "lshsc returns NOTHING\n\n";
  exit ( 255 );
}


# ----------------------------------------
# -- Save the Output --
my ( $outputFileName ) = $cInvScout_VPD_Directory . $cModuleName . $mtms_String . ".VPD.xml";
open ( fOUT, "> $cInvScout_DataDirectory/$outputFileName" );
print fOUT "$data";
print fOUT "\n";
close ( fOUT );

print "$outputFileName\n";


exit ( 2 );   # -- Standard RC for InvScout Complex Module --



# --------------------------------------------------------------------------------
sub get_Parameters
{
  my ( $outFlagContainerRef ) = @_;
  my ( $flag, $parameter ) = ();

  foreach $parameter (@ARGV)
  {
    if ( $parameter =~ /^-/ )
    {
      $flag = $parameter;
      ${$outFlagContainerRef}{$flag} = "";
  }
    else
    {
      ${$outFlagContainerRef}{$flag} = ( ${$outFlagContainerRef}{$flag} ?
          ${$outFlagContainerRef}{$flag}." ".$parameter :
          $parameter );
    }
  }
}



# --------------------------------------------------------------------------------
sub get_HMC_VPD_XML
{
  my ( $in_MTMS_String ) = @_;
  # ----------------------------------------
  # -- Execute 'lshsc -v -i -x -c MT-M_S' 
  # --  to get Hardware Management Console's hardware VPD information in XML format --
  # --
  my ( $cmd_UnallocatedResource ) = $cCommand_lshsc . " -v -i -x -c " . $in_MTMS_String;
  open ( fIN, "$cmd_UnallocatedResource |" );
  @data = <fIN>;
  close (fIN);

  return ( join ( "", @data ) );  # -- All in One String --
}



