#!/usr/bin/perl
## @file patch_jar
# @brief applies/undoes jar patches
# @details
# @param $ARGV[0] - can be -in/-out. Means action.(in - pins, out - poos)
# @param $ARGV[1] - jar patch file name
# @author Alex Arkhipov
# @author Vlad Ivanov
# @date August 2008
# @version 1.00 - Initial version
# @version 1.01 - patches_public.pm library is used for $TYPE_JAR variable
# @version 1.02 - Add warnings & strict, sub main() is added.
# @note
use lib "/opt/nortel/base/lib";

use strict;
use warnings;

use common_variables;           # base common data
use patchutil::patches_public;  # target/host patch common data
use patchutil::patches_target;  # target patch common data
use jarwarlib;                  # jar/war common data
use base_system;
use baselog;

set_subsystem('Base_Patch');

# The patch name
my $patch;

# The full path to the patch
my $full_patch_name;

# Forward declaration
## @skipline
sub main();
## @skipline
sub pins_jar();
## @skipline
sub poos_jar();
## @skipline
sub check_jar_conflict($);

exit(main());

## @fn sub main($flag, $patch)
# @brief Main entry point
# @param $flag  - Action to perform (-in/-out)
# @param $patch - Jar patch file name
# @return  $OK or $ERROR
# @author  Vladimir Ivanov
# @date September 2008
# @note
sub main()
{
   my $flag = $ARGV[0];

   # Check that flag was specified
   unless (defined $flag)
   {
      baselog($LOG_ERROR, "Missing flag parameter");
      return $ERROR;
   }

   $patch = $ARGV[1];

   # Check that patch name was specified
   unless (defined $patch)
   {
      baselog($LOG_ERROR, "Missing patch name", $TRUE);
      return $ERROR;
   }

   if ($flag ne "-in" && $flag ne "-out")
   {
      baselog($LOG_ERROR, "Incorrect first parameter. Possible values: -in/-out");
      return $ERROR;
   }

   # Variable definition
   unless (set_variables($patch, $TYPE_JAR))
   {
      return $ERROR;
   }

   $full_patch_name = "$my_patch_dir$patch_name";

   baselog($LOG_DEBUG, "patch=$patch");
   baselog($LOG_DEBUG, "my_patch_dir=$my_patch_dir");
   baselog($LOG_DEBUG, "patch_name=$patch_name");
   baselog($LOG_DEBUG, "full_patch_name=$full_patch_name");
   baselog($LOG_DEBUG, "my_tmp_dir=$my_tmp_dir");

   my $retval;
   if ($flag eq "-in")
   {
      $retval = pins_jar();
   }
   else
   {
      $retval = poos_jar();
   }

   cleanup();
   return $retval;
} ## end sub main()

## @fn sub poos_jar()
# @brief This function puts jar patch out-of-service
# @details
# @param none
# @return $OK    - the patch was deactivated successfully
# @return $ERROR - some error occurred
# @author
# @date
sub poos_jar()
{
   baselog($LOG_INFO, "Deactivating patch. Please wait...", $TRUE);

   # Check if the jar patch file exists
   unless (-e $full_patch_name)
   {
      baselog($LOG_WARNING, "Cannot find '$full_patch_name' file");
      baselog($LOG_WARNING, "Probably patch is not in service", $TRUE);
      return $OK;
   } ## end unless (-e $full_patch_name...

   # Delete patch file
   if (unlink($full_patch_name) != 1)
   {
      baselog($LOG_ERROR, "Cannot delete '$full_patch_name' file");
      return $ERROR;
   }
   return $OK;
} ## end sub poos_jar()

