#!/usr/bin/perl 
## @file plis
# @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.

=pod

=head1 NAME

B<plis> - Show detail information about patch

=head1 SYNOPSIS

B<plis> B<< <patch_handle> >>|B<--help>

=head1 DESCRIPTION

Print detailed information about a specific patch.

=head1 OPTIONS

=over 4

=item B<< <patch_handle> >>

=item B<--help>

Print this message and exit.

=back

=head1 EXAMPLES

$ B<plis 1>
 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

To list information about not existing patch

$ B<plis 6>
 Patch 6 does not exist

=head1 SEE ALSO

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

=cut

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

use warnings;
use strict;
use common_variables;
use common_functions;
use Switch;
use baselog;
use install_common;
use basedb;
use patchutil::patching;
use base_semctl;

sub main();

exit(main());

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

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

   if (scalar(@ARGV) != 1)
   {
      print_pod_usage();
      return $ERROR;
   }
   if ($ARGV[0] =~ /\-{0,2}h(?:-elp)?/)
   {
      print_pod_usage();
      return $OK;
   }

   signals_ignore();
   if ($ARGV[0] =~ /^\d+$/)
   {
      $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 "patch_db" creation failed');
         if (!$semmngr->sem_give($SEM_PATCH))
         {
            baselog($LOG_ERROR, "Cannot release semaphore.");
         }
         undef $semmngr;
         return $ERROR;
      } ## end unless (defined $patches)
      else
      {
         unless ($patches->show_plis($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();
      return $ERROR;
   } ## end else [ if ($ARGV[0] =~ /^\d+$/...

   #remove 'patching' object
   undef $patches;

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

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

