#!/usr/bin/perl

# --------------------------------------------------------------------------------
# -- This is an Inventory Scout SIMPLE module --
# -- Purpose : Perform VPD Survey for - Unallocated Resources
# --           using "lshsc -d -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 Unallocated Resource VPD Survey in XML format
my ( $data ) = &get_UnallocatedResourceXML ( $mtms_String );
unless ( $data )
{
  print STDERR "lshsc returns NOTHING\n\n";
  exit ( 255 );
}

# ----------------------------------------
# -- lshsc may return a blank ResourceSet which can be ignored by the concatenator module 
if ( 0 == &verify_UnallocatedResourceXML_isValid ( \$data ) )
{
  print "$data";   # -- prints the original output of lshsc --
  exit ( 0 );      # -- to be ignored by the caller
}

# ----------------------------------------
# -- NOTE: The Result of UnallocatedResource VPD Survey may be incomplete
# --       Check if modification is required.
if  ( 1 == &verify_UnallocatedResourceXML_needsToBeModified ( \$data ) )
{
  &modify_UnallocatedResourceXML ( $mtms_String, \$data );
}



# ----------------------------------------
# -- 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_UnallocatedResourceXML
{
  my ( $in_MTMS_String ) = @_;
  # ----------------------------------------
  # -- Execute 'lshsc -d -i -x -c MT-M_S' to get UnallocatedResource VPD XML --
  my ( $cmd_UnallocatedResource ) = $cCommand_lshsc . " -d -i -x -c " . $in_MTMS_String;
  open ( fIN, "$cmd_UnallocatedResource |" );
  @data = <fIN>;
  close (fIN);

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


# --------------------------------------------------------------------------------
sub verify_UnallocatedResourceXML_isValid
{
  my ( $ioXML_DataRef ) = @_;

  my ( $isValid ) = (1);  # -- Assume True / Valid --

  $isValid = 0 if ( ${$ioXML_DataRef} !~ /ResourceList displayName="Unallocated Resources"/ );


  return ( $isValid );
}

# --------------------------------------------------------------------------------
sub verify_UnallocatedResourceXML_needsToBeModified
{
  my ( $ioXML_DataRef ) = @_;

  my ( $needsModification ) = (1);  # -- Assume True

  $needsModification = 0 if ( ${$ioXML_DataRef} =~ /ResourceList displayName="Enclosures"/ );

  return ( $needsModification );
}



# --------------------------------------------------------------------------------
sub modify_UnallocatedResourceXML
{
  my ( $in_MTMS_String, $ioXML_DataRef ) = @_;
  
  # -- System's MTMS (inside the XML) must have format:
  # --   MT-M-S
  # -- However, this module is called with "MT-M_S"
  # -- Thus, we need to change "_" to "-" inside the XML
  $in_MTMS_String =~ s/_/-/;


  # ----------------------------------------
my ( $addonBegin ) =<<XML

<Resource displayName="$in_MTMS_String" uniqueId="$in_MTMS_String">
  $in_MTMS_String
  <ResourceList displayName="Enclosures">
XML
;
  
  
  # ----------------------------------------
my ( $addonEnd ) =<<XML
  </ResourceList>
</Resource>
XML
;
  

  # ----------------------------------------
  ${$ioXML_DataRef} =~ s/>/>\n/g;
  ${$ioXML_DataRef} =~ s/(<ResourceSet.*?>)/$1\n$addonBegin/;
  ${$ioXML_DataRef} =~ s/(<\/ResourceSet.*?>)/$addonEnd\n$1/;


  # -- Data were modified by reference, nothing needs to be returned --
}
