#!/usr/bin/perl 
## @file appinstall
# @brief This is application installation/upgrade/re-install script.
# @details
# The flag $INTERACTIVE determines prompt user for input or perform actions
# by default without any questions.
# The flag $baseAppsmode determines install Nortel Applications or Base Applications
# that are located in /var/opt/nortel/base_apps/ directory. Base Applications
# installation is performed only one time - during first bootup of the server.
# For baseAppsmode there are several differences:
# - baseapps-install.xml file is used instead of install.xml
# - installed RPMs are not backed up during installation.
# - file /admin/nortel/install/installedconfig is not created.
# - Base Applications are installed in non interactive mode.
# @param --help      print this help
# @param --test installs the applications from the specified place.
# - path to loadbuild
# @param --deploy installs the applications noninteractively
# - path to loadbuild
# @param --undeploy  undeploy installed applications
# @param --upgrade upgrades the applications noninteractively.
# - path to loadbuild
# @param --base-app installs base applications during first bootup
#
# @author Ilya Teterev
# @author Evgeny Lazarev
# @author Alexander Kolotov
# @version 1.00 - Initial version
# @version 1.01 - usage of 'su' command is replaced by invoking
#           prepare_command_to_run_as subroutine.
# @version 1.02 - POD section was added; the usage is displayed by the
#          common function - print_pod_usage() now.
# @version 1.10 - Base Applications support added.
# @version 1.11 - Added basic hardening items reapplying.
# @version 1.12 - Added mount option "nosuid"
# @version 1.15 - 'interrupt_installation' function is reworked;
#           'install_iteration' function is reworked: all RMPs are
#           installed in one transaction.
# @version 2.00 - added Centralized Deployment parameters: deploy, undeploy, upgrade.
#          appinstall can work noninteractively
# @version 2.01 - added support to accept an alphanumeric character in the rpm name
#          for Call Server support
# @version 2.02 - added support for single file installation. Place a .nai or .iso file on any
#          supported media and it will mount the file and do the install.
# @version 2.10 - support of the users accounts is added.
# @version 2.11 - fix issue with recovery Base Applications data. Function check_scripts is
#           invoked with the $applist parameter.
# @version 2.12 - logging output during deployment operatins has been added.
# @version 2.13 - Two colons were removed before shared functions and variables.
# @version 2.14 - Extended access points for Linux_SFTP_User and Linux_FTP_SFTP_User users
#          are mounted as well as for Linux_FTP_User users. Fix issue with removing
#          applications RPMs after instalaation failure.
# @version 2.15 - Recovery/backup actions for user accounts are removed.
# @version 2.16 - Added support for multiple NAI files.
# @version 2.17 - New error code is added: RSBLK
# @version 2.18 - New field 'STATUS' is added into installedconfig file.
#                 Copying of install.xml and instaledconfig files is moved
#                 before RPMs are installed. Temporary installedconfig.install is implemented
#                 It's copyed to installedconfig if appinstall successful.
# @version 2.19 - explicit list of semaphores is used instead of @SEMLIST variable. Fixed
#                 error message in the linuxbase.log file when Base applications are
#                 installing ("Can not create base_apps_service object")
# @version 2.20 - $ENV{'PATH'} is now patched to contain /bin and /usr/bin under
#                 restricted shell ('tar' command requires it).
# @version 2.21 - deletion text banner about UCM login from motd file when appinstall for
#                 nortel applications is finished successfully
# @version 2.22 - Added additional console output to indicate progress
#               - Removed from main() the cleanup of the backupdir since on firstboot (after the
#                 base app installation) one needs to recover any base patches backed-up
# @version 2.23 - Changes to keep appinstall log files for Base and Nortel Applications
#                 separately. New report file for appinstall is added.
# @version 2.24 - Added Base Patch Recovery during the "base-app" stage
# @version 2.25 - INIT and END section are moved into appinstall_INIT and appinstall_END functions.
#                 Invokes directly. Invoke of 'return' function in main is changed to invoke appinstall_END
#                 with correct exit code.
# @version 2.26 - Only "NULL" errors can now be replaced by "UNER" value. Another values should be just propagated.
# @version 2.27 - $INTERACTIVE variable is updated just after parsing of the input parameters.

=pod

=head1 NAME

B<appinstall> - tool to perform installation, upgrade and re-install of the applications.

=head1 SYNOPSIS

B<appinstall> [B<--help>]

=head1 DESCRIPTION

This is the application installation/upgrade/re-install script. It reads data
from the install.xml file on the application media and unpacks files for the
configuration, selected by user. Then starts the configuration scripts.
At the end, it starts the applications.

=head1 OPTIONS

=over 4

=item B<--help> 

Print this help message and exit.

=back

=head1 CAVEATS

B<appinstall> can be run only under Nortel authorised accounts.

=head1 SEE ALSO

B<appstart>

=cut

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

use common_variables;
use common_functions;
use install_common;
use base_accounts;
use XML::Parser;
use recovery::backup;
use appinstall::listgen;
use appinstall::errors;
use appinstall::update_sys_data;
use appinstall::appupgrade;
use appinstall::check_cd;
use appinstall::show_app_info;
use ifxml;
use base_firewall;
use baselog;
use File::Copy;
use File::Path;
use base_semctl;
use base_apps_service;
use base_system;
use hardening::hardening_common;
use base_ftp;
use Getopt::Long;
use CommonInstallUtils;
use base_monit;

use recovery::backup;

use strict;
use warnings;

sub main();

my @status_text = ();  # installation status information
my ($instnum, $order) = ();

my $testmode = $FALSE;  # install Applications from the specified location
$INTERACTIVE  = $TRUE;  # is installation interactive or not
$baseAppsmode = $FALSE; # if flag is 1 then perform Base Applications installation

my $mntpointbackup = $MNTPOINT;
exit(main());

