#!/usr/bin/perl
## @file patch_war
# @brief applies/undoes war patches
# @details
# @param $ARGV[0] - can be -in/-out. Means action.(in - pins, out - poos)
# @param $ARGV[1] - war patch file name
# @author Alex Arkhipov, Vlad Ivanov, Alex Kolotov
# @version 1.00 - Initial version
# @version 2.00 - Deletion files from the WAR file was added
# @version 2.01 - Two colons before shared functions and variables were removed
# @version 2.02 - Add warnings & strict, sub main() is added.
# @version 2.03 - Add "-o" option to UNZIP
# @version 2.04 - modified remove_files_from_archive() to not fail if file to
#                 remove already does not exist
# @version 2.05 - function main() has been modified to change created files
#                 permissions (script has been changed to work under sudo)
# @version 2.06 - ZIP and UNZIP commands invocations changed from command-in-string
#                 form to command-in-array form. WAR updating now happens for a
#                 temporary files in order to guarantee the archive integrity.

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

use strict;
use warnings;

use common_variables;           # base common data
use common_functions;
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');

# Needed for debug purposes
my $verbose_mode = $FALSE;
my @quiteZIP     = ($ZIP);
my @quiteUNZIP   = ($UNZIP);
if (not $verbose_mode)
{
   push @quiteZIP,   "-qq";
   push @quiteUNZIP, "-qq";
}

# Special return code of ZIP command when a file which is being removed from an
# archive does not exist in this archive
my $ZIP_special_rc = "12";

# Available operation
# -in  --- put in service
# -out --- put out of service
my $WAR_OP_IN  = "-in";
my $WAR_OP_OUT = "-out";

# The patch name
my $patch;

# The full path to the patch
my $full_patch_name;

# The directory to work with the current patch
my $war_patch_dir;

# The directory to work with the old patch
my $old_patch_dir;

# The full path to the patch in the tmp directory
my $tmp_patch_name;

# The path to the war configuration file
my $warfile;

# Forward declaration
## @skipline
sub main();
## @skipline
sub pins_war();
## @skipline
sub poos_war();
## @skipline
sub getwarpath($$);
## @skipline
sub get_war_file($$);
## @skipline
sub create_dir($);
## @skipline
sub trim($);
## @skipline
sub check_war_conflict($$);
## @skipline
sub save_files_from_archive($$$);
## @skipline
sub update_war_file($$$$$;$);
## @skipline
sub update_files_in_archive($$;$);
## @skipline
sub remove_files_from_archive($$);

exit(main());

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

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

   $patch = $ARGV[1];
   unless (defined $patch)
   {
      baselog($LOG_ERROR, "Missing patch name");
      return $ERROR;
   }

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

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

   $war_patch_dir   = $my_patch_dir . "$patch_name/";
   $old_patch_dir   = "${war_patch_dir}old";
   $full_patch_name = "$war_patch_dir$patch_name";
   $tmp_patch_name  = "$my_tmp_dir$patch_name";
   $warfile         = "";

   baselog($LOG_DEBUG, "patch=$patch");
   baselog($LOG_DEBUG, "patch_dir=$PATCHDIR");
   baselog($LOG_DEBUG, "full_patch_name=$full_patch_name");
   baselog($LOG_DEBUG, "my_tmp_dir=$my_tmp_dir");
   baselog($LOG_DEBUG, "tmp_patch_name=$tmp_patch_name");

   my $retval;
   if ($flag eq $WAR_OP_IN)
   {
      $retval = pins_war();
   }
   else
   {
      $retval = poos_war();
   }
   unless (do_system([$CHMOD, '-R', '770', $my_patch_dir]))
   {
      baselog($LOG_ERROR, "Failed to chmod 770 $my_patch_dir folder.");
      $retval = $ERROR;
   }
   unless (do_system([$CHOWN, '-R', "$NORTEL_ACCT:$GROUPS{'patchadmin'}", $my_patch_dir]))
   {
      baselog($LOG_ERROR, "Failed to chown $NORTEL_ACCT:$GROUPS{'patchadmin'} $my_patch_dir folder.");
      $retval = $ERROR;
   }
   cleanup();
   return $retval;
} ## end sub main()

