#!/usr/bin/perl
## @file backupbase
# @brief This script makes base backup. It collects patches data and important
#   system settings and puts them to archive in specific directory.
# @details
# @param none
# @return $OK    - OK
# @return $ERROR - ERROR
#
# @author Kate Bereznyakova
# @author Vladimir Ivanov
# @date October 2008
# @version 1.00 - Initial version
# @version 1.01 - 'use strict;' added. Creation of the backup directory via call
#    of the function create_base_backup_dir.
# @version 1.02 - Two colons were removed before shared functions and variables.
# @version 1.03 - Modified under the perlcheck warnings cleanup activity.
# @version 1.04 - Modified to backup R6.0 patches and SPs
# @note
use lib "/opt/nortel/base/lib";

use warnings;
use strict;
use common_variables;
use common_functions;
use recovery::backup;
use patchutil::patches_target;
use patchutil::patches_public;
use ifxml;
use baselog;

exit(main());

###########################################################
#               Main function (entry point)               #
#                                                         #
# INPUT: NO                                               #
#                                                         #
# OUTPUT: $OK    - backup successfully created            #
#         $ERROR - backup failed                          #
###########################################################
sub main
{
   baselog($LOG_INFO, "Performing Base Backup.\n", $TRUE);

   if (warning_if_not_nortel_grp())
   {
      return $ERROR;
   }

   my $retval = $ERROR;
   if (!-e $basebackup_dir)
   {
      if (create_base_backup_dir() == $FALSE)
      {
         baselog($LOG_ERROR, "Cannot create $basebackup_dir directory. Base backup operation failed.\n");
         return $retval;
      }

   } ## end if (!-e $basebackup_dir...
   else
   {
      if (system("$RM -rf $basebackup_dir/*") ne "0")
      {
         baselog($LOG_WARNING, "Problems in cleanup $basebackup_dir directory operation.\n", $TRUE);
      }
   } ## end else [ if (!-e $basebackup_dir...

   baselog($LOG_INFO, "Checking for available patches in $PATCHDIR", $TRUE);

   # Backup patches to allow patch re-install recovery
   if (`$LS $PATCHDIR | $GREP .$PATCHEXT | $WC -c` != 0)
   {  #
        # If patches exists then backup everything under the "/var/opt/nortel/patch/" folder.
        # Upgrades and restores will extract what is required.
        # - we may want to get smarter at a later time to backup only those
        #   patches in the patch database listed as in-service and loaded.
        #
      baselog($LOG_INFO, "   Creating patch archive $basebackup_dir/$patches_data_archive", $TRUE);
      if (system("$TAR -Pczf $basebackup_dir/$patches_data_archive $PATCHDIR --exclude '$SRV_PACK_DIR/*.$PATCHEXT'") != 0)
      {
         baselog($LOG_ERROR, "   Patch backup archive creation failed.\n", $TRUE);
      }
      else
      {
         baselog($LOG_ERROR, "   Patch backup archive creation completed.\n", $TRUE);
      }
   } ## end if (`$LS $PATCHDIR | $GREP .$PATCHEXT | $WC -c`...

   if (-e "$basebackup_dir/.backup_cookie")
   {
      unlink("$basebackup_dir/.backup_cookie");
   }

   system("$CP /opt/nortel/base/recovery/.backup_cookie $basebackup_dir/") == 0
     || baselog($LOG_WARNING, "Cannot copy .backup_cookie file to $basebackup_dir directory.\n");

   # Backup current base version
   my $parser = ifxml::new($APP_CFG_ROOT, $APPLCFG);
   my $current_base_version = "";
   if (!$parser)
   {
      baselog($LOG_WARNING, "Installed base version undefined. Parser initialization failed.");
   }
   else
   {
      $current_base_version = $parser->get_application_version({name => 'base'});
   }

   #calling destructor;
   undef $parser;

   if (ref $current_base_version eq 'ARRAY')
   {
      system("$ECHO ' Base version information.'>>$basebackup_dir/.backup_cookie");
      system("$ECHO 'baseversion=@$current_base_version' >> $basebackup_dir/.backup_cookie") == 0
        || baselog($LOG_WARNING, "Backup base version data failed.\n");
   } ## end if (ref $current_base_version...
   else
   {
      baselog($LOG_WARNING, "Cannot define base version.");
   }

   #Back up installed application configuration data
   if (-e $CONFVER)
   {
      system("$CP $CONFVER $basebackup_dir/") == 0
        || baselog($LOG_WARNING, "Can not copy $CONFVER file to $basebackup_dir directory.\n");
   }

   #----------- sync. sysinfo information

   # if sync. userinfo failed then save original userinfo
   if (system("$SUDO $SYSPARM_GEN $userinfo_sync_file") != $OK)
   {
      baselog($LOG_WARNING, "Cannot generate new userinfo.txt file. Old one will be used for backup.\n");
      system("$CP /admin/userinfo.txt $userinfo_sync_file") == 0
        || baselog($LOG_ERROR, "Can not copy old one userinfo.txt file. This will lead to fatal error. Base backup archive will not be created.\n");
   } ## end if (system("$SUDO $SYSPARM_GEN $userinfo_sync_file"...

   # Check that we have all that we need.
   # presents of installedconfig is not critical
   if (-e $userinfo_sync_file and -e "$basebackup_dir/.backup_cookie")
   {
      $retval = $OK;
   }

   baselog($LOG_INFO, "Base Back-up Completed\n", $TRUE);

   return $retval;
} ## end sub main