## @fn sub appinstall_INIT()
# @brief INIT actions for appinstall
# @return $TRUE  - on success
# @return $FALSE - on error ($LASTERR is set)
sub appinstall_INIT()
{
   baselog($LOG_INFO, "Appinstall is started");

   signals_ignore();

   system($MKDIR, '-p', $MNTPOINT);
   system($MKDIR, '-p', $NAIMNTPOINT);
   unless (-d $RPMBACKUP)
   {
      system($MKDIR, '-p', $RPMBACKUP);
      change_permissions_and_ownership(0775, $NORTEL_UID, $GROUPS{'patchadmin'}, [$RPMBACKUP]);
   } ## end unless (-d $RPMBACKUP)

   # remove Nortel's Apps report file if exists
   if (-e $APPINSTALL_REPORTFILE)
   {
      baselog($LOG_INFO, 'Removing status report file.');
      if (!do_system([$RM, '-f', $APPINSTALL_REPORTFILE]))
      {
         baselog($LOG_WARNING, "Cannot remove '$APPINSTALL_REPORTFILE'.");
      }
   } ## end if (-e $APPINSTALL_REPORTFILE...

   #initialize semaphore manager
   $SEMMNGR = base_semctl::new("APPINSTALL");
   if (!defined($SEMMNGR))
   {
      my $error_descr = $!;
      baselog($LOG_ERROR, "Cannot create semaphore manager object ($error_descr).");
      return $FALSE;
   } ## end if (!defined($SEMMNGR)...

   if (!$SEMMNGR->sem_take([$SEM_BACKUP, $SEM_PATCH, $SEM_APPINSTALL, $SEM_APPSTART, $SEM_CRONTAB, $SEM_HARD], IPC_NOWAIT))
   {
      baselog($LOG_ERROR, "Cannot take semaphores.");
      baselog($LOG_ERROR, "Appinstall operation aborted.\n", $TRUE);
      undef $SEMMNGR;
      $LASTERR = "RSBLK";
      return $FALSE;
   } ## end if (!$SEMMNGR->sem_take...
   return $TRUE;
} ## end sub appinstall_INIT()

## @fn sub appinstall_END($return_val)
# @brief appinstall cleanup.
# @param $return_val - appinstall exit code
# @return $OK or $ERROR(against $return_val) in interactive mode
# @return $LASTERR in non interactive mode
sub appinstall_END($)
{

   my $return_val = shift @_;
   if ($LASTERR eq "NULL" and $return_val ne $ERROR)
   {
      print_header("Application installation finished");
      baselog($LOG_INFO, "Appinstall is finished");

      # print status of the installation
      my $report_h = undef;
      if (not open($report_h, '>', $APPINSTALL_REPORTFILE))
      {
         baselog($LOG_ERROR, "Cannot open $APPINSTALL_REPORTFILE file: $!");
      }
      else
      {
         print $report_h @status_text;
         if (not close($report_h))
         {
            baselog($LOG_ERROR, "Cannot close the file descriptor: $!.");
         }
      } ## end else [ if (not open($report_h...

      #print installation status at interactive mode only.
      if ($INTERACTIVE)
      {
         print @status_text;
         print "\n";
      }

      if ($baseAppsmode)
      {
         showAppInfo("base");
      }
      else
      {

         # Application is installed, so delete $FIRST_LOGIN_GUIDE text from motd file
         delete_line_from_file($MOTDFILE, "To access the UCM Web page");
         delete_line_from_file($MOTDFILE, "UCM URL: http:");
         showAppInfo("all");
      } ## end else [ if ($baseAppsmode)
   } ## end if ($LASTERR eq "NULL"...
   elsif ($LASTERR eq "CNCL")
   {
      print_header("$appinstall::errors::ERRORDB{$LASTERR}");
      baselog($LOG_INFO, "$appinstall::errors::ERRORDB{$LASTERR}");
      unless (do_system([$TOUCH, $APPINSTALL_REPORTFILE]))
      {
         baselog($LOG_WARNING, "Cannot touch '$APPINSTALL_REPORTFILE'.");
      }
   } ## end elsif ($LASTERR eq "CNCL"...
   else
   {
      print_header("Error: $appinstall::errors::ERRORDB{$LASTERR}");
      baselog($LOG_ERROR, "$appinstall::errors::ERRORDB{$LASTERR}");
      unless (do_system([$TOUCH, $APPINSTALL_REPORTFILE]))
      {
         baselog($LOG_WARNING, "Cannot touch '$APPINSTALL_REPORTFILE'.");
      }
   } ## end else [ if ($LASTERR eq "NULL"...
   print "\n\n";
   chdir("/");
   if (!$testmode && $INTERACTIVE)
   {

      # the "-l" option tells "umount" to detach the filesystem from the
      # filesystem hierarchy and to clean all references to the
      # filesystem as soon as it is not busy anymore.
      system("$UMOUNT -l $MNTPOINT 2>/dev/null");
      system("$UMOUNT -l $mntpointbackup 2>/dev/null");
      if ($LASTERR eq "NULL")
      {
         system("$EJECT 2>/dev/null");
      }
   } ## end if (!$testmode && $INTERACTIVE...

   #delete semaphore manager object
   #destructor releases all semaphores taken by the manager
   if (defined $SEMMNGR)
   {
      undef $SEMMNGR;
   }
   if ($INTERACTIVE)
   {
      return $return_val;
   }
   else
   {
      return $ERRCODES{$LASTERR};
   }
} ## end sub appinstall_END($)

