#!/usr/bin/perl
## @file keycodeValidate
# @brief  This API checks keycode against provided target.
# @details  It gets keycode file, application to deploy and target parameters
#           to make a validation. The file is transferred to the remote target
#           and remote "kcv" tool is used to validate the keycode.
# @param hostname -  target hostname to deploy.
# @param IP - ip-address of the target host
# @param keycode - full path to the keycode file.
# @param appver - application load version. Only first two fractions are
#                 required.
# @return ERROR_CODE = 10 - on any  error found in this API
# @return $rtvcmd - value returned by "kcv" utility, if it was called
#                   successfully
# @author Kate Bereznyakova
# @date May 2008
# @version 1.00 - Initial version
# @version 1.01 - Two colons were removed before shared functions and variables.
# @version 1.02 - keycode file is transferred to a temporary directory on the
#                 target for kcv.

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

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

my $ERROR_CODE = 10;
my $KEYCODETMPPATH = "/tmp/deployment";

## @skipline
sub keycodeValidate();
## @skipline
exit(keycodeValidate());

## @fn keycodeValidate()
# @brief   Main function. It checks input parameters and implements general
#          logic.
# @param   $hostname - target hostname to deploy
# @param   IP - ip-address of the target host
# @param   keycode - full path to keycode file.
# @param   appver - application load version.
# @return  $retval - return code from kcv utility. Default error value is "10";
#          Default error code is set to "10" to avoid overlapping with kcv
#          codes.
# @author  Kate Bereznyakova
# @date    May 2008
sub keycodeValidate()
{
   my $hostname;
   my $ip;
   my $keycode;
   my $appver;

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

   my $retval = $ERROR_CODE;

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

   my $cfg_file = get_deploy_cfg_path($hostname);

   if ((!defined $ip) or ($ip eq ''))
   {
      baselog($LOG_ERROR, 'TLAN is undefined.');
      update_section($cfg_file, "/target/error", 'UNPA');
      return $retval;
   } ## end if ((!defined $ip) or ...

   if (!validateIP($ip))
   {
      baselog($LOG_ERROR, "Invalid TLAN IP data: $ip.");
      update_section($cfg_file, "/target/error", 'INVIP');
      return $retval;
   } ## end if (!validateIP($ip))

   if ((!defined $keycode) or ($keycode eq ''))
   {
      baselog($LOG_ERROR, 'Keycode file is undefined.');
      update_section($cfg_file, "/target/error", 'UNKCF');
      return $retval;
   } ## end if ((!defined $keycode...
   if (!-e $keycode)
   {
      baselog($LOG_ERROR, "Keycode file ('$keycode') does not exist.");
      update_section($cfg_file, "/target/error", 'INPF');
      return $retval;
   } ## end if (!-e $keycode)

   if ((!defined $appver) or ($appver eq ''))
   {
      baselog($LOG_ERROR, 'Application version is undefined.');
      update_section($cfg_file, "/target/error", 'UNPA');
      return $retval;
   } ## end if ((!defined $appver)...

   # end: check input parameters section.

   baselog($LOG_INFO, "keycode validation is called with the following params: hostname = '$hostname', ip ='$ip', keycode='$keycode', appver='$appver'.");

   my $deployMng = base_deploy::new($hostname, $ip);
   if (!defined $deployMng)
   {
      baselog($LOG_ERROR, 'Cannot create deploy object.');
      update_section($cfg_file, "/target/error", 'PREP');
      return $retval;
   } ## end if (!defined $deployMng...

   #Make sure that target directory exists.
   my $rmtMkTmpDirCmd = $deployMng->call_remote_cmd("$MKDIR -p $KEYCODETMPPATH");
   if ((!defined $rmtMkTmpDirCmd) or ($rmtMkTmpDirCmd != $OK))
   {
      baselog($LOG_ERROR, 'Cannot create a temp directory for keycode validation on the remote host.');
      update_section($cfg_file, "/target/error", 'CRDF');
   }
   else
   {

      # remote target directory must exist
      if (!$deployMng->transfer_file($keycode, $KEYCODETMPPATH))
      {
         baselog($LOG_ERROR, "Cannot copy keycode file '$keycode' to the target.");
         update_section($cfg_file, "/target/error", 'TRNSF');
      }
      else
      {
         baselog($LOG_INFO, "Keycode file '$keycode' is copied to the target.");

         my $rmt_keycode = basename($keycode);
         $rmt_keycode = "$KEYCODETMPPATH/$rmt_keycode";

         baselog($LOG_INFO, "Calling kcv with '$rmt_keycode' keycode ...");
         my $rtvcmd = $deployMng->call_remote_cmd("$KCV $appver $rmt_keycode");
         if (!defined $rtvcmd)
         {
            update_section($cfg_file, "/target/error", 'RSWF');
            baselog($LOG_ERROR, 'Failed to call kcv.');
         }
         else
         {
            baselog($LOG_INFO, "kcv returned '$rtvcmd'.");
            $retval = $rtvcmd;
         }
      } ## end else [ if (!$deployMng->transfer_file...
   } ## end else [ if (!$deployMng->prepare_file_transfer...
   undef $deployMng;

   return $retval;
} ## end sub keycodeValidate()
