#!/usr/bin/perl 
## @file pstat
# @brief
# @details this script provides interface to print patches statistics
# @author Peter Kascheev
# @date
# @version 1.00 - Initial version
# @version 2.00 - using patching object is added, using semaphore is introduced
# @version 2.01 - Two colons were removed before shared functions and variables.
# @version 2.02 - Default for no options changed to --list instead of --all

=pod

=head1 NAME

B<pstat> - Show a list of installed patches.

=head1 SYNOPSIS

B<pstat> B<< <handle> >>|B<-l, --list>|B<-a, --all>|B<-h, --help>

=head1 DESCRIPTION

Print a status summary of all loaded, in-service and out-of-service patches.

=head1 OPTIONS

=over 4

=item B<< <handle> >>

for information about patch with handle B<< <handle> >>

=item B<-l, --list>

to list all installed in-service patches

=item B<-a, --all>

to list all in-service and out-of-service patches in detail

=item B<-h, --help>

Print this help message and exit

=back

=head1 EXAMPLES

=over 4

=item Print information about patch with handle 0

$ B<pstat 0>
 Handle:   0
 Type:     PATCH
 Filename: p19000_1.ntl
 Dependency: None
 CR number: Q000000000
 Engineer: John Doe
 Created: Tue Sep 16 15:49:33 2008
 Loaded: Tue Sep 23 12:21:59 2008
 Patch is in-service
 In-service date: 10/03/07  01:24:08
 Special Instructions: no
 Applications stopped: None
 Applications started: None
 Requires reboot: No
 PrePatch script: No
 PostPatch script: No
 Patch member type: JAR
 RPM: nortel-cs1000-vtrk-6.00.00-00.i386.rpm
                                                                                            
=item Print information all installed in-service patches

$ B<pstat -l>
 Product Release: 6.00.00.00
 In service patches: 1
 PATCH#  NAME       IN_SERVICE  DATE      SPECINS  TYPE   RPM
 1       p20002_1   yes         10/03/07  no       BIN    mlocate-0.15-1.el5

 Applied service updates: 1
 SU#  IN_SERVICE  DATE       SPECINS   REMOVABLE  NAME
 4    yes         10/03/07   no        yes        acl-2.2.39-2.1.el5.001

=item To list all installed patches in detail

$ B<pstat -a>
 In system patches:5

 Handle:   0
 Type:     PATCH
 Filename: p19000_1.ntl
 Dependency: None
 CR number: Q000000000
 Engineer: Ana Alvarez
 Created: Tue Sep 16 15:49:33 2008
 Loaded: Tue Sep 23 12:21:59 2008
 Patch is in-service
 In-service date: 10/03/07  01:24:08
 Special Instructions: no
 Applications stopped: None
 Applications started: None
 Requires reboot: No
 PrePatch script: No
 PostPatch script: No
 Patch member type: JAR
 RPM: nortel-cs1000-vtrk-6.00.00-00.i386.rpm
                 
 Handle:   1
 Type:     PATCH
 Filename: p20002_1.ntl
 Dependency: None
 CR number: Q00000001
 ...........
                                                         
=back

=head1 SEE ALSO

B<pload>, B<pins>, B<poos>, B<pout>, B<plis>

=cut

use lib "/opt/nortel/base/lib/";

use warnings;
use strict;

use common_variables;
use common_functions;
use English;
use Switch;
use baselog;
use basedb;

use Getopt::Long;

use patchutil::patching;
use base_semctl;

sub main();  #entry point

exit(main());

sub main()
{
   if (warning_if_not_nortel_grp())
   {
      return $ERROR;
   }

   set_subsystem('Base_Patch');
   my $ret = $OK;
   my $patches;
   my $semmngr;

   my %params = (
                 'l'    => "",
                 'a'    => "",
                 'help' => ""
                );
   Getopt::Long::Configure("require_order");
   my $res = GetOptions(\%params, 'l|list', 'a|all', 'help|h');

   #Parameters checking
   if ($params{'help'})
   {
      print_pod_usage();
      return $OK;
   }
   if (!$res)
   {
      print "Incorrect options provided\n";
      print_pod_usage();
      return $ERROR;
   } ## end if (!$res)

   signals_ignore();
   $semmngr = base_semctl::new("PATCH");
   if (!defined($semmngr))
   {
      my $error_descr = $!;
      baselog($LOG_ERROR, "Cannot create semaphore manager object ($error_descr).");
      return $ERROR;
   } ## end if (!defined($semmngr)...
   if (!$semmngr->sem_take($SEM_PATCH, IPC_NOWAIT))
   {
      baselog($LOG_ERROR, "Cannot take semaphore.");
      baselog($LOG_ERROR, "patching operation aborted.\n", $TRUE);
      undef $semmngr;
      return $ERROR;
   } ## end if (!$semmngr->sem_take...

   $patches = new patchutil::patching;
   if (not defined $patches)
   {
      baselog($LOG_ERROR, 'The "patching" creation failed');
      if (!$semmngr->sem_give($SEM_PATCH))
      {
         baselog($LOG_ERROR, "Cannot release semaphore.");
      }
      undef $semmngr;
      return $ERROR;
   } ## end unless (defined $patches)
   else
   {
      if (!defined(@ARGV) or $params{'l'})
      {
         unless ($patches->show_pstat_extended())
         {
            baselog($LOG_ERROR, "Can not get patch status\n", $TRUE);
            $ret = $ERROR;
         }
      } ## end if (!defined(@ARGV) or...
      elsif ($params{'a'})
      {
         unless ($patches->show_pstat())
         {
            baselog($LOG_ERROR, "Can not get patch status\n", $TRUE);
            $ret = $ERROR;
         }

      } ## end elsif ($params{'a'})
      elsif (scalar @ARGV != 1)
      {
         print "Error: Wrong number of parameters.\n";
         print_pod_usage();
         $ret = $ERROR;
      } ## end elsif (scalar @ARGV != 1)
      elsif (defined $ARGV[0])
      {
         if ($ARGV[0] =~ /^\d+$/)
         {
            unless ($patches->show_pstat($ARGV[0]))
            {
               baselog($LOG_ERROR, "Can not get patch status\n", $TRUE);
               $ret = $ERROR;
            }
         } ## end if ($ARGV[0] =~ /^\d+$/...
         else
         {
            print "Error: Handle must be numeric.\n";
            print_pod_usage();
            $ret = $ERROR;
         } ## end else [ if ($ARGV[0] =~ /^\d+$/...
      } ## end elsif (defined $ARGV[0])
   }

   undef $patches;
 
   if (!$semmngr->sem_give($SEM_PATCH))
   {
      baselog($LOG_ERROR, "Cannot release semaphore.");
      $ret = $ERROR;
   } ## end if (!$semmngr->sem_give...

   undef $semmngr;
   return $ret;
} ## end sub main()