## @fn sub poos_war()
# @brief Puts the war patch out-of-service
# @details Restores the original state of the war file (before patching)
# @param none
# @return $OK    - the patch was deactivated successfully
# @return $ERROR - some error occurred
sub poos_war()
{
   baselog($LOG_INFO, "Deactivating patch. Please wait...", $TRUE);
   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...

   baselog($LOG_INFO, "Inspecting content of the patch...", $TRUE);

   # Get war file path from <war_patch_dir>/META-INF/patch-info file
   my $pinfo = $war_patch_dir . $pinfo_file;

   unless (get_war_file($pinfo, $warfile))
   {
      return $ERROR;
   }

   my $full_mod_file = $war_patch_dir . $mod_file;
   my $full_add_file = $war_patch_dir . $add_file;
   my $full_del_file = $war_patch_dir . $del_file;

   my $add_list = get_files_to_array($full_add_file);
   if (not defined($add_list))
   {
      baselog($LOG_ERROR, "Cannot get a list of files added.");
      baselog($LOG_ERROR, "Patch member is incorrect", $TRUE);
      return $ERROR;
   } ## end if (not defined($add_list...
   baselog($LOG_DEBUG, "add_list contains " . scalar(@{$add_list}) . " element(s)");

   my $mod_list = get_files_to_array($full_mod_file);
   if (not defined($mod_list))
   {
      baselog($LOG_ERROR, "Cannot get a list of files modified.");
      baselog($LOG_ERROR, "Patch member is incorrect", $TRUE);
      return $ERROR;
   } ## end if (not defined($mod_list...
   baselog($LOG_DEBUG, "mod_list contains " . scalar(@{$mod_list}) . " element(s)");

   my $del_list = get_files_to_array($full_del_file);
   if (not defined($del_list))
   {
      baselog($LOG_ERROR, "Cannot get a list of files deleted.");
      baselog($LOG_ERROR, "Patch member is incorrect", $TRUE);
      return $ERROR;
   } ## end if (not defined($del_list...
   baselog($LOG_DEBUG, "del_list contains " . scalar(@{$del_list}) . " element(s)");

   # Update war archive
   if (not update_war_file($WAR_OP_OUT, $warfile, $add_list, $mod_list, $del_list, $old_patch_dir))
   {
      return $ERROR;
   }

   # Remove the <war_patch_dir> directory
   do_system([$RM, '-rf', $war_patch_dir]);
   baselog($LOG_INFO, "Cleaning '$war_patch_dir'...");

   # Return OK
   return $OK;
} ## end sub poos_war()

