#!/usr/bin/perl
## @file    appHostBackupInfoGet
# @brief    It calls remote API that checks if there is an upgrade/backup data
#           on the target.
# @author   Kate Bereznyakova
# @date     April 2008
# @version  1.00 - Initial version
# @version  1.01 - Two colons were removed before shared functions and variables.
# @version  1.02 - change appHostBackupInfoGet function to call $TEST command in NOERROR mode.
# @version  1.03 - Updated log records with hostname and IP information.

=pod

=head1 NAME

B<appHostBackupInfoGet> - API to get system backup information from remote host.

=head1 SYNOPSIS

=over 4

=item B<appHostBackupInfoGet> B<-hostname> I<hostname> B<-IP> I<TLAN ip>

=back

=head1 DESCRIPTION

B<appHostBackupInfoGet> this API requests remote host about available system 
backup  data. If remote host has backup data then information about it 
will be saved to "installedconfig" file under appropriate deployed/<host>
directory.

=head1 OPTIONS

=over 4

=item B<-hostname> I<hostname>

Hostname of the target - equal to FQDN.

=item B<-IP> I<TLAN ip>

IP address of the target host.

=back

=cut

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

use strict;
use warnings;
use base_deploy;
use common_variables;
use baselog;
use Getopt::Long;
use CommonInstallUtils;

## @skipline
sub appHostBackupInfoGet();

exit(appHostBackupInfoGet());

###############################################################################
## @fn  appHostBackupInfoGet()
# @brief     Main function.
# @return    $OK(0)  - if there is no backup/upgrade data
# @return    $ERROR(1)  - if it fails to get backup/upgrade data information
# @author    Kate Bereznyakova
# @date      April 2008
sub appHostBackupInfoGet()
{
   my $hostname = undef;
   my $ip       = undef;

   GetOptions("hostname:s" => \$hostname,
              "IP:s"       => \$ip);

   my $retval = $OK;

   # check input parameters section.
   if ((!defined $hostname) or ($hostname eq ''))
   {
      baselog($LOG_ERROR, 'Hostname is undefined.');
      return $ERROR;
   }

   if ((!defined $ip) or ($ip eq ''))
   {
      baselog($LOG_ERROR, 'TLAN is undefined.');
      return $ERROR;
   } ## end if ((!defined $ip) or ...

   baselog($LOG_INFO, "$hostname, $ip. Starting check of upgrade/backup data on the target.");

   if (!validateIP($ip))
   {
      baselog($LOG_ERROR, "Invalid TLAN IP data: $ip.");
      return $ERROR;
   } ## end if (!validateIP($ip))

   # end: check input parameters section.

   my $deployMng = base_deploy::new($hostname, $ip);
   if (!defined $deployMng)
   {
      baselog($LOG_ERROR, 'Cannot create deploy object.');
      return $ERROR;
   } ## end if (!defined $deployMng...

   my $backupinfo = "$deployMng->{'deploymentHome'}/$INSTALLEDCONFIG";
   if (-e $backupinfo)
   {
      baselog($LOG_INFO, "Old backup information has been found. Removing '$backupinfo'.");
      if (!unlink($backupinfo))
      {
         baselog($LOG_ERROR, 'Cannot remove old backup info.');
         $retval = $ERROR;
      } ## end if (!unlink($backupinfo...
   } ## end if (-e $backupinfo)

   if ($retval == $OK)
   {
      baselog($LOG_INFO, 'Retrieving backup information.');
      my $make_dir = $deployMng->call_remote_cmd("$MKDIR -p $deployMng->{'deploymentHome'}");
      if ((defined $make_dir) and ($make_dir eq $OK))
      {
         baselog($LOG_INFO, 'Directory for backup archive has been created.');
         if ($deployMng->call_remote_cmd("$BASESYSINFO $SHOW_BACKUP_INFO $backupinfo") != $OK)
         {
            baselog($LOG_ERROR, 'Cannot get remote backup info.');
            $retval = $ERROR;
         } ## end if ($deployMng->call_remote_cmd...
         else
         {
            my $cmd = $deployMng->call_remote_cmd("$TEST -e '$backupinfo'", undef, $noERROR);
            if (!defined $cmd)
            {
               baselog($LOG_ERROR, 'Cannot get info about remote backup file.');
               $retval = $ERROR;
            } ## end if (!defined $cmd)
            elsif ($cmd eq $OK)
            {
               if (!$deployMng->transfer_file($backupinfo, $backupinfo, $FALSE))
               {
                  baselog($LOG_ERROR, "Cannot get '$backupinfo' file.");
                  $retval = $::ERROR;
               }
               else
               {
                  $retval = $OK;
               }
               my $is_local = $deployMng->is_local_deploy();
               if ((defined $is_local) and ($is_local eq $FALSE))
               {
                  my $cmd = $deployMng->call_remote_cmd("$RM -rf $backupinfo");
                  if ((!defined $cmd) or ($cmd eq $ERROR))
                  {
                     baselog($LOG_ERROR, "Cannot remove backup info data from target host");
                  }
               } ## end if ((defined $is_local...
            } ## end elsif ($cmd eq $OK)
            else
            {
               $retval = $OK;
            }
         } ## end else [ if ($deployMng->call_remote_cmd...
      } ## end if ((defined $make_dir...
      else
      {
         baselog($LOG_ERROR, "Cannot create directory for backup info.");
         $retval = $ERROR;
      } ## end else [ if ((defined $make_dir...
   } ## end if ($retval == $OK)
   undef $deployMng;
   return $retval;
} ## end sub appHostBackupInfoGet()

