#!/usr/bin/perl 

# @file patchreapply
# @brief this module restore the patches, sps, and patches database backed up by sysbackup.
# @details none
# @author Ana Alvarez
# @date Feb 2009
# @version 1.00 - Initial version

use lib '/opt/nortel/base/lib';

use warnings;

use common_variables;
use common_functions;
use baselog;
use basedb;
use install_common;
use ifxml;
use patchutil::patching;
use patchutil::patches_public;
use patchutil::patches_target;

use base_system;

sub main();             # entry point

my $patch_recovery_source     = "/admin/nortel/backup/base/patchesinfo.tar.gz";
my $backup_baseversion_source = "/admin/nortel/backup/base/.backup_cookie";

exit(main());

sub main()
{
   my ($DIR, $patch, @patches, $pins_time_tmp);

   # should i allow to restore an empty database? same applies to recovery.....
   if (!-e $patch_recovery_source)
   { 
      baselog($LOG_INFO, "There are no patches to restore.\n", $TRUE);
      return $OK;
   }

   # Check the installed and backed up base version
   if (-e $backup_baseversion_source)
   {
      baselog($LOG_DEBUG, "Restore source $backup_baseversion_source exists.");
      if (`$CAT $backup_baseversion_source | $GREP baseversion | $WC -l` != 1)
      {
         baselog($LOG_WARNING, "Backup does not have valid base version information.\n");
         return $ERROR;
      }
      else
      {
         my $parser = ifxml::new($APP_CFG_ROOT, $APPLCFG);
         if (!$parser)
         {
            baselog($LOG_WARNING, "Installed base version undefined. Patch restore skipped.");
            return $ERROR;
         }
         my $current_base_version = $parser->get_application_version({name => 'base'});

         #calling destructor;
         undef $parser;

         chomp(my $backup_version = `$CAT $backup_baseversion_source | $GREP baseversion`);
         baselog($LOG_DEBUG, "Backed up base version $backup_version.");

         my @tmp_list = ();
         my @patch_list = ();
         if ($backup_version ne "baseversion=@$current_base_version")
         {
            baselog($LOG_WARNING, "Backed up ($backup_version) and current (baseversion=@$current_base_version) base version are different. \n", $TRUE);
            baselog($LOG_WARNING, "No patches retrieved. Patch restoring operation aborted.\n", $TRUE);
            return $ERROR;

         } ## end if ($backup_version ne...
         else
         {  #
            # Retrieve the content of $PATCHDIR
            #
            baselog($LOG_INFO, "Patch restore operation in progress...\n", $TRUE);

            # Confirm that the patch db exists in the backup bundle
            @tmp_list = ();
            if (!do_system([$TAR, "-ztf", $patch_recovery_source, "\*\.handles" ], {$dsOUT => \@tmp_list}) )
            {
               baselog($LOG_ERROR, "Patch DB does not exist in backup ($patch_recovery_source)", $TRUE);
               return $ERROR;
            }

            if (!do_system([$TAR, "-ztf", $patch_recovery_source ], { $dsOUT => \@tmp_list, $dsNOERROR => $TRUE} ) )
            {
               baselog($LOG_ERROR, "Retrieving list of patches from the backup ($patch_recovery_source) failed", $TRUE);
               return $ERROR;
            }
            chomp(@tmp_list);
            push @patch_list, @tmp_list;

            print("... Restoring: \"$patch_recovery_source\"\n");
            foreach my $pp (@patch_list)
            {
               print("... $pp\n");
            }
            print("...\n");  
 
            # calling sudo_nortel to cleanup $PATCHDIR prior to restoring
            unless (do_system([$SUDO, $SUDO_NORTEL, $CLEANUP_PATCHDIR]))
            {
                baselog($LOG_ERROR, "Cannot cleanup $PATCHDIR prior to restoring.\n", $TRUE);
            }

            # Retrieve all files from patch backup 
            if (system($TAR, "-zPxf", "$patch_recovery_source"))
            {
               baselog($LOG_ERROR, "Failed to retrieve content from backup ($patch_recovery_source)", $TRUE);
               return $ERROR;
            }
         }
      } ## end else [ if (`$CAT $backup_baseversion_source | $GREP baseversion | $WC -l`...
   } ## end if (-e $backup_baseversion_source...
   else
   {
      baselog($LOG_INFO, "No backup base version information.\n", 1);
      return $ERROR;
   }
   #
   # Reapply all patches flagged by the original .handles db that was recovered
   # from the sysbackup archive file.
   #
   my $patching = patchutil::patching::new();
   if (not defined($patching))
   {
      baselog($LOG_ERROR, 'Cannot create patching object');
      return $ERROR;
   }
   else
   {
      if (not $patching->restore_patches())
      {
         baselog($LOG_ERROR, "Patch restore failed");
         $patching = undef;
         return $ERROR;
      }
      $patching = undef;
   } ## end else [ if (not defined($patching...
   return $OK;

} ## end sub main()

#Ctrl-C handler
sub cleanup()
{
   return;
}
