#!/usr/bin/perl
## @file    baseSysinfo
# @brief    This is a generic API to get some information about system.
# @author   Kate Bereznyakova
# @date     May 2008
# @version  1.00 - Initial version
# @version  1.01 - Two colons were removed before shared functions and variables.


=pod

=head1 NAME

B<baseSysinfo> - tool to get LinuxBase system information.

=head1 SYNOPSIS

=over 4

=item B<baseSysinfo> B<backup>

=back

=head1 DESCRIPTION

B<baseSysinfo> tool is used to get LinuxBase system information.

=head1 OPTIONS

=over 4

=item B<backup>

It detects if available system backup data and prints an information about it.

=back

=head1 EXAMPLES

=over 4

=item B<baseSysinfo> B<backup>

APP_INSTALLED=The Primary ECM Server (install NRS and the primary ECM security service)

APP_VERSION=5.50.12

=back

=cut

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

use strict;
use warnings;
use common_variables;
use baselog;
use recovery::backup;

## @skipline
sub main();

## @skipline
sub show_backup_info(;$);

my %hash_modules = ($SHOW_BACKUP_INFO => \&show_backup_info);

exit(main());

###############################################################################
## @fn main()
# @brief     Main function. It detects which functionality was requiested and
#            executes appropriate function.
# @return    $OK(0)  - operation successful
# @return    $ERROR(1)  - some error happens
# @author    Kate Bereznyakova
# @date      May 2008
sub main()
{
   my $retval = $ERROR;

   if (!defined(@ARGV))
   {
      baselog($LOG_ERROR, 'Required parameter is missing.');
   }
   else
   {
      my $parameters = join(',', @ARGV);
      baselog($LOG_DEBUG, "Incoming parameters are: $parameters.");

      my $func = shift @ARGV;
      if (exists $hash_modules{$func})
      {
         $retval = $hash_modules{$func}->(@ARGV);
      }
      else
      {
         baselog($LOG_ERROR, "Incorrect parameter " . $func . " is provided.");
      }
   } ## end else [ if (!defined(@ARGV))
   return $retval;
} ## end sub main()

###############################################################################
## @fn show_backup_info($file)
# @brief     This function checks if there is system upgrade data present on
#            the system or not and prints backup information.
# @param     $file - Output file where to save information
# @return    $OK(0)  - if there is no backup/upgrade data or if backup data
#                      successfully retrieved from backup archive.
# @return    $ERROR(1)  - if it fails to get backup/upgrade data information
# @author    Kate Bereznyakova
# @date      May 2008
sub show_backup_info(;$)
{
   my $retval = $ERROR;
   my $file   = shift @_;
   baselog($LOG_INFO, 'show_backup_info is invoked.');
   if (!is_restore_exists())
   {
      $retval = $OK;
   }
   else
   {
      my ($app_config, $app_ver) = getBackupConfiguration($UPGRADE_RESTORE);
      if ($app_config and $app_ver)
      {
         baselog($LOG_INFO, "Restore data has been found: '$app_config [$app_ver]'.");
         if ((defined $file) and ($file ne ''))
         {
            baselog($LOG_DEBUG, "File name is defined ('$file').");
            my $cfg;
            unless (open($cfg, '>', $file))
            {
               baselog($LOG_ERROR, "Cannot access '$file' file.");
            }
            else
            {
               print $cfg "APP_INSTALLED=$app_config\n";
               print $cfg "APP_VERSION=$app_ver\n";
               if (!close $cfg)
               {
                  baselog($LOG_ERROR, "Cannot save base information to '$file' file.");
               }
               else
               {
                  $retval = $OK;
               }
            } ## end else
         } ## end if ((defined $file) and...
         else
         {
            print "APP_INSTALLED=$app_config\n";
            print "APP_VERSION=$app_ver\n";
            $retval = $OK;
         } ## end else [ if ((defined $file) and...

      } ## end if ($app_config and $app_ver...
      else
      {
         baselog($LOG_ERROR, 'Cannot get information about backed up data.');
      }
   } ## end else [ if (!is_restore_exists...
   return $retval;
} ## end sub show_backup_info(;$)
