#!/usr/bin/perl 
## @file spstat
# @brief
# @details this script provides interface to print service pack statistics
# @author
# @date
# @version 1.00 - Initial version
# @version 1.01 - Correction to the "-l" input
# @version 1.02 - Fix for Enhancement CR Q02059485 - CS 1000 R6.0 Linux Patching Support of "REFRESH" SP Capability

=pod

=head1 NAME

B<spstat> - Show the installed and in-service SPs.

=head1 SYNOPSIS

B<spstat> [B<-h, --help>]

=head1 DESCRIPTION

Print a status summary of all loaded and in-service SPs.

=head1 OPTIONS

=over 4

=item B<-h, --help>

Print this help message and exit

=back

=head1 EXAMPLES

=over 4

=item Print summary of installed and in-service SPs.

$ B<spstat>
 There is no SP in loaded status.
 The following SP is in service: Nortel_Service_Pack_Linux_6.00_08.ntl
 Put in service by user nortel on Tue Apr  7 15:24:50 2009.
 spins command completed with no errors detected..

=back

=head1 SEE ALSO

B<spload>, B<spins>, B<spout>

=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 patchutil::patches_target;
use patchutil::patches_public;
use patchutil::header_xml;
use base_semctl;
use base_system;

our $VERSION = "1.02";

sub main();  #entry point

exit(main());

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

   set_subsystem('Base_Patch');
   my $ret = $OK;
   my $semmngr;
   my $tag    = "";
   my $data   = "";
   my $user   = "";
   my $time   = "";
   my $bundle = "";
   my $status = "";
   my $fd;

   my %params = (
                 'l'    => "",
                 'help' => ""
                );
   Getopt::Long::Configure("require_order");
   my $res = GetOptions(\%params, 'l|list', '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).", $TRUE);
      return $ERROR;
   } ## end if (!defined($semmngr)...
   if (!$semmngr->sem_take($SEM_PATCH, IPC_NOWAIT))
   {
      baselog($LOG_ERROR, "Cannot take semaphore.", $TRUE);
      undef $semmngr;
      return $ERROR;
   } ## end if (!$semmngr->sem_take...

   unless (open($fd, "$SPLOAD_INFO_FILE"))
   {
      print "There is no SP in loaded status.\n";
   }
   else
   {
      while (<$fd>)
      {
         chomp;
         ($tag, $data) = split(/::/);
         unless (!defined($tag))
         {
            unless (!defined($data))
            {
               if ($tag eq "BUNDLE_NAME")
               {
                  $bundle = $data;
               }
               elsif ($tag eq "SPLOAD_USER")
               {
                  $user = $data;
               }
               elsif ($tag eq "SPLOAD_DATE")
               {
                  $time = $data;
               }
            } ## end unless (!defined($data))
         } ## end unless (!defined($tag))
      } ## end while (<$fd>)

      # Close file
      close($fd);
      print "The following SP is in loaded status: $bundle\n";
      if (not show_SP_type($SPLOAD_DIR))
      {
         baselog($LOG_ERROR, "Cannot show SP type.");
         return $ERROR;
      }
      print "Loaded by user $user on $time.\n\n";
      if ($params{'l'})
      {
         print "The following patches were loaded by SP $bundle:\n";
         do_system([$CAT, "$CPLIST_FILE"], {$dsSTDOUT => $TRUE});
         print "\n\n";
      } ## end if ($params{'l'})
   } ## end else

   $user   = "";
   $time   = "";
   $bundle = "";

   unless (open($fd, "$SPINS_INFO_FILE"))
   {
      print "There is no SP in service.\n";
   }
   else
   {
      while (<$fd>)
      {
         chomp;
         ($tag, $data) = split(/::/);
         unless (!defined($tag))
         {
            unless (!defined($data))
            {
               if ($tag eq "BUNDLE_NAME")
               {
                  $bundle = $data;
               }
               elsif ($tag eq "SPINS_USER")
               {
                  $user = $data;
               }
               elsif ($tag eq "SPINS_DATE")
               {
                  $time = $data;
               }
               elsif ($tag eq "STATUS")
               {
                  $status = $data;
               }
            } ## end unless (!defined($data))
         } ## end unless (!defined($tag))
      } ## end while (<$fd>)

      # Close file
      close($fd);
      print "The following SP is in service: $bundle\n";
      if (not show_SP_type($SPINS_DIR))
      {
         baselog($LOG_ERROR, "Cannot show SP type.");
         return $ERROR;
      }
      print "Put in service by user $user on $time.\n";
      print "$status\n\n";

      if ($params{'l'})
      {
         print "The following patches activity took place when SP $bundle was put in service:\n";
         do_system([$CAT, $SPINS_FILE], {$dsSTDOUT => $TRUE});
         print "\n\n";
      } ## end if ($params{'l'})
   } ## end else

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

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

## @fn     show_SP_type($sp_dir)
# @brief   Show SP type for DELTA and SEARCH SPs
# @param   $sp_dir - directory where is placed SP header.xml
# @return  TRUE  - operation successful
# @return  FALSE - otherwise
# @author  Michael Samokhin
# @date    September 2009
sub show_SP_type($)
{
   my ($sp_dir) = shift @_;

   if (not defined $sp_dir)
   {
      baselog($LOG_ERROR, "SP directory is undefined.");
      return $FALSE;
   }

   my $header_xml = $sp_dir . "/" . $PATCHINFO_FILE;
   if (-e $header_xml)
   {
      my $h_obj = patchutil::header_xml::new($header_xml);
      if (not defined($h_obj))
      {
         baselog($LOG_ERROR, "Cannot create header_xml object", $TRUE);
         return $FALSE;
      }
      if (($h_obj->{'type'} eq 'DELTA_SP') or ($h_obj->{'type'} eq 'SEARCH_SP'))
      {
         my $SP_type = (split(/_/, $h_obj->{'type'}))[0];
         print "It is a $SP_type SP.\n";
      }
      elsif ($h_obj->{'type'} eq 'SP')
      {
         print "It is a STANDARD SP.\n";
      }
      else
      {
         baselog($LOG_ERROR, "Unknown SP type.", $TRUE);
         return $FALSE;
      }
      undef $h_obj;
   } ## end if (-e $header_xml)
   return $TRUE;
} ## end sub show_SP_type($)

