#!/usr/bin/perl
## @file recoverbase
# @brief This script recovers system-wide data.
# @details Restore operation includes the following steps:
#     1. Clean up system route table to avoid any loops (if there 
#        are some other  settings already configured.)
#     2. Restore backed up appcustconfig files back to 
#        /var/opt/nortel/base/appconfig/appcustconfig/ directory.
#     3. Reapply new routes.
#     4. Reapply new host settings.
#     5. Reapply new firewall rules.
#     6. Reapply hardening items.
#			   
# @author Kate Bereznyakova
# @date December 2007
# @version 1.00 - Initial version
# @version 1.01 - Reapplying hardening items is added.
# @version 1.02 - Two colons were removed before shared functions and variables.
# @version 1.03 - remove confirmation of recovering system wide data.
# @note
use lib "/opt/nortel/base/lib";

use strict;
use warnings;

use recovery::backup;
use hostControl;
use base_firewall;
use RouteControl;
use common_variables;
use base_system;
use hardening::hardening_common;
use baselog;
use CommonInstallUtils;

sub main();
exit(main());

sub main()
{
   my $retval = $OK;
   if (-e $appconfig)
   {
      baselog($LOG_INFO, "Backup system wide data found.\n", $TRUE);
      my $route = new RouteControl;
      if (!defined $route)
      {
         baselog($LOG_ERROR, 'Cannot create RouteControl object.');
         $retval = $ERROR;
      }
      else
      {
         # Unset appcustom config data
         if (!$route->apply_all('del'))
         {
            baselog($LOG_ERROR, 'Cannot update routes');
            $retval = $ERROR;
         }
         undef $route;
      }
      # Extract appcustom config data from backup archive
      unless (do_system([$TAR, '-Pxzf', $appconfig]))
      {
         baselog($LOG_ERROR, "Cannot extract custom appconfig backup data.\n", $TRUE);
      }

      # Set up appcustom config data with new or old settings.
      # 1. Set up routes
      $route = new RouteControl;
      if (defined $route)
      {
         if (!$route->apply_all('add'))
         {
            baselog($LOG_ERROR, 'Cannot update routes');
            $retval = $ERROR;
         }
	 undef $route;
      } ## end if (defined $route)
      else
      {
         baselog($LOG_ERROR, 'Cannot create RouteControl object.');
	 $retval = $ERROR;
      }

      # 2. Set up hosts
      my $host = new hostControl;
      if (!defined $host)
      {
         baselog($LOG_ERROR, 'Cannot create hostControl object');
         $retval = $ERROR;
      }
      elsif (!$host->sync())
      {
         baselog($LOG_ERROR, 'Cannot synchronize hosts.');
         $retval = $ERROR;
      }
      undef $host;

      # 3. Set up firewall
      unless (firewall_restart())
      {
         baselog($LOG_ERROR, 'Cannot restart firewall');
         $retval = $ERROR;
      }
      # 4. Reapply all hardening modules
      if (not do_basic_hardening_action($H_IF_RECOVERY))
      { 
         baselog($LOG_ERROR, "Basic hardening items reapplying failed.");
         $retval = $ERROR;
      }
      if (not do_enhanced_hardening_action($H_IF_RECOVERY))
      {
          baselog($LOG_ERROR, "Enhancement hardening items reapplying failed.");
          $retval = $ERROR;
      }

   } ## end if (-e $appconfig...
   else
   {
      baselog($LOG_DEBUG, 'System wide data backup file does not exist.');
   }
   return $retval;
} ## end sub main()