## @fn sub pins_jar()
# @brief This function put jar patch in-service
# @details
# @param none
# @return $OK    - the patch was activated successfully
# @return $ERROR - some error occurred
# @author
# @date
sub pins_jar()
{
   baselog($LOG_INFO, "Activating patch. Please wait...", $TRUE);

   # Check if the jar patch file exists
   unless (-e $patch)
   {
      baselog($LOG_ERROR, "Cannot find '$patch' file. Aborting...", $TRUE);
      return $ERROR;
   }

   # Check if patch file is already exists in patch dir
   if (-e $full_patch_name)
   {
      baselog($LOG_INFO, "The '$full_patch_name' already exists.");
      baselog($LOG_ERROR, "Probably patch is already in service.", $TRUE);
      return $ERROR;
   } ## end if (-e $full_patch_name...

   #######################
   # Check for conflicts #
   #######################

   # Create tmp directory (clean if exists).
   if (-d $my_tmp_dir)
   {
      do_system([$RM, '-rf', $my_tmp_dir]);
      baselog($LOG_INFO, "Cleaning '$my_tmp_dir'...");
   }

   baselog($LOG_DEBUG, "Creating '$my_tmp_dir' directory...");
   unless (do_system([$MKDIR, '-p', $my_tmp_dir]))
   {
      baselog($LOG_ERROR, "Cannot create '$my_tmp_dir' directory");
      return $ERROR;
   }

   # Copy patch file to tmp dir
   baselog($LOG_INFO, "Copy '$patch' to '$my_tmp_dir'");
   unless (do_system([$CP, $patch, $my_tmp_dir]))
   {
      baselog($LOG_ERROR, "Cannot copy the '$patch' file", $TRUE);
      return $ERROR;
   }

   # Extract jar patch to <tmp_dir>/jar/<patch_name>/ folder
   baselog($LOG_DEBUG, "Extracting patch archive...");
   chdir $my_tmp_dir;
   unless (do_system("$jar -xf $patch_name $files_to_extract"))
   {
      baselog($LOG_ERROR, "Cannot extract patch archive");
      return $ERROR;
   }

   my $full_mod_file = $my_tmp_dir . $mod_file;
   my $full_add_file = $my_tmp_dir . $add_file;
   my ($list, $list1, $list2);

   unless (get_files($full_mod_file, $list1) and get_files($full_add_file, $list2))
   {
      baselog($LOG_ERROR, "Cannot get file list");
      baselog($LOG_ERROR, "Patch member is incorrect", $TRUE);
      return $ERROR;
   } ## end unless (get_files($full_mod_file...

   $list = $list1 . $list2;
   baselog($LOG_DEBUG, "list=$list1$list2");

   # Check if jar patch directory exists
   # If doesn't then create it
   unless (-d $my_patch_dir)
   {
      baselog($LOG_DEBUG, "Creating '$my_patch_dir' directory...");
      unless (do_system([$MKDIR, '-p', $my_patch_dir]))
      {
         baselog($LOG_ERROR, "Cannot create '$my_patch_dir' directory");
         return $ERROR;
      }
   } ## end unless (-d $my_patch_dir)

   # Check conflicts
   unless (check_jar_conflict($list))
   {
      baselog($LOG_ERROR, "Conflict checking fail...", $TRUE);
      return $ERROR;
   }

   # Copy jar patch file into the jar patch dir
   baselog($LOG_DEBUG, "$CP $patch_name $full_patch_name");
   unless (do_system([$CP, $patch, $full_patch_name]))
   {
      baselog($LOG_ERROR, "Cannot copy '$patch' file");
      return $ERROR;
   }
   return $OK;
} ## end sub pins_jar()

## @fn sub check_jar_conflict($filelist)
# @brief This function checks jar patch for conflicts
# @details
# @param $filelist - filelist to compare (both modified and added files)
# @return $TRUE  - no conflicts
# @return $FALSE - there is(are) conflict(s)
# @author
# @date
sub check_jar_conflict($)
{
   my $filelist = shift @_;
   my $dir      = $my_patch_dir;
   my ($patchname, $tmp_file, $tmp_dir);

   my $ret = $FALSE;
   unless (opendir(Dir, $dir))
   {
      baselog($LOG_ERROR, "Cannot open '$dir' directory");
      return $ret;
   }

   while (my $file = readdir(Dir))
   {

      # find all patches
      if (($file =~ /\.jar$/i))
      {
         $patchname = "$dir$file";

         # check conflict for particular patch
         $tmp_dir = $TMP_DIR . "$TYPE_JAR/tmp/$file/";
         unless (do_system([$MKDIR, '-p', $tmp_dir]))
         {
            baselog($LOG_ERROR, "Cannot create '$tmp_dir' directory");
            goto end;
         }
         chdir $tmp_dir;

         # Extract modified-files and added-files
         baselog($LOG_DEBUG, "jar -xf $patchname $files_to_extract");
         unless (do_system("$jar -xf $patchname $files_to_extract"))
         {
            baselog($LOG_ERROR, "Cannot extract '$patchname' patch");
            do_system([$RM, '-rf', $tmp_dir]);
            goto end;
         }

         # Check modified files for conflicts
         $tmp_file = $tmp_dir . $mod_file;
         if (is_conflict($tmp_file, $filelist))
         {
            baselog($LOG_ERROR, "The conflict with the '$file' patch was detected", $TRUE);
            do_system([$RM, '-rf', $tmp_dir]);
            goto end;
         }

         # Check added files for conflicts
         $tmp_file = $tmp_dir . $add_file;
         if (is_conflict($tmp_file, $filelist))
         {
            baselog($LOG_ERROR, "The conflict with the '$file' patch was detected", $TRUE);
            do_system([$RM, '-rf', $tmp_dir]);
            goto end;
         }

         # Cleanup the tmp directory
         do_system([$RM, '-rf', $tmp_dir]);
      } ## end if (($file =~ /\.jar$/i...
   }  # while ($file = readdir(Dir))
   $ret = $TRUE;
 end:
   closedir(Dir);
   return $ret;
} ## end sub check_jar_conflict($)

