#!/usr/bin/perl
## @file spout
# @brief This command unloads a service pack that has never been put into service.
# @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 Dave Cairns
# @version 1.00 - Initial version
# @version 1.01 - Added call to final spout

=pod

=head1 NAME

B<spout> - Unload a Service Pack (bundle of patches and SUs) from the system database

=head1 SYNOPSIS

B<spout> [B<--help>]

=head1 DESCRIPTION

Unload a Service Pack (set of Patches and Sus) from the system database

=head1 OPTIONS

=over 4

=item B<--help>

Print this help message and exit.

=back

=head1 EXAMPLES

=over 4

=item spout 

$ B<spout> 
 Patch 0 has been removed successfully
 Patch 1 has been removed successfully
 Patch 2 has been removed successfully
 Patch 3 has been removed successfully
 ...

=back 

=head1 CAVEATS

spload must have been successfully run previously for this command to work
spins cannot have been run on this service pack

=head1 SEE ALSO

B<spload>, B<spins>

=cut

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

use warnings;
use strict;

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

use patchutil::patching;
use base_semctl;

sub spout_main(); 

exit(spout_main());
## @fn sub spout_main ()
# @brief Main function of the script
# @details This function is an entry point of spout command.
# @param
#   - -help - shows usage
#   - -r - interactive flag to allow prompts for user (optional).
#   - -sp - ignores reboot (added for consistency with spins optional).
# @return
# One of the return codes defined in target_common.pm:
#    $OK - spout command run successfully
#    $ERROR - spout command failed.
# @author Dave Cairns
# @date Oct 2008
# @note
sub spout_main()
{ 
   my $helpflag;
   my $remoteflag;
   my $spflag;
   my $ret_val  = $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)
   
   if ($helpflag)
   {
      print_pod_usage();
      return $OK;
   }

## ARGV[0] only has data if there were uncaught parameters.
   if (defined($ARGV[0]))
   { 
      baselog($LOG_ERROR, "Invalid parameters entered\n", $TRUE); 
      print_pod_usage();
      return $ERROR;
   }

   my %params = ();
   $params{'interact'} = $TRUE;
   $params{'sp'} = $FALSE;        

   # check input parameters
   #validate option -r
   if ($remoteflag)
   {
      $params{'interact'} = $FALSE;
   }
      
   # validate option -sp
   if ($spflag)
   {
      $params{'sp'} = $TRUE;
   }

   ::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_val  =  $ERROR;
   }
   else
   {
      if (not $patching->spout(\%params))
      {
         baselog($LOG_ERROR, "Failed to unload Service Pack.\n", $TRUE);
         $ret_val  =  $ERROR;
      }
      undef $patching;
   } ## end else [ if (not defined($patching...

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

   return $ret_val;
} ## end sub spout_main()



