#!/usr/bin/perl
##############################################################
# Description: This script performs the backup, asks a user to
#              insert the Base media with  the LinuxBase release
#              and reboots the machine.
# Author:
# Vlad Ivanov
#
# History of changes:
#   1.03 - Two colons were removed before shared functions and variables.
#   1.02 - POD section was added; the usage is displayed by the
#          common function - print_pod_usage() now.
#   1.01 - usage of 'su' command is replaced by invoking
#          prepare_command_to_run_as subroutine.
#   1.00 - Initial version
##############################################################

=pod

=head1 NAME

B<upgrade> - tool to perform upgrade 

=head1 SYNOPSIS

B<upgrade> [I<--help>]

=head1 DESCRIPTION

=over 4

B<upgrade> tool performs backup, asks a user to insert Base media with the LinuxBase release (the "Insert Media" message) and reboots the machine.

=back

=head1 OPTIONS

=over 4

=item B<--help>

Print this help message and exit.

=back

=head1 EXAMPLES

$ B<upgrade>
   You have to have the root privilege to execute this command.
   Password for root:

   This tool will perform Linux Base upgrade. Before the upgrade
   it will back up all data.

   Do you want to continue upgrade? (Y/N) [N]? y

   System data will be saved at /admin partition.
   Please use option "Re-use /admin partition" during Linux Base installation.

   Do you want to backup data to external source (USB/SFTP) as well? (Y/N) [Y]? n
   Backup started. Please wait...
   ...
   Backup complete.

   Please insert Linux Base Media for upgrade, then press ENTER key (a reboot will occur) 

   The system is going down for reboot NOW!

=head1 CAVEATS

B<upgrade> can be run only under Nortel authorized accounts.
The system reboot will be required to start the upgrade process.

=cut

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

use warnings;
use strict;

use common_variables;
use common_functions;
use CommonInstallUtils;
use baselog;
use base_system;

sub main;

exit(main());

sub main
{
   if (warning_if_not_root_uid())
   {
      return $ERROR;
   }

   if (defined $ARGV[0] and $ARGV[0] =~ /help$/)
   {
      print_pod_usage();
      return $OK;
   }
   my $warning = "\nThis tool will perform Linux Base upgrade. Before the upgrade\nit will back up all data.\n\nDo you want to continue upgrade?";
   return $OK if (!getYNResponse($warning, "N"));

   $warning =
       "\nSystem data will be saved at /admin partition.\n"
     . "Please use option \"Re-use /admin partition\" during Linux Base installation.\n\n"
     . "Do you want to backup data to external source (USB/SFTP) as well?";

   my $ret = $ERROR;

   my $command = "$SYSBACKUP";
   if (getYNResponse($warning, "Y"))
   {
      $command .= " -b";
   }
   $command .= " --upgrade";

   my @cmdarr = @{prepare_command_to_run_as($command, $NORTEL_ACCT)};
   if (scalar @cmdarr != 0)
   {
      baselog($LOG_INFO, "@cmdarr");
      if (not do_system(\@cmdarr, {$dsINTERACT => $TRUE}))
      {
         baselog($LOG_ERROR, "$SYSBACKUP failed");
         return $ERROR;
      }
   } ## end if (scalar @cmdarr != ...
   else
   {
      baselog($LOG_ERROR, "$SYSBACKUP can not be executed.");
      return $ERROR;
   }

   print "\nPlease insert Linux Base Media for upgrade, then press ENTER key (a reboot will occur) \n";
   <STDIN>;

   do_system($REBOOT);
   return $OK;
} ## end sub main