## @fn sub pins_war()
# @brief Puts the war patch in-service
# @details Update the war file with files from the patch; saves old and
#          deleted files in the patch directory
# @param none
# @return $OK    - the patch was activated successfully
# @return $ERROR - some error occurred
sub pins_war()
{
   baselog($LOG_INFO, "Activating patch. Please wait...", $TRUE);

   #Check that $patch file exists
   unless (-e $patch)
   {
      baselog($LOG_ERROR, "No '$patch' file");
      return $ERROR;
   }

   # Check if this file is already exists then exit
   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 $OK;
   } ## end if (-e $full_patch_name...

   unless (create_dir($my_tmp_dir))
   {
      return $ERROR;
   }

   # Copy patch file to tmp dir
   baselog($LOG_INFO, "Preparing the patch '$patch_name' for activation...", $TRUE);

   baselog($LOG_DEBUG, "Copy '$patch' to '$my_tmp_dir'");
   unless (do_system([$CP, $patch, $my_tmp_dir]))
   {
      baselog($LOG_ERROR, "Cannot copy the '$patch_name' file");
      return $ERROR;
   }

   # Extract war patch (jar file) to /tmp/war/<patch_name>/ folder
   baselog($LOG_INFO, "Inspecting content of the patch...", $TRUE);

   chdir $my_tmp_dir;
   unless (do_system([$jar, '-xf', $patch_name]))
   {
      baselog($LOG_ERROR, "Cannot extract patch archive", $TRUE);
      return $ERROR;
   }

   # Get war file path from <my_tmp_dir>/war/<patch_name>/META-INF/patch-info file
   my $pinfo = $my_tmp_dir . $pinfo_file;
   unless (get_war_file($pinfo, $warfile))
   {
      return $ERROR;
   }

   my $full_mod_file = $my_tmp_dir . $mod_file;
   my $full_add_file = $my_tmp_dir . $add_file;
   my $full_del_file = $my_tmp_dir . $del_file;

   my $add_list = get_files_to_array($full_add_file);
   if (not defined($add_list))
   {
      baselog($LOG_ERROR, "Cannot get a list of files added.");
      baselog($LOG_ERROR, "Patch member is incorrect", $TRUE);
      return $ERROR;
   } ## end if (not defined($add_list...
   baselog($LOG_DEBUG, "add_list contains " . scalar(@{$add_list}) . " element(s)");

   my $mod_list = get_files_to_array($full_mod_file);
   if (not defined($mod_list))
   {
      baselog($LOG_ERROR, "Cannot get a list of files modified.");
      baselog($LOG_ERROR, "Patch member is incorrect", $TRUE);
      return $ERROR;
   } ## end if (not defined($mod_list...
   baselog($LOG_DEBUG, "mod_list contains " . scalar(@{$mod_list}) . " element(s)");

   my $del_list = get_files_to_array($full_del_file);
   if (not defined($del_list))
   {
      baselog($LOG_ERROR, "Cannot get a list of files deleted.");
      baselog($LOG_ERROR, "Patch member is incorrect", $TRUE);
      return $ERROR;
   } ## end if (not defined($del_list...
   baselog($LOG_DEBUG, "del_list contains " . scalar(@{$del_list}) . " element(s)");

   unless (create_dir($war_patch_dir))
   {
      return $ERROR;
   }

   # Check conflicts
   unless (check_war_conflict($warfile, [@{$add_list}, @{$mod_list}, @{$del_list}]))
   {
      baselog($LOG_ERROR, "Conflict checking failed... ", $TRUE);
      print "Abort activation";
      do_system([$RM, '-rf', $war_patch_dir]);
      return $ERROR;
   } ## end unless (check_war_conflict...

   # START ACTIVATION

   # Copy war patch (jar file)
   # to the <patch_dir>/war/<patch_name>/ directory.
   unless (do_system([$CP, $patch_name, $full_patch_name]))
   {
      baselog($LOG_ERROR, "Cannot copy '$patch_name' file to '$full_patch_name'");
      do_system([$RM, '-rf', $war_patch_dir]);
      return $ERROR;
   }

   # Copy meta information patch dir for using in poos
   unless (do_system([$CP, '-rf', 'META-INF/', $war_patch_dir]))
   {
      baselog($LOG_ERROR, "Cannot copy 'META-INF' file to '$war_patch_dir'");
      do_system([$RM, '-rf', $war_patch_dir]);
      return $ERROR;
   }

   # Create the <patch_dir>/war/<patch_name>/old/ directory
   unless (create_dir($old_patch_dir))
   {
      do_system([$RM, '-rf', $war_patch_dir]);
      return $ERROR;
   }

   # Save old files (modified and deleted files)
   unless (save_files_from_archive($warfile, [@{$mod_list}, @{$del_list}], $old_patch_dir))
   {
      baselog($LOG_ERROR, "Cannot save patched files", $TRUE);
      do_system([$RM, '-rf', $war_patch_dir]);
      return $ERROR;
   } ## end unless (save_files_from_archive...

   # Update war archive
   if (not update_war_file($WAR_OP_IN, $warfile, $add_list, $mod_list, $del_list))
   {
      do_system([$RM, '-rf', $war_patch_dir]);
      return $ERROR;
   }

   return $OK;
} ## end sub pins_war()

