#!/usr/bin/perl
## @file    rmtHostUndeploy
# @brief    Remote host undeploy
# @details  Successful operation returns 0, otherwise 1 will be returned.
# @param    -hostname key - indicates that the next param is target host name
# @param    Target hostname to undeploy
# @author   Ekaterina Bazhanova
# @date     April 2008
# @version  1.00 - Initial version
# @version 1.01 - Two colons were removed before shared functions and variables.
# @version 1.10 - deployment status sync is updated.
# @version 1.11 - End section is updated to keep main function return code.
# @version 1.12 - signals_ignore() usage added to ignore connection lost on the target side
# @note

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

use strict;
use warnings;
use base_deploy;
use base_system;
use appinstall::errors;
use Getopt::Long;
use baselog;
use common_variables;
use common_functions;

## @skipline
sub rmtHostUndeploy();

exit(rmtHostUndeploy());

sub END
{

   # end section should not change actual return value
   # got from main function.
   my $return_val = $?;

   # re-sync deployment parameters with baseOs.properties file
   # update current deployment.xml file with accurate data
   if (!base_deploy::op_stat_sync())
   {
      baselog($LOG_WARNING, "Failed to sync deployment status data.");
   }

   $? = $return_val;

} ## end sub END

###############################################################################
## @fn rmtHostUndeploy
# @brief     Main function.
# @details   This function invokes "appinstall -undeploy"
#            If appinstall is finished successfully, then deployment.xml and
#            baseOs.properties files
#            are updated with "Deployed" status and returns $OK.
# @return    $OK(0)  - if appinstall was finished successfully
# @return    $ERROR(1)  - if appinstall was not finished successfully
# @author    Ekaterina Bazhanova
# @date      April 2008

sub rmtHostUndeploy()
{
   baselog($LOG_INFO, 'Blocking signals.');
   signals_ignore();

   my $appinstall_out;
   my $error_abbr;
   my $retval = $OK;

   #   script options
   my $hostname = '';

   baselog($LOG_INFO, "rmtHostUndeploy is started with the following options: @ARGV");
   GetOptions("hostname=s" => \$hostname);
   if ((!defined $hostname) or ($hostname eq ''))
   {
      baselog($LOG_ERROR, 'Hostname is undefined.');
      return $ERROR;
   }

   my $cfg_file = get_deploy_cfg_path($hostname);

   #invoke "appinstall -undeploy"
   if (do_system([$SUDO, $SUDO_NORTEL, $APP_DEPLOY, '-undeploy'], {$dsRETVAL => \$appinstall_out}))
   {
      baselog($LOG_INFO, 'Applications were uninstalled.');
   }
   else
   {
      baselog($LOG_ERROR, 'Cannot uninstall applications.');
      $retval = $ERROR;
   }

   # updating deployment status in baseOS.properties file
   change_deploy_status($hostname);

   #checking error code from appinstall
   $error_abbr = appinstall::errors::error_decoder($appinstall_out);
   unless ($error_abbr)
   {
      baselog($LOG_ERROR, 'Appinstall error code is undefined');
      $retval = $ERROR;
   }

   $error_abbr = '' if ($error_abbr eq 'NULL');
   if (!update_section($cfg_file, "/target/error", $error_abbr))
   {
      baselog($LOG_ERROR, "Failed to update error section with new value ($error_abbr) for '$hostname' host.");
      $retval = $ERROR;
   }
   return $retval;
} ## end sub rmtHostUndeploy()
