#!/usr/bin/perl 

=pod

=head1 NAME

B<pload> - Load the patch into the system database

=head1 SYNOPSIS

B<pload> [B<--all>]|I<< <patch_name>.ntl >>|B<--help>

=head1 DESCRIPTION

Load a patch file and update the on-switch database with the specific patch information.
Patch type includes the following:

=over 4

=item 1

PATCH - it can be one of the following: JAR, WAR, BIN, FRU (file replacement unit).

=item 2

SU - Serviceability Update.

=back

The format of patch name is the following:

I<pxxxxx_x.ntl> or I<< <any_SU_name>.ntl >>

=head1 OPTIONS

=over 4

=item I<< <patch_file> >>

Load the patch I<< <patch_file> >> from the patch directory

=item B<--all>

Load all patches from the patch directory

=item B<--help>

Print this help message and exit.

=back

=head1 EXAMPLES

=over 4

=item load p20002_1.ntl patch 

$ B<pload> I<p20002_1.ntl>
 Loading patch /var/opt/nortel/patch/p20002_1.ntl
 Patch handle is: 1

=item Load all patches

$ B<pload> B<--all>
 Loading patch /var/opt/nortel/patch/p19000_1.ntl
 Patch handle is: 0

 Loading patch /var/opt/nortel/patch/p20002_1.ntl
 Patch handle is: 1
 ...

=back 

=head1 CAVEATS

The /var/opt/nortel/patch directory must contain the patch file to be loaded.

=head1 SEE ALSO

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

=cut

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

use warnings;
use strict;

use common_variables;
use common_functions;
use baselog;

use patchutil::patching;
use base_semctl;
use CommonInstallUtils;

use Getopt::Long;
use patchutil::patches_target;

sub get_filename();  #get patch filename
sub pload_main();    #entry point

exit(pload_main());

sub pload_main()
{
   if (warning_if_not_nortel_grp())
   {
      return $ERROR;
   }
   set_subsystem('Base_Patch');

   my @arg      = @ARGV;
   my $args     = scalar(@arg);
   my $interact = $TRUE;
   my $all      = $FALSE;
   my $spack    = $FALSE;
   my $helpflag;
   my $remoteflag;
   my $allflag;
   my $spflag;

   my $res = GetOptions(
              "help"   => \$helpflag,
              "remote" => \$remoteflag,
              "sp"     => \$spflag,
              "all"    => \$allflag
             );
   if (!$res)
   {
      print "Incorrect options provided\n";
      print_pod_usage();
      return $ERROR;
   } ## end if (!$res)

   if ($helpflag)
   {
      print_pod_usage();
      return $OK;
   }

   my %params = ();

   # Handle -r or --remote option
   if ($remoteflag)
   {
      $args--;
      if (not $args)
      {
         baselog($LOG_ERROR, "Insufficient number of arguments.\n", $TRUE);
         print_pod_usage();
         return $ERROR;
      }
      $interact = $FALSE;
   } ## end if ($remoteflag)

   # Handle -s or --sp option
   if ($spflag)
   {
      $args--;
      if (not $args)
      {
         baselog($LOG_ERROR, "Insufficient number of arguments.", $TRUE);
         print_pod_usage();
         return $ERROR;
      }
      $spack = $TRUE;
      $interact = $FALSE;
   } ## end if ($spflag)

   if ($allflag)
   {
      $all = $TRUE;
   }
   elsif (not $ARGV[0])
   {
      $params{'filename'} = get_filename();
      if ($params{'filename'} eq 'q')
      {
         return $OK;
      }
   } ## end elsif (not $ARGV[0])
   else
   {
      if ($ARGV[0] =~ /^[\w\.\+\-]+$/)
      {
         $params{'filename'} = $ARGV[0];
      }
      elsif ($ARGV[0] =~ m/\//)
      {
         baselog($LOG_ERROR,"Patchname can not include path to file.\n",$TRUE);
         return $ERROR;
      }
      else
      {
         print "Error: Invalid patch name provided.\n";
         return $ERROR;
      }
   } ## end else [ if ($allflag)

   $params{'interact'} = $interact;
   $params{'all'}      = $all;
   $params{'sp'}       = $spack;

   if (not $all)
   {
      my $file_full_name = $PATCHDIR . "/" . $params{'filename'};
      if (!-e $file_full_name)
      {
         baselog($LOG_ERROR, "Could not find the patch file $params{'filename'} in $PATCHDIR.\n", $TRUE);
         return $ERROR;
      }
   }
   # make sure that if there is pending SP command, pload is not allowed
   if (-d $SPLOAD_DIR)
   {
      baselog($LOG_INFO, "SP in progress. Please use spins or spout command before executing pload.\n", $TRUE);
      return $OK;
   }
   signals_ignore();
   my $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 semaphores.");
      baselog($LOG_ERROR, "patching operation aborted.\n", $TRUE);
      undef $semmngr;
      return $ERROR;
   } ## end if (!$semmngr->sem_take...

   my $retval   = $OK;
   my $patching = patchutil::patching::new();
   if (not defined($patching))
   {
      baselog($LOG_ERROR, 'Cannot create patching object');
      $retval = $ERROR;
   }
   else
   {
      if (not $patching->pload(\%params))
      {
         baselog($LOG_ERROR, 'Patch loading failed.');
         $retval = $ERROR;
      }
      $patching = undef;
   } ## end else [ if (not defined($patching...
   if (!$semmngr->sem_give($SEM_PATCH))
   {
      baselog($LOG_ERROR, "Cannot release semaphores.");
      undef $semmngr;
      return $ERROR;

   } ## end if (!$semmngr->sem_give...
   undef $semmngr;

   return $retval;
} ## end sub pload_main()

# Prompt user enter patch name
sub get_filename()
{
   my ($filename);

   $filename = getTextResponse("Enter patch filename (without path) or 'q' to exit", 'q');
   while (($filename !~ /^[\w\.\+\-]+$/) and ($filename ne 'q'))
   {
      baselog($LOG_ERROR, "Invalid file name provided.\n", $TRUE);
      $filename = getTextResponse("Enter patch filename (without path) or 'q' to exit", 'q');
   }
   return $filename;

} ## end sub get_filename()

sub cleanup()
{
   return;
}
