#!/usr/bin/perl
## @file appUndeploy
# @brief It undeploys installed nortel applications.
# @details  Successful operation returns 0, otherwise 1 will be returned.
# @param hostname - target host name to undeploy (key parameter to find the data
#                    related to the undeployment operation).
# @param IP - ip-address of the target host
# @author Kate Bazhanova
# @date May 2008
# @version 1.00 - Initial version
# @version 1.01 - Two colons were removed before shared functions and variables.
# @version 1.10 - Undeploy operation status update is changed.
# @version 1.11 - Undeploy operation status update is changed (update status before each return command).
# @version 1.12 - Updated error section checking
# @version 1.13 - Updated log records with hostname and IP information.
# @version 1.15 - Get deployment report/logs step is added.
#                 "/error" and "errorMsg" replaced with variables.

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

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

## @skipline
sub appUndeploy();

exit(appUndeploy());

###############################################################################
## @fn appUndeploy()
# @brief   Main function. It checks input parameters and implements general
#          logic.
# @details None.
# @param   $hostname - target hostname to deploy
# @param   IP - ip-address of the target host
# @return  $OK (0)    -  undeployment succeed
# @return  $ERROR (1) -  undeployment failed
# @author  Kate Bazhanova
# @date    May 2008

sub appUndeploy()
{
   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;
   }

   my $cfg_file = get_deploy_cfg_path($hostname);

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

   baselog($LOG_INFO, "$hostname, $ip. Starting undeploy operation.");

   if (!validateIP($ip))
   {
      baselog($LOG_ERROR, "Invalid TLAN IP data: $ip.");
      update_section($cfg_file, $DEPERRORPATH, 'INVIP');
      reset_status($cfg_file);
      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.');
      update_section($cfg_file, $DEPERRORPATH,    'PREP');
      update_section($cfg_file, $DEPERRORMSGPATH, 'Cannot create deploy object.');
      reset_status($cfg_file);
      return $ERROR;
   } ## end if (!defined $deployMng...

   #log directory exists. Need to remove old logs first.
   $deployMng->cleanup_logs_dir();

   if (!$deployMng->update_deploy_status('Uninstallation is in progress'))
   {
      baselog($LOG_ERROR, 'Cannot update status in the deployment.xml file.');
   }

   my $deploy_cmd = "$RMT_UNDEPLOY_CMD -hostname $hostname";

   baselog($LOG_DEBUG, "Remote command is '$deploy_cmd'.");
   baselog($LOG_INFO,  "Uploading 'deployment.xml' file to the target host.");
   if (!$deployMng->transfer_deploy_xml())
   {
      baselog($LOG_ERROR, "Fail to upload 'deployment.xml' file to the target host.");
      $retval = $ERROR;
   }
   else
   {
      baselog($LOG_INFO, "'deployment.xml' file successfully uploaded to the target host.");
   }

   my $deploy_call = $deployMng->call_remote_cmd($deploy_cmd);
   if ((!defined $deploy_call) or ($deploy_call ne $OK))
   {

      # to avoid loosing error in case of local operation
      # or previous error, check the tag before updating
      # if operation local and tag is empty then it is possible
      # that corresponding script was not triggered as well.
      my $temp_error = get_error_code($deployMng->{'deploymentCfg'});
      if ((defined $temp_error) and ($temp_error eq ''))
      {
         update_section($cfg_file, $DEPERRORPATH, 'RSWF');
      }

      baselog($LOG_ERROR, 'Undeployment failed.');
      $retval = $ERROR;
   } ## end if ((!defined $deploy_call...
   else
   {
      $retval = $OK;
   }

   if (!$deployMng->retrieve_deploy_xml())
   {
      baselog($LOG_ERROR, 'Fail to get remote deployment.xml file.');
      update_section($cfg_file, $DEPERRORMSGPATH, 'Cannot synchronize status.');
      $retval = $ERROR;
   } ## end if (!$deployMng->retrieve_deploy_xml...
   $deployMng->get_deploy_logs();
   undef $deployMng;

   # check if operation status set correctly
   reset_status($cfg_file);

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