#!/usr/bin/perl
## @file spload
# @brief This is the command to load a Service Pack (bundle of patches and Sus) into the system database.
# @details
# @parm -r determines prompt user for input or perform actions
# by default without any questions.
# @parm -sp ignores reboot (added for consistency with spins).
# @param --help      print this help
#
# @author Ana Alvarez
# @version 1.00 - Creation of spload command to allow load of a Service Pack into the system database

=pod

=head1 NAME

B<spload> - Load a Service Pack (bundle of patches and SUs) into the system database.

=head1 SYNOPSIS

B<spload> I<< <service_pack_file_name>.ntl >>|B<--help>

=head1 DESCRIPTION

Load a Service Pack content (set of Patches and SUs) into system database.
Service Pack content types 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 Service Pack file name is the following:

I<Nortel_Service_Pack_Linux_x.xx_xx.ntl> 

=head1 OPTIONS

=over 4

=item I<< <service_pack_file_name> >>

Load a Service Pack I<< <service_pack_file_name> >> into the system

=item B<--help>

Print this help message and exit.

=back

=head1 EXAMPLES

=over 4

=item spload Nortel_Service_Pack_Linux_6.00_08.ntl

$ B<spload> I<Nortel_Service_Pack_Linux_6.00_08.ntl>
 This command will load all the SUs and patches contained in the SP (Nortel_Service_Pack_Linux_6.00_08.ntl).
 Do you want to proceed? (Y/N) [N]? Y
 Outing patches with out of service status.
 There are no patches in out of service state.
 Cannot find RPM name nortel-cs1000-nrsm installed for SU nortel-cs1000-nrsm-6.00.08.03-00.i386.000.ntl.
 Patch nortel-cs1000-tps-6.00.08.05-00.i386.000 loaded with handle 1
 Cannot find RPM nortel-cs1000-pd-6.00.08-00.i386 associated to patch p28066_1.ntl.
 Patch p28110_1 loaded with handle 2
 Patch p28120_1 loaded with handle 3

 spload command for SP Nortel_Service_Pack_Linux_6.00_08.ntl completed with some errors detected. Please refer to the logs for more details.

B<Note:> It is valid case when the SP is being applied to system where some applications are not installed.
In this there will be an output similar to the output above.

=back 

=head1 SEE ALSO

B<spout>, B<spins> 

=cut

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

use warnings;
use strict;

use common_variables;
use common_functions;
use CommonInstallUtils;
use baselog;
use Getopt::Long;

use patchutil::patching;
use base_semctl;
use patchutil::patches_target;

sub spload_main();

exit(spload_main());
## @fn sub spload_main ()
# @brief Main function of the script
# @details This function is an ecntry point of spload command.
# @param
#   - -help - shows usage
#   - -r - interactive flag to allow prompts for user (optional).
#   - -sp - ignores reboot (added for consistency with spins, optional parameter).
# @return
# One of the return codes defined in target_common.pm:
#    $OK - spload command run successfully
#    $ERROR - spload command failed.
# @author Ana Alvarez
# @date Aug 2008
# @note
sub spload_main()
{
   my $helpflag;
   my $remoteflag;
   my $spflag;
   my %params = ();
   $params{'interact'} = $TRUE;
   $params{'sp'}       = $FALSE;
   my $ret = $OK;

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

   # get parameters entered
   my $res = GetOptions(
              "help"   => \$helpflag,
              "remote" => \$remoteflag,
              "sp"     => \$spflag
             );

   if (!$res)
   {
      print "Incorrect options provided\n";
      print_pod_usage();
      return $ERROR;
   } ## end if (!$res)

   # check input parameters
   if ($helpflag)
   {
      print_pod_usage();
      return $OK;
   }

   #validate option -r
   if ($remoteflag)
   {
      $params{'interact'} = $FALSE;
   }

   # validate option -sp
   if ($spflag)
   {
      $params{'sp'} = $TRUE;
   }

   ## ARGV[0] only has data if there were uncaught parameters.
   ## This includes the service_pack_file_name.
   if (defined($ARGV[0]))
   {
      if (defined $ARGV[1]) ## this is an extra parameter, dump an error
      {
         baselog($LOG_ERROR, "Incorrect number of parameters\n", $TRUE);
         print_pod_usage();
         return $ERROR;
      } ## end if ($ARGV[1])
      if ($ARGV[0] =~ /^[\w\.\+\-]+$/)
      {
         $params{'filename'} = $ARGV[0];  ## assume this is the service_pack_file_name
      } ## end if ($ARGV[0] =~ /^[\w\.\+\-]+$/...
      elsif ($ARGV[0] =~ m/\//)
      {
         baselog($LOG_ERROR, "Patchname can not include path to file.\n",$TRUE);
	 print_pod_usage();
         return $ERROR;
      } ## end ilsif ($ARGV[0] =~ m/\//)
      else                                ## no service_pack_file_name, extra parameter
      {
         baselog($LOG_ERROR, "Incorrect service pack file name provided\n", $TRUE);
         print_pod_usage();
         return $ERROR;
      } ## end else [ if ($ARGV[0] =~ /^[\w\.\+\-]+$/...
   } ## end if (defined($ARGV[0]))
   else                                   # (not $ARGV[0])
   {
      if ($params{'interact'} == $TRUE)   ## Can only prompt for a filename in interactive mode
      {
         $params{'filename'} = get_filename();
         if ($params{'filename'} eq 'q')
         {
            return $OK;
         }
      } ## end if ($params{'interact'...
      else                                ## Non-interactive mode with no filename provided
      {
         baselog($LOG_ERROR, "No service pack file name provided\n", $TRUE);
         print_pod_usage();
         return $ERROR;
      } ## end else [ if ($params{'interact'...
   } ## end else [ if (defined($ARGV[0]))

   my $bundle_full_name = $SRV_PACK_DIR . "/" . $params{'filename'};
   if (!-e $bundle_full_name)
   {
      baselog($LOG_ERROR, "Could not find the SP file $params{'filename'} in $SRV_PACK_DIR.\n", $TRUE);
      return $ERROR;
   }

   # check if SPLOAD dir exists
   if (-d $SPLOAD_DIR)  # spload dir exists
   {
      baselog($LOG_INFO, "There is already an SP loaded in the system. Please use spins or spout command.\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).\n", $TRUE);
      return $ERROR;
   } ## end if (!defined($semmngr)...

   if (!$semmngr->sem_take($SEM_PATCH, IPC_NOWAIT))
   {
      baselog($LOG_ERROR, "Cannot take semaphores. Patching operation aborted\n", $TRUE);
      undef $semmngr;
      return $ERROR;
   } ## end if (!$semmngr->sem_take...

   my $patching = patchutil::patching::new();
   if (not defined($patching))
   {
      baselog($LOG_ERROR, "Cannot create patching object\n", $TRUE);
      $ret = $ERROR;
   }
   else
   {
      if (not $patching->spload(\%params))
      {
         $ret = $ERROR;
      }
      $patching = undef;
   } ## end else [ if (not defined($patching...
   if (!$semmngr->sem_give($SEM_PATCH))
   {
      baselog($LOG_ERROR, "Cannot release semaphores.\n", $TRUE);
      undef $semmngr;
      return $ERROR;

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

   return $ret;
} ## end sub spload_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()