## @fn sub set_install_source(@)
# @brief Choose and mount image file for installation
# @param @appfiles - list of image files
# @return $TRUE  - on success
# @return $FALSE - on error ($LASTERR is set)
# @author Alexander Khoroshilov
# @date November 2008
sub set_install_source(@)
{
   my @appfiles     = @_;
   my $files_number = scalar(@appfiles);
   my $chosen_file;

   if ($files_number == 0)
   {

      # no any files provided
      $LASTERR = "NEPA";
      return $FALSE;
   } ## end if ($files_number == 0...
   elsif ($files_number == 1)
   {

      # only one file provided
      $chosen_file = 1;
   } ## end elsif ($files_number == 1...
   else
   {

      #multiple files provided
      $chosen_file = 0;
      do
      {

         # print list
         clear();
         print "Please select one of the files ('q' means exit):\n";
         showListByPage(\@appfiles, 'q');

         # get user answer
         chomp(my $input = <STDIN>);
         if ($input eq 'q')
         {

            # abort
            $LASTERR = "CNCL";
            return $FALSE;
         } ## end if ($input eq 'q')
         if ($input =~ /^\d+$/)
         {
            if (($input > 0) && ($input <= $files_number))
            {

               # file choice done
               $chosen_file = $input;
            } ## end if (($input > 0) && ($input...
         } ## end if ($input =~ /^\d+$/)
      } while (!$chosen_file);
   } ## end else [ if ($files_number == 0...

   # change $MNTPOINT and mount an image
   $MNTPOINT = $NAIMNTPOINT;

   # The US DoD requirement is to mount removable media with nosuid option.
   unless (do_system([$MOUNT, "-o", "nosuid,loop", "-t", "iso9660", $appfiles[$chosen_file - 1], $MNTPOINT]))
   {

      #Can't mount media
      $LASTERR = "EMNT";
      return $FALSE;
   } ## end unless (do_system([$MOUNT,...
   chdir($MNTPOINT);

   return $TRUE;
} ## end sub set_install_source(@)