## @fn sub get_war_file($pinfo, $warpath)
# @brief Defines the full path to the war file
# @details Gets warfile from META-INF/patch-info file, tests that the file exists
# @param $pinfo   - patch-info file
# @param $warpath - variable to save warfile path
# @return $TRUE  - the war path defined successfully
# @return $FALSE - some error occurred
# @author  Vladimir Ivanov
# @date February 2008
# @note warfile is returned through the second function argument
sub get_war_file($$)
{

   # Get war file path from patch-info file
   my $pinfo = $_[0];

   baselog($LOG_DEBUG, "get_war_file: pinfo=$pinfo");
   if (-e $pinfo)
   {
      unless (getwarpath($pinfo, $_[1]))
      {
         baselog($LOG_ERROR, "Cannot find war file path in patch-info");
         return $FALSE;
      }
   } ## end if (-e $pinfo)
   else
   {
      baselog($LOG_ERROR, "Cannot find the patch-info file", $TRUE);
      return $FALSE;
   }

   # Check that target war file exists
   unless (-e $_[1])
   {
      baselog($LOG_ERROR, "Target war file $_[1] doesn't exist", $TRUE);
      return $FALSE;
   }
   return $TRUE;
} ## end sub get_war_file($$)

## @fn sub create_dir($dir)
# @brief Creates the directory; cleans if exists
# @details
# @param $dir - The directory name to create
# @return $TRUE  - the directory was created successfully
# @return $FALSE - some error occurred
# @author  Vladimir Ivanov
# @date February 2008
# @note
sub create_dir($)
{
   my $dir = shift @_;
   if (-d $dir)
   {
      do_system([$RM, '-rf', $dir]);
      baselog($LOG_DEBUG, "Cleaning '$dir'...");
   }

   baselog($LOG_DEBUG, "Creating '$dir' directory...");
   unless (do_system([$MKDIR, '-p', $dir]))
   {
      baselog($LOG_ERROR, "Cannot create '$dir' directory");
      return $FALSE;
   }
   return $TRUE;
} ## end sub create_dir($)

## @fn sub trim($line)
# @brief Trims white spaces
# @details Removes unnecessary space characters
# @param   $line - the line to remove white spaces
# @return  $line - the same line w\o spaces
# @author  Vladimir Ivanov
# @date February 2008
# @note
sub trim($)
{
   my $line = $_[0];
   $line =~ s/^\s+//;
   $line =~ s/\s+$//;

   return $line;
} ## end sub trim($)

## @fn sub getwarpath($pinfo, $warpath)
# @brief Defines the full path to the war file
# @details Gets warfile from META-INF/patch-info file
# @param $pinfo   - patch-info file
# @param $warpath - variable to save warfile path
# @return $TRUE  - the war path defined successfully
# @return $FALSE - some error occurred
# @author  Vladimir Ivanov
# @date February 2008
# @note war path is returned through the second function argument
sub getwarpath($$)
{
   my $pinfo = $_[0];

   my $FilePath = "FilePath";
   my $unknown  = "Unknown";

   my ($key, $val);

   unless (open(File, "$pinfo"))
   {
      baselog($LOG_ERROR, "Cannot open $pinfo file");
      return $FALSE;
   }

   while (my $line = <File>)
   {
      chomp($line);
      ($key, $val) = split("=", $line);
      $key = trim($key);
      $val = trim($val);
      if ($key eq $FilePath)
      {
         $_[1] = $val;
         close(File);

         # Check $unknown
         if ($val eq $unknown)
         {
            return $FALSE;
         }
         return $TRUE;
      } ## end if ($key eq $FilePath)
   } ## end while (my $line = <File>)
   close(File);

   # Didn't find $FilePath
   return $TRUE;
} ## end sub getwarpath($$)

