#!/usr/bin/perl

=pod

=head1 NAME

B<poos> - Put a patch out of service.

=head1 SYNOPSIS

B<poos> B<< <patch_id> >>|B<--all> >>|B<--help,-h>

=head1 DESCRIPTION

Remove a patch from service. 
The patch is removed from service from all processes in which it was in service.

=head1 OPTIONS

=over 4

=item B<< <patch_id> >>

Deactivate patch with B<< <patch_id> >> handle.

=item B<--all>

Deactivate all patches.

=item B<--help>

Print this help message and exit.

=back

=head1 EXAMPLES

=over 4

=item Deactivate patch with 2 handle

$ B<poos 2>
 Patch handle: 2
 Please ensure that the application solid is stopped before proceeding patch un-installation.
 Do you want to continue patch un-installation? (Y/N) [N]? y
 Performing the uninstallation:
 Performing uninstall RPM patch...
 Preparing...                ########################################### [100%]
    1:nortel-cs1000-solid    ########################################### [100%]
 executing Solid DB post install...
 Installation nortel Solid database server completed.
 Unstalling the Solid database server package done

 Done.
 The RPM patch uninstallation is completed.
 The patch 2 has been deactivated successfully.

=item Deactivate all patches

$ B<poos --all>
 Patch handle: 0
 Performing the uninstallation:
 The patch 0 has been deactivated successfully
 Patch handle: 1
 Performing the uninstallation:
 The patch 1 has been deactivated successfully
 Patch handle: 2
 Performing the uninstallation:
 The patch 2 has been deactivated successfully             

=back

=head1 SEE ALSO

B<pload>, B<pout>, B<pins>, 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 poos_main();  #entry point

my $ssh_hang_on_exit_bug_flag ;         # NEEDED BY END FUNCTION

exit(poos_main());

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

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

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

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

   if (not $args)
   {
      print_pod_usage();
      return $ERROR;
   }
   elsif ($helpflag)
   {
      print_pod_usage();
      return $OK;
   }

   my %params = ();

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

   if ($ssh_hang_on_exit_bug_flag)
   {
      $args--;
      $interact = $FALSE;
   }

   if ($allflag)
   {
      $all = $TRUE;
   }
   elsif (($args == 2) and ($appflag))
   {
      $params{'app'} = $ARGV[0];  ## $ARGV[0] contains uncaught arguments
   }
   elsif (($args == 1) and ($appflag))
   {
      baselog($LOG_ERROR, "Insufficient number of arguments.", $TRUE);
      print_pod_usage();
      return $ERROR;
   }
   elsif (($args == 1) and ($ARGV[0] =~ /^\d+$/))
   {
      $params{'pid'} = $ARGV[0];  ## $ARGV[0] contains uncaught arguments
   }
   else
   {
      baselog($LOG_ERROR, "Insufficient number of arguments.", $TRUE);
      print_pod_usage();
      return $ERROR;
   } ## end else [ if ($allflag)

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

   # make sure that if there is pending SP command, poos is not allowed
   if (-d $SPLOAD_DIR)
   {
      baselog($LOG_INFO, "SP in progress. Please use spins or spout command before executing poos.\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)...
   my @sem_list = ($SEM_PATCH, $SEM_APPSTART, $SEM_HARD);

   if (!$semmngr->sem_take(\@sem_list, 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 $patching = patchutil::patching::new();
   if (not defined($patching))
   {
      baselog($LOG_ERROR, 'Cannot create patching object');
      $retval = $ERROR;
   }
   else
   {
      if (not $patching->poos(\%params))
      {
         baselog($LOG_ERROR, "Patch deactivation failed.");
         $retval = $ERROR;
      }
      $patching = undef;
   } ## end else [ if (not defined($patching...

   #release semaphore
   if (!$semmngr->sem_give(\@sem_list))
   {
      baselog($LOG_ERROR, "Cannot release semaphores.");
      undef $semmngr;
      return $ERROR;

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

   undef $semmngr;
   return $retval;
} ## end sub poos_main()

sub cleanup()
{

}

END
{   # WORKAROUND - YUK!
    #
    #   Due to a OpenSSH hang-on-exit issue, if we are being called remotely we
    #   will create and call a script to kill our parent "sshd: nortel@notty"
    #   to terminate the ssh session on this side of a remote ssh reguest
    #   session
    #
    if ($ssh_hang_on_exit_bug_flag)
    {
        # Save original return value
        my$retVal = $? ;

        if( ssh_hang_on_exit_bug() == $ERROR )
        {
           return $ERROR;
        }

        baselog($LOG_INFO,"I AM OUTA HERE");

        # Return the original return value since "ssh_hang_on_exit_bug" would
        # have changed the result.
        return $retVal;
    }
}   # END