## @fn sub main ()
# @brief Main function of the script
# @details This function is an ecntry point of appinstall commmand.
# @param
#   - -test - install applications from specified place with prompts for user.
#   - -deploy - install applications from specified place without prompts for user. This key is used for the Centralised Deployment.
#   - -undeploy - perfoms un-installation of the applications without prompts for user. This key is used for the Centralised Deployment.
#   - -upgrade - perfoms applications upgrade from specified place without prompts for user. This key is used for the Centralised Deploymenta
#   - -test - show help message
# @return
#   One of the errors defined in error.pm module, or 0 if operation is successfull.
# @author Evgeny Lazarev
# @date April 2008
# @note
sub main()
{
   my ($help, $testpath, $deploypath, $undeploymode, $upgradepath) = ($FALSE, undef, undef, $FALSE, undef);
   GetOptions('test=s' => \$testpath, 'base-app' => \$baseAppsmode, 'deploy=s' => \$deploypath, 'undeploy' => \$undeploymode, 'upgrade=s' => \$upgradepath, 'help' => \$help);
   if ($help)
   {
      print_pod_usage();
      return $OK;
   }

   # set installation process to a non-interactive mode in four cases:
   # Base applications installation and Deploy/Undeploy/Upgrade via DM
   if ($baseAppsmode || $deploypath || $undeploymode || $upgradepath)
   {
      $INTERACTIVE = $FALSE;
   }

   if (not appinstall_INIT())
   {
      return appinstall_END($ERROR);
   }
   recover_env_path();
   if (is_rpm_database_corrupted())
   {
      $LASTERR = "RPMDB";
      baselog($LOG_ERROR, "RPM datadase is CORRUPTED! System must be reinstalled!", $TRUE);
      return appinstall_END($ERROR);
   } ## end if (is_rpm_database_corrupted...

   if (warning_if_not_root_uid())
   {
      $LASTERR = "NUSR";
      return appinstall_END($ERROR);
   }
   baselog($LOG_DEBUG, "appinstall is started with the following options: @ARGV");

   if ($baseAppsmode)
   {
      $APPINSTALL_OUTFILE    = $APPINSTALL_BASEAPPS_OUTFILE;
      $APPINSTALL_ERRFILE    = $APPINSTALL_BASEAPPS_ERRFILE;
      $APPINSTALL_REPORTFILE = $APPINSTALL_BASEAPPS_REPORTFILE;
   } ## end if ($baseAppsmode)

   #add timestamp to log files.
   if (!do_system([$DATE], {$dsOUTFILE => $APPINSTALL_OUTFILE, $dsOUTMODE => '>>'}))
   {
      baselog($LOG_ERROR, "Cannot put timestamp to '$APPINSTALL_OUTFILE'.");
   }

   if (!do_system([$DATE], {$dsOUTFILE => $APPINSTALL_ERRFILE, $dsOUTMODE => '>>'}))
   {
      baselog($LOG_ERROR, "Cannot put timestamp to '$APPINSTALL_ERRFILE'.");
   }

   #Check for input parameter

   if (defined($testpath))
   {
      $testmode       = $TRUE;
      $MNTPOINT       = $testpath;
      $INSTVER        = $MNTPOINT . "/" . $INSTCFG;
      $INSTALL_FOLDER = $MNTPOINT;
   } ## end if (defined($testpath)...
   elsif ($baseAppsmode)
   {
      baselog($LOG_DEBUG, "Base Applications installation started", $TRUE);
      $baseAppsmode   = $TRUE;
      $CONFVER        = $BASEAPPCONFVER;
      $INSTCFG        = $BASEAPPINSTCFG;
      $MNTPOINT       = $BASEAPPDIR;
      $INSTVER        = $MNTPOINT . '/' . $INSTCFG;
      $INSTALL_FOLDER = $MNTPOINT;
      $ARCHVER        = $ARCHDIR . '/' . $INSTCFG;

   } ## end elsif ($baseAppsmode)
   elsif (defined($deploypath))
   {
      baselog($LOG_DEBUG, "Nortel Applications deployment started.", $TRUE);
      if (-e $CONFVER)
      {
         $LASTERR = "AAIN";
         return appinstall_END($ERROR);
      }
      $MNTPOINT       = $deploypath;
      $INSTVER        = $MNTPOINT . "/" . $INSTCFG;
      $INSTALL_FOLDER = $MNTPOINT;
   } ## end elsif (defined($deploypath...
   elsif ($undeploymode)
   {
      baselog($LOG_DEBUG, "Nortel Applications undeployment started.", $TRUE);
      if (!(-e $CONFVER))
      {
         $LASTERR = "ANIN";
         return appinstall_END($ERROR);
      }
      if (!appinstall::appupgrade::check_upgrade())
      {
         if ($LASTERR eq "NULL")
         {
            $LASTERR = "UNER";  # generic error if no specific
         }
         return appinstall_END($ERROR);
      } ## end if (!appinstall::appupgrade::check_upgrade...

      #restart monit with configuration reload
      #unmonitor undeployed applications.
      unless (appinstall::update_sys_data::update_sdb())
      {
         baselog($LOG_ERROR, "Cannot reload configuration data.");
      }
      else
      {
         restart_monit();
      }

      base_accounts::del_users($NORTEL_USERS);
      if (-e $ARCHVER)
      {
         if (unlink($ARCHVER) != 1)
         {
            baselog($LOG_INFO, "$ARCHVER could not be removed.");
         }
         else
         {
            baselog($LOG_INFO, "$ARCHVER has been removed.");
         }
      } ## end if (-e $ARCHVER)
      return appinstall_END($OK);
   } ## end elsif ($undeploymode)
   elsif (defined($upgradepath))
   {
      baselog($LOG_DEBUG, "Nortel Applications upgrade started.", $TRUE);
      if (!(-e $CONFVER))
      {
         $LASTERR = "ANIN";
         return appinstall_END($ERROR);
      }
      $MNTPOINT       = $upgradepath;
      $INSTVER        = $MNTPOINT . "/" . $INSTCFG;
      $INSTALL_FOLDER = $MNTPOINT;
   } ## end elsif (defined($upgradepath...

   my $retval;

   # Set effective group ID to nortel
   # This is necessary to make sure all application stuff
   # can be read by nortel
   $) = $NORTEL_GID;

   if (!$testmode && $INTERACTIVE)
   {

      #mount the application delivery media
      my $mntdev = install_common::getMediaDev();

      # The US DoD requirement is to mount removable media with nosuid option.
      if (not do_system([$MOUNT, "-o", "nosuid", $mntdev, $MNTPOINT]))
      {

         #Can't mount media
         $LASTERR = "EMNT";
         return appinstall_END($ERROR);
      } ## end if (not do_system([$MOUNT...
      chdir($MNTPOINT);

      #check if installation files are in old format (iso burned to CD) or new format (iso or nai file copied to CD/CF)

      # Check if there are .nai files. If so, use one of them.
      my @appfiles = glob("*.nai");
      if (@appfiles)  # non-zero if at least one file is found
      {
         unless (set_install_source(@appfiles))
         {
            return appinstall_END($ERROR);
         }
      } ## end if (@appfiles)
      else
      {

         # Check if there are .iso files. If so, use one of them.
         @appfiles = glob("*.iso");
         if (@appfiles)  # non-zero if at least one file is found
         {
            unless (set_install_source(@appfiles))
            {
               return appinstall_END($ERROR);
            }
         } ## end if (@appfiles)
      } ## end else [ if (@appfiles)

      #check media files
      if (getYNResponse("Do you want to check the media?", 'N'))
      {
         if (!appinstall::check_cd::do_cdcheck())
         {
            return appinstall_END($ERROR);
         }
      } ## end if (getYNResponse("Do you want to check the media?"...

      #check media software version
      if (!appinstall::check_cd::do_swcheck())
      {
         return appinstall_END($ERROR);
      }
   } ## end if (!$testmode && $INTERACTIVE...

   $LASTERR = "NULL";
   $ENV{'APPINSTALLPATH'} = $INSTALL_FOLDER;

   #INSTALL SECTION
   {
      print_header("Start Application Installation");

      my $inst_dir    = "";
      my $license_dir = "";
      if (!$testmode && $INTERACTIVE)
      {
         $inst_dir    = $INSTVER;
         $license_dir = "$INSTALL_FOLDER/license/";
      }
      else
      {
         $inst_dir    = $INSTCFG;
         $license_dir = "license/";
      }

      # The following variables are declared as global because
      # they're used for order determination in all packages
      ($instnum, $order) = (0, 0);

      #
      # return information from the install.xml file
      # - if the RPMs are installed it will also make sure that
      #   they are removed as well (in case this is an upgrade)
      #
      ($instnum, $order) = appinstall::listgen::get_package();
      if (!($instnum && $order))
      {
         my $confver = use_confver($CONFVER);
         interrupt_installation() unless ((defined $confver) and ($confver->{'STATUS'} eq $CONFVER_STATES{'COMPLETE'}));
         last;
      } ## end if (!($instnum && $order...

      # remove Nortel users before installation/upgrade/re-install of the Nortel applications
      if (!$baseAppsmode)
      {
         base_accounts::del_users($NORTEL_USERS);
      }

      chdir $INSTALL_FOLDER;

      # Save install.xml from the application media
      # to /admin partition for upgrade purposes
      if (!do_system([$CP, $inst_dir, $ARCHVER]))
      {
         baselog($LOG_ERROR, "Could not copy $INSTCFG file.\n", $TRUE);
         $LASTERR = "INSF";
         return appinstall_END($ERROR);
      } ## end if (!do_system([$CP, $inst_dir...

      # Installation phase
      push @status_text, "                         Installation status                        \n\n";
      print_header("Installing RPM packages");
      $retval = install_iteration($instnum);
      print("RPM installation finished\n");
      if (!$retval)
      {
         $LASTERR = "RPMF";
         interrupt_installation();
         return appinstall_END($ERROR);
      } ## end if (!$retval)
      update_app_installation_status($CONFVER_STATES{'RPM_INSTALLED'}) unless ($baseAppsmode);

      # Copy license info to the target system
      if (-d $license_dir)
      {
         print("Copy licenses...\n");
         system($CP, '-rf', $license_dir, $LICENSAPP);
         if ($? >> 8)
         {
            baselog($LOG_ERROR, "Could not copy licenses.");
         }
      } ## end if (-d $license_dir)

      # Update applications' configure.xml files
      update_app_configure_xml();

      my $applist = [];  #list of the applications
      if ($baseAppsmode)
      {
         $applist = getBaseApplicationsList();
      }
      else
      {
         $applist = getNortelApplicationsList();
      }

      # add users defined in configure.xml
      baselog($LOG_INFO, "Adding user accounts...", $TRUE);
      my $user_type = $BASE_USERS;
      if (!$baseAppsmode)
      {
         $user_type = $NORTEL_USERS;
      }
      if (!base_accounts::add_users($user_type))
      {
         baselog($LOG_ERROR, "Could not add users. Installation will be stopped.", $TRUE);
         print "All applications will be uninstalled.\n";
         base_accounts::del_users($user_type);
         remove_app_RPMs();
         appinstall::appupgrade::application_cleanup();
         $LASTERR = "STOP";
         return appinstall_END($ERROR);
      } ## end if (!base_accounts::add_users...

      # pre-set FTP extended points.
      if (not base_ftp::prepare_ftp_homes($applist))
      {
         baselog($LOG_ERROR, "FTP setup failed.");
      }
      else
      {
         if (is_option_permitted($base_ftp::FTP) == $TRUE)
         {
            baselog($LOG_INFO, 'Activate mount points.');
            if (not base_ftp::activate_ftp_mounts())
            {
               baselog($LOG_ERROR, 'Cannot activate extended FTP access.');
            }
         } ## end if (is_option_permitted...
         my $added_users = base_accounts::get_added_usernames($user_type);
         if ($added_users)
         {
            if (!base_ftp::mount_sftp_jail_dirs($added_users))
            {
               baselog($LOG_ERROR, 'Cannot mount directories required for SFTP jail.', $TRUE);
            }
            if (!base_ftp::activate_sftp_mounts())
            {
               baselog($LOG_ERROR, 'Cannot activate extended SFTP access.', $TRUE);
            }
         } ## end if ($added_users)
         else
         {
            baselog($LOG_INFO, 'The list of added users is empty.', $TRUE);
         }
      } ## end else [ if (not base_ftp::prepare_ftp_homes...

      my $ptr;

      # Parse configure.xml files
      last if (!($ptr = appinstall::update_sys_data::update_sdb()));

      print_header("Configuration stage");

      # Perform applications' configuration
      print("Configure applications\n");
      if (!app_config($ptr, $applist))
      {
         baselog($LOG_ERROR, "'Configure' stage will not be performed.\n", $TRUE);
         print "All applications will be uninstalled.\n";
         remove_app_RPMs();
         appinstall::appupgrade::application_cleanup();
         $LASTERR = "STOP";
         return appinstall_END($ERROR);
      } ## end if (!app_config($ptr, ...
      print("Configuration stage finished\n");

      if ($INSTALL_TYPE eq "install")
      {
         if (-e $UPGRADE_RESTORE)
         {

            # Invoke recover scripts for first time appinstall
            print_header("Post-installation recovery stage");

            baselog($LOG_INFO, "... Post-installation recovery stage (baseAppsmode=$baseAppsmode)", $TRUE);

            if (!$baseAppsmode || (-e $BASERECOVERY_FLAG))
            {
               if (!do_system([$TAR, '-Pxzf', $UPGRADE_RESTORE], {$dsCHDIR => '/'}))
               {
                  baselog($LOG_ERROR, "Backup archive ($UPGRADE_RESTORE) files could not be extracted ", $TRUE);
               }
               else
               {
                  baselog($LOG_INFO, "Backup archive ($UPGRADE_RESTORE) files extracted", $TRUE);
               }

               # run the application backup recovery scripts
               recover($ptr, $applist);

               # Base Patch recovery from the patch backup archive (only during the Base Application installation
               # stage)
               if ($baseAppsmode)
               {
                  print_header("Post-installation Base Patch Recovery stage");

                  my $PATCHES_ARCHIVE = "$basebackup_dir/$patches_data_archive";
                  if (-e $PATCHES_ARCHIVE)
                  {
                     my $PATCHLOG      = "/admin/nortel/base/log/patchrecovery.log";
                     my $PATCHRECOVERY = "/opt/nortel/base/recovery/bin/patchrecovery.pl --recover";

                     # Now that the base applications are installed, attempt to install all recovered
                     # base patches.
                     # - All patching recovery must be done as "nortel" (we are currently running as "root")
                     # - The patching utilities all dump to the console, thus to ensure a "cleaner" looking
                     #   bootup screen, all output is piped into the "patchrecovery.log" file.
                     #
                     baselog($LOG_INFO, "Base Patch Recovery Started.", $TRUE);
                     if (!do_system([$RUNUSER, "-", $NORTEL_ACCT, "-c", "$PERL $PATCHRECOVERY"], {$dsOUTFILE => $PATCHLOG}))
                     {
                        baselog($LOG_ERROR, "Base Patch Recovery Failed (see $PATCHLOG).", $TRUE);
                     }
                     else
                     {
                        baselog($LOG_INFO, "Base Patch Recovery Completed.", $TRUE);
                     }
                  }  # if (-e $PATCHES_ARCHIVE)
                  else
                  {
                     baselog($LOG_INFO, "Base Patch Recovery not performed (no archive found).", $TRUE);
                  }  # if (-e $PATCHES_ARCHIVE)
               }  # if ($baseAppsmode)

               #
               # Remove the backup archives
               if (system("$RM -rf $BACKUPDIR/*") != 0)
               {
                  baselog($LOG_WARNING, "Cannot cleaned up $BACKUPDIR directory.");
               }
               else
               {
                  baselog($LOG_INFO, "Cleaned up $BACKUPDIR.");
               }

            } ## end if (!$baseAppsmode || ...

            # do not remove archive during Base Applications installation
            if (!$baseAppsmode)
            {
               if (!do_system([$RM, '-rf', $UPGRADE_RESTORE]))
               {
                  baselog($LOG_WARNING, "Cannot remove default upgrade restore archive.");
               }
            } ## end if (!$baseAppsmode)
            else
            {
               if (!do_system([$RM, '-rf', $BASERECOVERY_FLAG]))
               {
                  baselog($LOG_WARNING, "Cannot remove recovery flag.");
               }
               else
               {
                  baselog($LOG_INFO, "Removed recovery flag ($BASERECOVERY_FLAG).");
               }
            } ## end else [ if (!$baseAppsmode)
            print("Recovery stage finished\n");
         } ## end if (-e $UPGRADE_RESTORE...
         else
         {
            baselog($LOG_INFO, "Nothing to recover. Recovery stage skipped.", $TRUE);
         }
      } ## end if ($INSTALL_TYPE eq "install"...
      elsif ($INSTALL_TYPE eq "upgrade")
      {  # and invoke post-update scripts for upgrade
         print_header("Post-installation update stage");
         if (!postupdate($ptr, $applist))
         {
            print "'Post update' stage will not be performed. $CONTACT_MESSAGE\n";
         }
         else
         {
            print("Post update stage finished\n");
         }
      } ## end elsif ($INSTALL_TYPE eq "upgrade"...

      #if appinstall will be broken here, hardening items will be applied in next reboot.
      update_app_installation_status($CONFVER_STATES{'COMPLETE'}) unless ($baseAppsmode);

      # Reapply basic hardening items
      baselog($LOG_INFO, "Reapplying basic hardening... Please wait a moment (POST_APPINSTALL).", $TRUE);
      if (not do_basic_hardening_action($H_IF_POST_APPINSTALL))
      {
         baselog($LOG_ERROR, "Reapplying basic hardening failed (POST_APPINSTALL).");
      }

      # try to start Nortel Applications (for Nortel applications installation only)
      if (!$baseAppsmode)
      {
         print_header("Application start up stage");
         my $app = new base_apps_service({$APPL_TYPE => $TYPE_NORTEL_APPL});
         unless (defined $app)
         {
            baselog($LOG_ERROR, "Can not create base_apps_service object");
            print "'Appstartup' will not be perfomed. $CONTACT_MESSAGE\n";
            return appinstall_END($ERROR);
         } ## end unless (defined $app)
         else
         {
            unless ($app->start())
            {
               baselog($LOG_ERROR, "Applications can not be started");
               print "'Appstartup' will not be perfomed. $CONTACT_MESSAGE\n";
               undef $app;
               return appinstall_END($ERROR);
            } ## end unless ($app->start())
            undef $app;
         } ## end else
         print("Application start up finished\n");
      } ## end if (!$baseAppsmode)
   }
   return appinstall_END($OK);

   #End of installation section
} ## end sub main()