## @fn sub save_files_from_archive($archive, $flist, $dir)
# @brief Saves files from the archive
# @details Extracts files from the archive into the given directory
# @param   $archive - full path to the archive
# @param   $flist   - file list to save
# @param   $dir     - directory to save files in
# @return $TRUE  - files were saved successfully
# @return $FALSE - some error occurred
sub save_files_from_archive($$$)
{
   my ($archive, $flist, $dir) = @_;
   my $retval = $TRUE;

   if (scalar(@{$flist}) > 0)
   {
      baselog($LOG_INFO, "Extract & Save files (total " . scalar(@{$flist}) . ") from the archive ($archive)", $TRUE);

      #do not try to list all extracting files here - the list can be very huge
      #so the log message will be useless anyway.
      baselog($LOG_DEBUG, "@quiteUNZIP -o $archive <some files list here> -d $dir");

      if (not do_system([@quiteUNZIP, '-o', $archive, @{$flist}, '-d', $dir], {$dsINTERACT => $verbose_mode}))
      {
         baselog($LOG_ERROR, "Cannot save files from the archive", $TRUE);
         $retval = $FALSE;
      }
   } ## end if (scalar(@{$flist}) ...
   else
   {
      baselog($LOG_INFO, "Warning: There is nothing to save");
   }
   return $retval;
} ## end sub save_files_from_archive($$$)

## @fn sub update_war_file($operation, $archive, $add_list, $mod_list, $del_list, $dir)
# @brief Updates war archive
# @details Updates war archive in accordance to the list of files given. In order
#          to guarantee the archive integrity, actual update happens against a backup
#          file. The subroutine uses update_files_in_archive() and
#          remove_files_from_archive().
# @param   $operation - full path to the archive
# @param   $archive   - full path to the archive
# @param   $add_list  - reference to a list of files added by a patch
# @param   $mod_list  - reference to a list of files modified by a patch
# @param   $del_list  - reference to a list of files deleted by a patch
# @param   $dir       - directory to save files in
# @return $TRUE  - files were saved successfully
# @return $FALSE - some error occurred
sub update_war_file($$$$$;$)
{
   my ($operation, $archive, $add_list, $mod_list, $del_list, $dir) = @_;
   my $retval = $FALSE;

   if ((scalar(@{$add_list}) + scalar(@{$mod_list}) + scalar(@{$del_list})) > 0)
   {
      my $tmp_archive = $archive . "." . $$;

      baselog($LOG_INFO, "Create working copy for the archive '$archive'", $TRUE);

      baselog($LOG_DEBUG, "Working copy of the archive is '$tmp_archive'");
      if (not do_system([$CP, $archive, $tmp_archive]))
      {
         baselog($LOG_ERROR, "Backing up of the archive failed", $TRUE);
      }
      else
      {
         my $res = $TRUE;

         if ($operation eq $WAR_OP_IN)
         {

            #PUT IN SERVICE

            # Update war archive
            baselog($LOG_DEBUG, "Updating target war file...");
            if (not update_files_in_archive($tmp_archive, [@{$mod_list}, @{$add_list}]))
            {
               baselog($LOG_ERROR, "Cannot update the '$tmp_archive' file", $TRUE);
               $res = $FALSE;
            }
            else
            {

               # Remove files from the war archive
               baselog($LOG_DEBUG, "Deleting files from the war file...");
               if (not remove_files_from_archive($tmp_archive, $del_list))
               {
                  baselog($LOG_ERROR, "Cannot delete files from the '$tmp_archive' file", $TRUE);
                  $res = $FALSE;
               }
            } ## end else [ if (not update_files_in_archive...

         } ## end if ($operation eq $WAR_OP_IN...
         elsif ($operation eq $WAR_OP_OUT)
         {

            #PUT OUT OF SERVICE

            #Remove from war file all files listed in the <war_patch_dir>/added-files file
            baselog($LOG_DEBUG, "Removing files from war archive...");
            if (not remove_files_from_archive($tmp_archive, $add_list))
            {
               baselog($LOG_ERROR, "Cannot delete files from the '$tmp_archive' file", $TRUE);
               $res = $FALSE;
            }
            else
            {

               # Update war file with the files from the <war_patch_dir>/old/ directory.
               baselog($LOG_DEBUG, "Updating target war file...");
               if (not update_files_in_archive($tmp_archive, [@{$mod_list}, @{$del_list}], $dir))
               {
                  baselog($LOG_ERROR, "Cannot update the '$tmp_archive' file", $TRUE);
                  $res = $FALSE;
               }
            } ## end else [ if (not remove_files_from_archive...

         } ## end elsif ($operation eq $WAR_OP_OUT...

         # if some error happend during WAR update
         if (not $res)
         {
            do_system([$RM, '-f', $tmp_archive]);
         }
         else
         {
            baselog($LOG_INFO, "Replacing the actual archive with the working copy.", $TRUE);

            baselog($LOG_DEBUG, "the actual archive is '$archive', the working copy is '$tmp_archive'");
            if (not do_system([$MV, $tmp_archive, $archive]))
            {
               baselog($LOG_ERROR, "Replacing of the archive failed", $TRUE);
               do_system([$RM, '-f', $tmp_archive]);
            }
            else
            {
               if (not do_system([$CHOWN, "$NORTEL_ACCT:$NORTEL_GRP", $archive]))
               {
                  baselog($LOG_ERROR, "Cannot change the '$archive' archive's ownership.", $TRUE);
               }
               else
               {
                  $retval = $TRUE;
               }
            } ## end else [ if (not do_system([$MV...
         } ## end else [ if (not $res)
      } ## end else [ if (not do_system([$CP...
   } ## end if ((scalar(@{$add_list...
   else
   {
      baselog($LOG_INFO, "Warning: There is nothing to update");
      $retval = $TRUE;
   }

   return $retval;
} ## end sub update_war_file($$$$$;$)

