#!/usr/bin/perl 

=pod

=head1 NAME

B<pout> - Unload the patch from the system database

=head1 SYNOPSIS

B<pout> B<< <patch_id> >>|B<--all>|B<--help>

=head1 DESCRIPTION

Unload a patch that was loaded with the pload command and cleanup the on-switch database of the specific patch information.

=head1 OPTIONS

=over 4

=item B<< <patch_id> >>

Unload patch with B<< <patch_id> >> handle

=item B<--all>

Unload all out of service patches

=item B<--help>

Print this help message and exit

=back

=head1 EXAMPLES

=over 4

=item Unload the patch with 0 handle

$ B<pout 0>
 Patch 0 has been removed successfully

=item Unload the patch with not existing handle

$ B<pout 5>
 Patch 5 does not exist

=item Unload all patches

$ B<pout --all>
 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 SEE ALSO

B<pload>, 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 Getopt::Long;
use patchutil::patches_target;

sub pout_main();  #entry point

sub get_handle(); #get patch handle from user

exit(pout_main());

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

   my ($handle, $count);
   my $interact = $TRUE;
   my $all      = $FALSE;
   my $spack    = $FALSE;
   my @arg      = @ARGV;
   my $args     = scalar(@arg);
   my %params   = ();

   my $helpflag;
   my $remoteflag;
   my $allflag;
   my $appflag;
   my $spflag;

   my $res = GetOptions(
              "help"   => \$helpflag,
              "remote" => \$remoteflag,
              "sp"     => \$spflag,
              "app"    => \$appflag,
              "all"    => \$allflag
             );

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

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

   # Handle -r or --remote option
   if ($remoteflag)
   {
      $args--;
      $interact = $FALSE;
   }

   # Handle -s or --sp option
   if ($spflag)
   {
      $args--;
      $spack = $TRUE;
      $interact = $FALSE;  # if we're running in --sp mode, we are non-interactive
   }

   if (defined $ARGV[0])
   {
      if (not $appflag)

      {
         $handle = $ARGV[0];
         unless ($handle =~ /^\d+$/)
         {
            print "Error: Patch handle must be numeric.\n";
            return $ERROR;
         }
         $params{'pid'} = $handle;
      } ## end if (not $appflag)
      else
      {
         $params{'app'} = $ARGV[0];
      }
   } ## end if (defined $ARGV[0])
   elsif ($allflag)
   {
      $all = $TRUE;
   }
   elsif ($appflag and !$ARGV[0])
   {
      baselog($LOG_ERROR, "Insufficient number of arguments.", $TRUE);
      print_pod_usage();
      return $ERROR;
   }
   else
   {
      if (not $interact)
      {
         baselog($LOG_ERROR, "Insufficient number of arguments.", $TRUE);
         print_pod_usage();
         return $ERROR;
      }
      $handle = get_handle();   # ask user parameters
      $params{'pid'} = $handle;
   } ## end else [ if (defined $ARGV[0])

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

   # make sure that if there is pending SP command, pout is not allowed
   if (-d $SPLOAD_DIR)
   {
      baselog($LOG_INFO, "SP in progress. Please use spins or spout command before executing pout.\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->pout(\%params))
      {
         baselog($LOG_ERROR, 'Pout operation failed');
         $retval = $ERROR;
      }
      $patching = undef;
   } ## end else [ if (not defined($patching...

   # release semaphore
   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 pout_main()

sub get_handle()
{
   my ($handle);

   while ($TRUE)
   {
      print "Patch handle?";
      chomp($handle = <STDIN>);
      if ($handle =~ /^\d+$/)
      {
         last;
      }
      else
      {
         print "Handle must be a number\n";
         next;
      }
   } ## end while ($TRUE)
   return $handle;
} ## end sub get_handle()

sub cleanup()
{
   return;
}