## @fn app_config($data, $applist)
# @brief This function runs app config scripts
# @param $data pointer to application data
# @param $applist reference to the list of the applications names
# @return $TRUE no errors
# @return $FALSE one of "config" scripts is absent or has wrong permissions
sub app_config($$)
{
   my ($data, $applist) = (@_);
   unless (defined($data))
   {
      baselog($LOG_ERROR, "Pointer to application data is undefined.");
      return $FALSE;
   }
   unless (defined($applist))
   {
      baselog($LOG_ERROR, "Applications list is undefined.");
      return $FALSE;
   }
   if (!check_scripts("config", $applist))
   {
      return $FALSE;
   }
   baselog($LOG_INFO, "Configuration stage:\n");
   push @status_text, "Configuration stage:\n";
   my @fail_scripts = run_script($data, "config");
   if (scalar(@fail_scripts))
   {
      push @status_text, "   Status: WARNING\n";
      push @status_text, "   There is(are) " . scalar(@fail_scripts) . " configuration script(s) that did not return ok.\n\n";
   }
   else
   {
      push @status_text, "   Status: OK\n\n";
   }
   baselog($LOG_INFO, "Configuration complete\n");
   return $TRUE;
} ## end sub app_config($$)

## @fn postupdate($data, $applist)
# @brief This function runs app postupdate scripts
# @param $data pointer to application data
# @param $applist reference to the list of the applications names
# @return $TRUE no errors
# @return $FALSE one of "postupdate" scripts is absent or has wrong permissions
sub postupdate($$)
{
   my ($data, $applist) = (@_);
   unless (defined($data))
   {
      baselog($LOG_ERROR, "Pointer to application data is undefined.");
      return $FALSE;
   }
   unless (defined($applist))
   {
      baselog($LOG_ERROR, "Applications list is undefined.");
      return $FALSE;
   }
   if (!check_scripts("postupdate", $applist))
   {
      return $FALSE;
   }
   baselog($LOG_INFO, "Postupdate stage:\n", $TRUE);
   push @status_text, "Postupdate stage:\n";
   my @fail_scripts = run_script($data, "postupdate");
   if (scalar(@fail_scripts))
   {
      push @status_text, "   Status: WARNING\n";
      push @status_text, "   There is(are) " . scalar(@fail_scripts) . " postupdate script(s) that did not return ok.\n\n";
   }
   else
   {
      push @status_text, "   Status: OK\n\n";
   }
   baselog($LOG_INFO, "Postupdate complete\n");
   return $TRUE;
} ## end sub postupdate($$)

