#!/usr/bin/perl

=pod

=head1 NAME

B<pins> - Put a patch into service.

=head1 SYNOPSIS

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

=head1 DESCRIPTION

Put a patch into service; 
the patch will be placed into service for all processes to which it applies.

=head1 OPTIONS

=over 4

=item B<< <patch_id> >>

Activate patch with B<< <patch_id> >> handle (as identified by B<pload> or B<pstat> commands).

=item B<--all>

Activate all loaded patches.

=item B<--help>

Print this help message and exit.

=back

=head1 EXAMPLES

=over 4

=item Activate patch with 4 handle (SU example).

$ B<pins 4>
 Patch handle: 4

 All Nortel applications will be stopped.
 Do you want to continue (Y/N) [N]? y
 ...
 
=item Activate all loaded patches

$ B<pins --all>
 Patch handle: 2
 Please ensure that the application solid is stopped before proceeding patch installation.
 Do you want to continue patch installation? (Y/N) [N]? y

 Performing the installation:
 Name        : nortel-cs1000-solid          Relocations: (not relocatable)
 Version     : 5.25.06                           Vendor: (none)
 Release     : 00.4.50.0090                  Build Date: Fri 12 Oct 2007 05:43:58 PM MSD
 Install Date: (not installed)               Build Host: zbvwh0ja.ca.nortel.com
 Group       : Application Software          Source RPM: nortel-cs1000-solid-5.25.06-00.4.50.0090.src.rpm
 Size        : 17825125                         License: Commercial
 Signature   : (none)
 URL         : www.nortel.com
 Summary     : change summary
 Description :
 Solid database server boost engine distributed with nortel CS1000 product.
 Do you want to continue? (Y/N) [Y]?
 Performing new RPM patch installation...
 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 installation is completed.
 The patch 2 has been activated successfully.
 Patch handle: 1

 Performing the installation:
 The patch 1 has been activated successfully.
 Patch handle: 0
 Performing the installation:
 Warning: installing a new file. Please pay attention to the /etc/test1 file attributes
 The patch 0 has been activated successfully.
 Patch handle: 3

 All Nortel applications will be stopped.
 Do you want to continue (Y/N) [N]? y
 ...

=back

=head1 CAVEATS

The patch should be loaded before putting into service.
The patch cannot be put in service, if it has a conflict with another one.
The patch conflict checking includes: application checking and patch element checking.

=head1 SEE ALSO

B<pload>, B<pout>, 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;

my $ssh_hang_on_exit_bug_flag ;         # NEEDED BY END FUNCTION

sub pins_main();  #entry point

exit(pins_main());

sub pins_main()
{
   my $interact = $TRUE;
   my $spack    = $FALSE;
   my $all      = $FALSE;
   my $helpflag;
   my $remoteflag;
   my $allflag;
   my $patchid;
   my $spflag;

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

   my $res = GetOptions(
              "help"   => \$helpflag,
              "remote" => \$remoteflag,
              "sp"     => \$spflag,
              "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 (defined $ARGV[0] and $ARGV[0] =~ /^\d+$/)  ## ARGV[0] only has data if there were uncaught parameters. Check for patch id.
   {
      $patchid = $ARGV[0];
      if (defined $ARGV[1])                       ## extra uncaught parameter
      {
         print_pod_usage();
         return $ERROR;
      }
   } ## end if (defined $ARGV[0] and...
   elsif ($ARGV[0])                               ## ARGV[0] only has data if there were uncaught parameters. No patch id in this case.
   {
      print_pod_usage();
      return $ERROR;
   }
   elsif ($helpflag)
   {
      print_pod_usage();
      return $OK;
   }

   my %params = ();

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

   if ($spflag)
   {
      $spack = $TRUE;
      $interact = $FALSE;
   }

   if ($allflag)
   {
      $all = $TRUE;
   }
   elsif (defined $patchid)
   {
      $params{'pid'} = $patchid;
   }
   else
   {
      baselog($LOG_ERROR, "Insufficient number of arguments.", $TRUE);
      print_pod_usage();
      return $ERROR;
   }

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

   # make sure that if there is pending SP command, pins is not allowed
   if (-d $SPLOAD_DIR)
   {
      baselog($LOG_INFO, "SP in progress. Please use spins or spout command before executing pins.\n", $TRUE);
      return $OK;
   }
   my $retval = $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).", $TRUE);
      return $ERROR;
   } ## end if (!defined($semmngr)...
   my @sem_list = ($SEM_APPSTART, $SEM_HARD);

   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...

   if (!$semmngr->sem_take(\@sem_list, $WAIT_FLAG, $PINS_SEM_WAITTIME))
   {
      baselog($LOG_ERROR, "Cannot take semaphores. Patching operation aborted. Retry command.\n", $TRUE);
      if (!$semmngr->sem_give($SEM_PATCH))
      {
         baselog($LOG_ERROR, "Cannot release semaphores.");
      }
      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, aborting.", $TRUE);
      $retval = $ERROR;
   }
   else
   {
      if (not $patching->pins(\%params))
      {
         $retval = $ERROR;
      }
      $patching = undef;
   } ## end else [ if (not defined($patching...

   if (!$semmngr->sem_give(\@sem_list))
   {
      baselog($LOG_ERROR, "Cannot release semaphores.");
      undef $semmngr;
      return $ERROR;
   } ## end if (!$semmngr->sem_give...
   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 pins_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
 