## @fn sub update_files_in_archive($archive, $flist, $dir)
# @brief Updates files in the archive
# @details Updates files in the archive with the files given in the list
# @param   $archive - full path to the archive
# @param   $flist   - reference to files list to update
# @param   $dir     - the directory to do chdir (optional) as some files are
#                     listed with a path relatively this directory.
# @return $TRUE  - files were updated successfully
# @return $FALSE - some error occurred
sub update_files_in_archive($$;$)
{
   my ($archive, $flist, $dir) = @_;
   my $retval = $TRUE;

   if (scalar(@{$flist}) > 0)
   {
      baselog($LOG_INFO, "Updating the archive with files (total " . scalar(@{$flist}) . ")", $TRUE);

      #do not try to list all extracting files here - the list can be very huge
      #so the log message will be useless anyway.
      baselog($LOG_DEBUG, "@quiteZIP -r $archive <some files list here>");

      if (not do_system([@quiteZIP, '-r', $archive, @{$flist}], {$dsINTERACT => $verbose_mode, $dsCHDIR => $dir}))
      {
         baselog($LOG_ERROR, "Updating archive failed", $TRUE);
         $retval = $FALSE;
      }

   } ## end if (scalar(@{$flist}) ...
   else
   {
      baselog($LOG_INFO, "Warning: There is nothing to update");
   }
   return $retval;
} ## end sub update_files_in_archive($$;$)

## @fn sub remove_files_from_archive($archive, $flist)
# @brief Removes files from the archive
# @details Updates an archive as so files given in the list are removed from
#          this archive
# @param   $archive - full path to the archive
# @param   $flist   - file list to remove
# @return $TRUE  - files were removed successfully
# @return $FALSE - some error occurred
sub remove_files_from_archive($$)
{
   my ($archive, $flist) = @_;
   my $retval = $TRUE;
   my $rc     = 0;

   if (scalar(@{$flist}) > 0)
   {
      baselog($LOG_INFO, "Removing files (total " . scalar(@{$flist}) . ") from the archive", $TRUE);

      #do not try to list all extracting files here - the list can be very huge
      #so the log message will be useless anyway.
      baselog($LOG_DEBUG, "@quiteZIP -d $archive <some files list here>");

      if (not do_system([@quiteZIP, '-d', $archive, @{$flist}], {$dsRETVAL => \$rc, $dsINTERACT => $verbose_mode}))
      {

         # If the file does not exist then zip will fail but this is not an error since the
         # file indeed does not exist. Thus issue a warning only.
         # - "12" == file does not exist in archive
         if ($rc == $ZIP_special_rc)
         {
            baselog($LOG_WARNING, "Warning: Removing files failed - some files may not exist in archive", $TRUE);
         }
         else
         {
            baselog($LOG_ERROR, "Removing files failed", $TRUE);
            $retval = $FALSE;
         }
      } ## end if (not do_system([@quiteZIP...

   } ## end if (scalar(@{$flist}) ...
   else
   {
      baselog($LOG_INFO, "Warning: There is nothing to remove");
      $retval = $TRUE;
   }
   return $retval;
} ## end sub remove_files_from_archive($$)