## @fn recover($data, $applist)
# @brief This function runs app recover scripts
# @param $data pointer to application data
# @param $applist reference to the list of the applications names
# @return $TRUE no errors
# @return $FALSE one of "restore" scripts is absent or has wrong permissions
sub recover($$)
{
   my ($data, $applist) = (@_);
   unless (defined($data))
   {
      baselog($LOG_ERROR, "Pointer to application data is undefined.");
      return $FALSE;
   }
   unless (defined($applist))
   {
      baselog($LOG_ERROR, "Applications list is undefined.");
      return $FALSE;
   }
   if (!check_scripts("restore", $applist))
   {
      return $FALSE;
   }
   baselog($LOG_INFO, "Application Recovery stage:\n", $TRUE);
   push @status_text, "Recovery stage:\n";
   my @fail_scripts = run_script($data, "restore");
   if (scalar(@fail_scripts))
   {
      push @status_text, "   Status: WARNING\n";
      push @status_text, "   There is(are) " . scalar(@fail_scripts) . " recovery script(s)  that did not return ok.\n\n";
   }
   else
   {
      push @status_text, "   Status: OK\n\n";
   }
   baselog($LOG_INFO, "Application Recovery complete\n");
   return $TRUE;
} ## end sub recover($$)

#Function: run_script
#Description: This function run scripts
#Input: $_[0] - pointer to application data
#       $_[1] - script section name from configure.xml
#Output: None
sub run_script($$)
{
   my ($ptr, $script_name) = @_;
   my $config        = {};  #for checking configuration order
   my $tmpconfig     = 0;
   my @fail_scripts  = ();
   my $total_scripts = 0;

   foreach $tmpconfig (@{$ptr->{$script_name}})
   {
      if (!$config->{$tmpconfig->{'appl'}})
      {
         $config->{$tmpconfig->{'appl'}} = [];
      }
      push @{$config->{$tmpconfig->{'appl'}}}, [$tmpconfig->{'file'}, $tmpconfig->{'as'}];
   } ## end foreach $tmpconfig (@{$ptr->...

   foreach my $tmporder (@{$order})
   {
      my $ok_app_scripts   = 0;
      my $fail_app_scripts = 0;

      foreach my $cfgfile (@{$config->{$tmporder}})
      {
         my ($cfile, $cas) = @{$cfgfile};

         #Let execute this file:
         my @cmdarr = @{prepare_command_to_run_as(qq|APPINSTALLPATH=$INSTALL_FOLDER $cfile|, $cas)};
         $total_scripts++;
         if (scalar @cmdarr != 0)
         {
            my $cmd_retval = $OK;
            if (!$INTERACTIVE)
            {
               do_system(\@cmdarr, {$dsRETVAL => \$cmd_retval, $dsCHDIR => $dsHOMEDIR, $dsOUTMODE => '>>', $dsERRMODE => '>>', $dsERRFILE => $APPINSTALL_ERRFILE, $dsOUTFILE => $APPINSTALL_OUTFILE});
            }
            else
            {
               do_system(\@cmdarr, {$dsRETVAL => \$cmd_retval, $dsINTERACT => $TRUE, $dsCHDIR => $dsHOMEDIR});
            }
            if ($cmd_retval != $OK)
            {
               push @fail_scripts, $cfile;
               baselog($LOG_INFO, "FAILED\t @cmdarr (return: $cmd_retval)");
               $fail_app_scripts++;
            } ## end if ($cmd_retval != $OK...
            else
            {
               $ok_app_scripts++;
               baselog($LOG_INFO, "OK\t @cmdarr");
            }
         } ## end if (scalar @cmdarr != ...
         else
         {
            push @fail_scripts, $cfile;
            baselog($LOG_INFO, "FAILED\t (can not run $script_name script)");
            $fail_app_scripts++;
         } ## end else [ if (scalar @cmdarr != ...
      } ## end foreach my $cfgfile (@{$config...

      #generate status report text for an application.
      if (($ok_app_scripts > 0) or ($fail_app_scripts > 0))
      {
         my $app_status_text = '';
         $app_status_text = "   $tmporder:";

         if ($ok_app_scripts > 0)
         {
            $app_status_text .= "  $ok_app_scripts script(s) OK;";
         }

         if ($fail_app_scripts > 0)
         {
            $app_status_text .= "  $fail_app_scripts script(s) FAILED;";
         }

         push @status_text, "$app_status_text\n";
      } ## end if (($ok_app_scripts >...

   } ## end foreach my $tmporder (@{$order...
   push @status_text, "   -------------------------\n";
   push @status_text, "   Total scripts executed: $total_scripts\n";
   return @fail_scripts;
} ## end sub run_script($$)

