#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos72X src/bos/usr/sbin/install/suma/bin/suma_swinv.perl 1.6.1.1 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2004,2021 
# 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 

use strict;
use Getopt::Std;

$ENV{"PATH"}="/usr/bin:/usr/sbin:".$ENV{"PATH"};

BEGIN {
  # SUMA modules
  use lib qw(/usr/suma/lib);

  # Make sure SUMA is installed
  eval {
    require SUMA::GConfig;
  };
  if($@) {
    print STDERR "This program requires bos.suma to be installed.\n";
    exit 1;
  }

  # Now we're okay.
  SUMA::GConfig->import(qw/:lvl setGlobal getGlobal/);
}

BEGIN {
  use SUMA::SoftwareInventory;
  use SUMA::Messenger qw(
                          MSG_USAGE_SUMA_SWINV2
                        );
}

# Error Codes
my $ecFailure=1;                # General error including _usage
my $ecPartialFailure=2;         # Some clients returned information others did not


# Default Values
my $numClients=0;               # The number of clients including localhost 
my $hostname="localhost";

setGlobal(LOGFILE_VERBOSE => LVL_OFF);
setGlobal(NOTIFY_VERBOSE => LVL_OFF);
setGlobal(SCREEN_VERBOSE => LVL_INFO);


sub _usage
   {
   mesg(LVL_ERROR, MSG_USAGE_SUMA_SWINV2);
   exit($ecFailure); 
   }







################ main ##################

# Parse Commandline
my %opts;
my $getopts_rc;
my $numClientsAndSwitches=$#ARGV + 1;  
my $currentArgNum=0;
my $moreSwitches=1;
my $lslpp_flags;
my $format="lslpp";


# parse switches
$getopts_rc = getopts('?rf:a:',\%opts);
if (!$getopts_rc) {_usage;}

if (defined($opts{"?"})) {_usage;} 
if (!defined($opts{"f"}) && defined($opts{"r"})) {_usage;} 

if (defined($opts{"f"}))
   {
   if ($opts{"f"} eq "lslpp" && defined($opts{"r"})) {_usage;}
   elsif ($opts{"f"} eq "lslpp" && !defined($opts{"r"})) {$format="lslpp";}
   elsif ($opts{"f"} eq "xml" && defined($opts{"r"})) {$format="xmlrc";}
   elsif ($opts{"f"} eq "xml" && !defined($opts{"r"})) {$format="xml";}
   else {_usage;}
   } 

if (defined($opts{"a"})) 
   {
   if ((defined($opts{"f"})) && ($opts{"f"} eq "xml")) {_usage;}
   $lslpp_flags=$opts{"a"};
   }



# Validate arguments
$numClients=@ARGV;
if (($format eq "lslpp") && ($numClients > 1)) {_usage;}

my $pid;
my $software_list_fh;
if ($numClients == 0) 
   {
   ($software_list_fh,$pid)=get_inv_sys($format,$lslpp_flags,"localhost");
   }
else
   {
   ($software_list_fh,$pid)=get_inv_sys($format,$lslpp_flags,@ARGV);
   }

print (<$software_list_fh>);
close $software_list_fh;

waitpid($pid,0);
my $rc=$? >> 8;

exit $rc;