## @fn sub check_war_conflict($warfile, $filelist)
# @brief Checks war patch for conflicts
# @details Searches all patches for this war file for files, going to be patched
#          by this patch (it is forbidden)
# @param  $warfile  - full path to the archive
# @param  $filelist_array - reference to files list
# @return $TRUE  - no conflicts
# @return $FALSE - conflict detected
sub check_war_conflict($$)
{
   my ($warfile, $filelist_array) = @_;
   my ($pinfo, $addfile, $modfile, $delfile);
   my ($wardir, $wf);

   my $ret = $FALSE;

   if (not is_array($filelist_array))
   {
      baselog($LOG_ERROR, "Cannot handle the war files list.");
      return $ret;
   }

   my $filelist = "@{$filelist_array}";

   baselog($LOG_DEBUG, "warfile=$warfile");
   baselog($LOG_DEBUG, "filelist contains " . scalar(@{$filelist_array}) . " element(s)");

   unless (opendir(Dir, $my_patch_dir))
   {
      baselog($LOG_ERROR, "Cannot open '$my_patch_dir' directory");
      return $ret;
   }

   baselog($LOG_DEBUG, "Looking for war patches installed in the '$my_patch_dir' directory");

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

      # find all patch directories
      $wardir = $my_patch_dir . $file;
      if (-d $wardir)
      {
         unless ($file =~ /^\./)
         {
            baselog($LOG_DEBUG, "Trying to inspect content for '$file' directory");

            $pinfo   = "$wardir/$pinfo_file";
            $addfile = "$wardir/$add_file";
            $modfile = "$wardir/$mod_file";
            $delfile = "$wardir/$del_file";

            # Get war file path from /META-INF/patch-info file
            if (-e $pinfo)
            {
               unless (getwarpath($pinfo, $wf))
               {

                  # Check return value
                  baselog($LOG_ERROR, "Cannot find war file path in patch-info");
                  goto end;
               } ## end unless (getwarpath($pinfo,...

            } ## end if (-e $pinfo)
            else
            {

               # File pinfo does not exist; Something wrong here
               baselog($LOG_DEBUG, "Patch info file ($pinfo_file) does not exists.");
               next;
            } ## end else [ if (-e $pinfo)

            # Check if we patches the same war patch
            if ($warfile ne $wf)
            {
               baselog($LOG_DEBUG, "'$warfile' not equal '$wf'");
               next;
            }

            # Check modified files for conflicts
            baselog($LOG_DEBUG, "Checking modified files from '$wf' for conflicts");
            if (is_conflict($modfile, $filelist))
            {
               baselog($LOG_ERROR, "The conflict with the '$file' patch was detected", $TRUE);
               goto end;
            }

            # Check added files for conflicts
            baselog($LOG_DEBUG, "Checking added files from '$wf' for conflicts");
            if (is_conflict($addfile, $filelist))
            {
               baselog($LOG_ERROR, "The conflict with the '$file' patch was detected", $TRUE);
               goto end;
            }

            # Check deleted files for conflicts
            baselog($LOG_DEBUG, "Checking deleted files from '$wf' for conflicts");
            if (is_conflict($delfile, $filelist))
            {
               baselog($LOG_ERROR, "The conflict with the '$file' patch was detected", $TRUE);
               goto end;
            }

         } ## end unless ($file =~ /^\./)
         else
         {
            baselog($LOG_DEBUG, "Found '$file' directory - ignoring");
         }
      } ## end if (-d $wardir)
   } ## end while (my $file = readdir...
   $ret = $TRUE;
 end:
   closedir(Dir);
   return $ret;
}  # End of check_war_conflict()