## @fn       install_iteration($package)
# @brief     This function installs RPM packages
# @details
# @param     $package - pointer to sorted package list
# @return    $TRUE if installation pass
# @return    $FALSE if some error was occured
sub install_iteration($)
{
   my $package     = $_[0];
   my @cmd_install = ($RPM, "-ivh", "--replacepkgs", "--replacefiles", "--nosignature");
   my @rpms        = ();
   my $ret         = $OK;

   foreach my $appl (@{$order})
   {
      foreach my $rpmfile (@{$package->{$appl}})
      {
         push @rpms, $rpmfile;
      }
   } ## end foreach my $appl (@{$order}...
   @cmd_install = (@cmd_install, @rpms);
   if ($INTERACTIVE)
   {
      do_system(\@cmd_install, {$dsRETVAL => \$ret, $dsINTERACT => $TRUE});
   }
   else
   {

      # do a redirection into two logfiles for testing purposes in case of deployment installation
      do_system(\@cmd_install, {$dsOUTMODE => '>>', $dsERRMODE => '>>', $dsERRFILE => $APPINSTALL_ERRFILE, $dsOUTFILE => $APPINSTALL_OUTFILE, $dsRETVAL => \$ret});
   } ## end else [ if ($INTERACTIVE)
   if ($ret == $OK)
   {
      baselog($LOG_INFO, "Instaled rpms: " . join(" ", @rpms) . ".");
      foreach my $rpmfile (@rpms)
      {
         if (!copy($rpmfile, $RPMBACKUP))
         {
            baselog($LOG_ERROR, "Could not backup the $rpmfile\n", $TRUE);
            if ($INTERACTIVE)
            {
               if (getYNResponse("Do you want to cancel the installation", 'Y'))
               {
                  ## remove installed rpms and exit
                  print "Installation is interrupted.\n";
                  interrupt_installation(\@rpms);
                  push @status_text, "Status: INTERRUPTED at rpm installation stage.\n";
                  return $FALSE;
               } ## end if (getYNResponse("Do you want to cancel the installation"...
            } ## end if ($INTERACTIVE)
            else
            {
               interrupt_installation(\@rpms);
               push @status_text, "Instalation is interrupted.\n";
               return $FALSE;
            } ## end else [ if ($INTERACTIVE)
         } ## end if (!copy($rpmfile, $RPMBACKUP...
      } ## end foreach my $rpmfile (@rpms)
   } ## end if ($ret == $OK)
   else
   {
      baselog($LOG_ERROR, "Instalation of applications rpms FAILED");
      print "Instalation of applications rpms FAILED.\n";
      interrupt_installation(\@rpms);
      push @status_text, "Installation of rpms failed.\n";
      if ($INTERACTIVE)
      {
         print "press ENTER to continue";
         <STDIN>;
      }
      return $FALSE;
   } ## end else [ if ($ret == $OK)

   baselog($LOG_INFO, "Total installed RPMs: " . scalar(@rpms) . ".");
   push @status_text, "Installing RPM packages stage:\n";
   push @status_text, "   Total installed RPMs: " . scalar(@rpms) . "\n";
   push @status_text, "   Status: OK\n\n";

   #changing ownership of appl files to nortel:nortel
   foreach my $appl (@{$order})
   {
      if (-d $APP_BIN_ROOT . "/" . $appl)
      {
         unless (do_system([$CHOWN, '-R', "$NORTEL_ACCT:$NORTEL_ACCT", "$APP_BIN_ROOT/$appl"], {$dsRETVAL => \$ret}))
         {
            baselog($LOG_ERROR, "Cannot change owner to nortel for '$APP_BIN_ROOT/$appl'.");
         }
      } ## end if (-d $APP_BIN_ROOT ....
      if (-d $APP_CFG_ROOT . "/" . $appl)
      {
         unless (do_system([$CHOWN, '-R', "$NORTEL_ACCT:$NORTEL_ACCT", "$APP_CFG_ROOT/$appl"], {$dsRETVAL => \$ret}))
         {
            baselog($LOG_ERROR, "Cannot change owner to nortel for '$APP_BIN_ROOT/$appl'.");
         }
      } ## end if (-d $APP_CFG_ROOT ....
   } ## end foreach my $appl (@{$order}...
   return $TRUE;
} ## end sub install_iteration($)

## @fn       interrupt_installation($rpms)
# @brief     This function uninstalls RPMs packages
# @details   This function removes $ARCHVER and $CONFVER files
#            and if RPMs were installed uninstalls them
# @param     $rpms - reference to array with RPMs.
# @return    none
sub interrupt_installation(;$)
{
   my $rpms = shift @_;

   if (defined $rpms)
   {
      unless (uninstall_rpms(@{$rpms}))
      {
         baselog($LOG_ERROR, "Cannot uninstall rpms: " . join(" ", @{$rpms}) . "\n", $TRUE);
      }
      baselog($LOG_ERROR, "Cannot remove '$RPMBACKUP' tree.\n") unless (rmtree $RPMBACKUP);
   } ## end if (defined $rpms)
   if (-f $ARCHVER)
   {
      baselog($LOG_ERROR, "Cannot delete '$ARCHVER' file.\n") unless (unlink $ARCHVER);
   }
   if (-f $CONFVER)
   {
      baselog($LOG_ERROR, "Cannot delete '$CONFVER' file.\n") unless (unlink $CONFVER);
   }
} ## end sub interrupt_installation(;$)

#########################################################
# This sub updates apps configure.xml files if necessary
# It adds "version" attribute to application
# "configure.xml" files if it does not exist
# The "version" will be - the application media version
# Input:  none
# Output: none
#########################################################
sub update_app_configure_xml()
{
   baselog($LOG_INFO, "Updating configure.xml files:\n");
   my $def_ver = "5.00.00";

   #Define Linux Base release version
   my $parser = ifxml::new($ARCHDIR, $INSTCFG);
   if (!$parser)
   {
      baselog($LOG_ERROR, "Can not find file $INSTCFG\n");
      baselog($LOG_ERROR, "Using default one $def_ver\n");
   }
   else
   {
      my $entries = $parser->get_base();
      my $ver     = @$entries[0]->{'version'};
      if ($ver =~ /(\d+\.\d+\.\d+)/ || $ver =~ /(\d+\.\d+\.[A-Z])/)
      {
         $def_ver = $1;
      }
      else
      {
         baselog($LOG_ERROR, qq{Version ($ver) does not match format});
      }
      undef $parser;
   } ## end else [ if (!$parser)

   foreach my $app (@{$order})
   {
      unless (configure_xml_update($app, $def_ver))
      {
         baselog($LOG_ERROR, qq{Can not modify configure.xml file for $app application});
      }
   } ## end foreach my $app (@{$order})
   baselog($LOG_INFO, "Updating configure.xml complete\n");
} ## end sub update_app_configure_xml()

#####################################################
#Function: remove_app_RPMs
#Description: This function uninstalls all applications RPM packages
#Input: none
#Output: none
######################################################
sub remove_app_RPMs()
{
   my $appl     = 0;
   my @revorder = reverse @{$order};

   foreach $appl (@revorder)
   {
      uninstall_rpms(@{$instnum->{$appl}});
   }
   print "RPMs uninstallation complete\n";
} ## end sub remove_app_RPMs()

