#!/bin/sh
# OPC_WHAT_STRING="@(#)HP OpenView Operations 09.01.180 (08/20/09)"

################################################################################
#
# File:         opcping
# Description:  generic ALIVE_CHECK for unix family
# Language:     Bourne Shell
# Package:      HP OpenView OperationsCenter
#
# (c) Copyright 1993 - 2009 Hewlett-Packard Development Company, L.P. 
#
################################################################################

#
#  DESCRIPTION
#    Generic unix family ALIVE_CHECK script.
#    This script checks if $OPC_NODE is reachable.
#    Following is list of all actions:
#
#      1. check if OPC_NODE is reachable via ping utility.
#         check if all send packagages are echoed back.
#
#      2. check if ITO control agent is alive
#
#      3. check password of OPC_NODE OPC_INST_USER .
#         This does unix common function: opcuxpwget (see opcuxpwget(1m))
#         Parameters :
#         Output environment variables:
#           OPC_PASSWD_REQ OPC_PASSWD
#         Return code:
#           0 .. OK
#           1 .. ERROR in password 
#         If return code equals 1, display error and skip this node.
#
#      4. check time difference between Management Server and Managed Node,
##        if OPC_METHOD equals INSTALL
#
#
#  PARAMETERS
#    output_file .. name of temporary file where line of password_file(5) for
#                   actual node will be placed
#
#  EXIT_CODE
#    0 .. ok, node is reachable via ping utility
#    1 .. error, node is not reachable
#

BIN_DIR=/usr/bin
OS_DIR=/usr
OPT_DIR=/opt
VAR_DIR=/var/opt
ETC_DIR=/etc/opt

# localization
LANG_C=C
LANG_SJIS=ja_JP.SJIS
LANG_EUC=ja_JP.eucJP

#
# nslookup function - nslookup does not use LDAP
#
nslookup()
{
    _name=$1
    _result=""
    if [ -x ${OPCSVR_BIN_DIR}/utils/opcsvnsl ]
    then
        _result=`${OPCSVR_BIN_DIR}/utils/opcsvnsl ${_name} 2>&1`
    else
        if [ -x ${OPCAGT_INST_DIR}/opcnsl ]
        then
            _result=`${OPCAGT_INST_DIR}/opcnsl ${_name} 2>&1`
        else
            if [ -x /tmp/opcsvnsl ]
            then
                _result=`/tmp/opcsvnsl ${_name} 2>&1`
            else
                /usr/bin/nslookup ${_name}
            fi
       fi
   fi
   if [ ! -z "${_result}" ]
   then
        echo ${_result} | grep 'Not found' 1>/dev/null 2>&1
        if [ $? -eq 0 ]
        then
            echo
            echo "*** No address (A) records available for ${_name}"
        else
            _rname=`echo ${_result} | cut -f1 -d:`
            _raddr=`echo ${_result} | cut -f2 -d:`
            echo
            echo "Name:    ${_rname}"
            echo "Address:  ${_raddr}"
            unset _rname
            unset _raddr
       fi
   fi
   unset _name
   unset _result
   return 0
} # END nslookup()

GREP=/bin/grep
TR=/usr/bin/tr

# for agents we may need a different root directory e.g. in a cluster
# environment else it is the root directory
#
ROOT_DIR=${ROOT_DIR:-""}

# defined in every script - the version of this script
INST_OPCVERS=09.01.180

# prerequisites:
#---------------
#
# the paths
#
#   OPT_DIR	e.g. /opt
#   VAR_DIR	e.g. /var/opt
#   ETC_DIR	e.g. /etc/opt
#
# must be defined before using this module
#
# Naming Scheme
# <prog>_<name>_DIR	1 directory e.g. OV_<name>_DIR, OPC_<name>_DIR
# <name>_PATH		several directories delimited by ":"
#
TMP_DIR=${ROOT_DIR}/tmp/opc_tmp

#-------------------------------------
# generic paths
#-------------------------------------
# /opt/OV
OV_OPT_DIR=${OV_OPT_DIR:-${ROOT_DIR}${OPT_DIR}/OV}
OPC_OPT_DIR=${OPC_OPT_DIR:-${OV_OPT_DIR}}
# /var/opt/OV
OV_VAR_DIR=${OV_VAR_DIR:-${ROOT_DIR}${VAR_DIR}/OV}
OV_VAR_SH_DIR=${OV_VAR_SH_DIR:-${OV_VAR_DIR}/share}
OPC_VAR_DIR=${OPC_VAR_DIR:-${OV_VAR_DIR}}
OV_DEFAULTS_PATH=${OV_VAR_DIR}/tmp
# /etc/opt/OV
OV_ETC_DIR=${OV_ETC_DIR:-${ROOT_DIR}${ETC_DIR}/OV}
OV_ETC_SH_DIR=${OV_ETC_SH_DIR:-${OV_ETC_DIR}/share}
OPC_ETC_DIR=${OPC_ETC_DIR:-${OV_ETC_DIR}/share}

OV_NEWCFG_DIR=${OV_NEWCFG_DIR:-${OV_OPT_DIR}/newconfig}
OV_OLDCFG_DIR=${OV_OLDCFG_DIR:-${OV_OPT_DIR}/old}

# WWW
OV_WWW_DIR=$OV_OPT_DIR/www

# /opt/OV
OV_OPT10_DIR=${OV_OPT10_DIR:-${ROOT_DIR}${OPT10_DIR}/OV}
# /var/opt/OV
OV_VAR10_DIR=${OV_VAR10_DIR:-${ROOT_DIR}${VAR10_DIR}/OV}
OV_VAR10_SH_DIR=${OV_VAR10_SH_DIR:-${OV_VAR10_DIR}/share}
# /etc/opt/OV
OV_ETC10_DIR=${OV_ETC10_DIR:-${ROOT_DIR}${ETC10_DIR}/OV}
OV_ETC10_SH_DIR=${OV_ETC10_SH_DIR:-${OV_ETC10_DIR}/share}

#-------------------------------------
# OpenView paths
#-------------------------------------
OV_BIN_DIR=${OV_BIN_DIR:-${OV_OPT_DIR}/bin}
OV_TMP_DIR=${OV_TMP_DIR:-${OV_VAR_DIR}/tmp}
OV_LOG_DIR=${OV_LOG_DIR:-${OV_VAR_DIR}/log}
OV_CFG_DIR=${OV_CFG_DIR:-${OV_ETC_DIR}/share/conf}
OV_ALT_CFG_DIR=${OV_ALT_CFG_DIR:-${OV_ETC_DIR}/share/\#conf}

#-------------------------------------
# ITO Server Paths
#-------------------------------------
# /usr/OV/bin/OpC
OPCSVR_BIN_DIR=${OPCSVR_BIN_DIR:-${OPC_OPT_DIR}/bin/OpC}
# /usr/OV/bin/OpC/install
OPCSVR_INST_DIR=${OPCSVR_INST_DIR:-${OPCSVR_BIN_DIR}/install}
# /opt/OV/lib
OPCSVR_LIB_DIR=${OPCSVR_LIB_DIR:-${OPC_OPT_DIR}/lib64}
# /opt/OV/nls
OPCSVR_NLS_DIR=${OPCSVR_NLS_DIR:-${OPCSVR_LIB_DIR}/nls}
# /opt/OV/include
OPCSVR_INC_DIR=${OPCSVR_INC_DIR:-${OPC_OPT_DIR}/include}
# /usr/OV/bin/OpC/utils
OPCSVR_UTIL_DIR=${OPCSVR_UTIL_DIR:-${OPCSVR_BIN_DIR}/utils}
# /usr/OV/bin/OpC/agtinstall
OPCSVR_AGTINST_DIR=${OPCSVR_AGTINST_DIR:-${OPCSVR_BIN_DIR}/agtinstall}
# /etc/opt/OV/share/lrf
OPCSVR_OPC_DIR=${OPCSVR_OPC_DIR:-${OPC_OPT_DIR}/OpC}
OPCSVR_NEWCFG_DIR=${OPCSVR_NEWCFG_DIR:-${OV_NEWCFG_DIR}/OpC}
OPCSVR_OLDCFG_DIR=${OPCSVR_OLDCFG_DIR:-${OV_OLDCFG_DIR}/OpC}
OPCSVR_HELP_DIR=${OPCSVR_HELP_DIR:-${OV_VAR_DIR}/share/help}

# /etc/opt/OV directories
OPCSVR_LRF_DIR=${OPCSVR_LRF_DIR:-${OPC_ETC_DIR}/lrf}
OPCSVR_REG_DIR=${OPCSVR_REG_DIR:-${OPC_ETC_DIR}/registration}

# /var/opt/OV/share/databases
OPCSVR_DB_DIR=${OPCSVR_DB_DIR:-${OPC_VAR_DIR}/share/databases/OpC}
# /var/opt/OV/share/databases/OpC/mgd_node/vendor
OPCSVR_VEND_DIR=${OPCSVR_DB_DIR}/mgd_node/vendor
# /var/opt/OV/share/databases/OpC/mgd_node/customer
OPCSVR_CUST_DIR=${OPCSVR_DB_DIR}/mgd_node/customer

# dynamic paths
OPCSVR_TMP_DIR=${OPCSVR_TMP_DIR:-${OPC_VAR_DIR}/share/tmp/OpC}
OPCSVR_LOG_DIR=${OPCSVR_LOG_DIR:-${OPC_VAR_DIR}/log/OpC}
OPCSVR_CFG_DIR=${OPCSVR_CFG_DIR:-${OPC_ETC_DIR}/conf/OpC}
OPCSVR_APPL_DIR=${OPCSVR_APPL_DIR:-${OPC_VAR_DIR}/share/tmp/OpC_appl}
OPCSVR_MAN_DIR=${OPCSVR_MAN_DIR:-${OPC_OPT_DIR}/man}

OPCSVR_INFO_F=${OPCSVR_INFO_F:-${OPCSVR_INST_DIR}/opcsvinfo}

# Config file needed by distributed GUI client
OPC_GUICLT_MSV_F=${OPC_GUICLT_MSV_F:-${OV_CFG_DIR}/opc_guiclt_msv}
OPC_GUICLT_TMP_MSV_F=${OPC_GUICLT_TMP_MSV_F:-/tmp/opc_guiclt_msv}

# DB config file
OVDB_CONFIG_FILE=${OVDB_CONFIG_FILE:-${OV_CFG_DIR}/ovdbconf}

#-------------------------------------
# ITO Agent Paths
#-------------------------------------
# /usr/OV/bin/OpC/s700
OPCAGT_BIN_DIR=${OPCAGT_BIN_DIR:-${OPC_OPT_DIR}/bin/OpC}
OPCAGT_LIB_DIR=${OPCAGT_LIB_DIR:-${OPC_OPT_DIR}/lib64}
OPCAGT_NLS_DIR=${OPCAGT_NLS_DIR:-${OPCAGT_LIB_DIR}/nls}
OPCAGT_INC_DIR=${OPCAGT_INC_DIR:-${OPC_OPT_DIR}/include}
# /usr/OV/bin/OpC/s700/utils
OPCAGT_UTIL_DIR=${OPCAGT_UTIL_DIR:-${OPCAGT_BIN_DIR}/utils}
# /usr/OV/bin/OpC/s700/install
OPCAGT_INST_DIR=${OPCAGT_INST_DIR:-${OPCAGT_BIN_DIR}/install}

# /var/opt/OV/bin/OpC
OPCAGT_ACTMONCMD_DIR=${OPCAGT_ACTMONCMD_DIR:-${OPC_VAR_DIR}/bin/OpC}

# /var/opt/OV/bin/OpC/monitor
OPCAGT_MON_DIR=${OPCAGT_MON_DIR:-${OPC_VAR_DIR}/bin/OpC/monitor}
# /opt/OV/bin/OpC/actions
OPCAGT_ACT_DIR=${OPCAGT_ACT_DIR:-${OPC_VAR_DIR}/bin/OpC/actions}
# /var/opt/OV/bin/OpC/cmds
OPCAGT_CMD_DIR=${OPCAGT_CMD_DIR:-${OPC_VAR_DIR}/bin/OpC/cmds}

# dynamic paths
OPCAGT_LOG_DIR=${OPCAGT_LOG_DIR:-${OPC_VAR_DIR}/log/OpC}
OPCAGT_CFG_DIR=${OPCAGT_CFG_DIR:-${OPC_VAR_DIR}/conf/OpC}
OPCAGT_TMP_DIR=${OPCAGT_TMP_DIR:-${OPC_VAR_DIR}/tmp/OpC}
OPCAGT_VARBIN_DIR=${OPCAGT_TMP_DIR:-${OPC_VAR_DIR}/bin/OpC}
OPCAGT_TMPBIN_DIR=${OPCAGT_TMPBIN_DIR:-${OPCAGT_TMP_DIR}/bin}
OPCAGT_TMPCFG_DIR=${OPCAGT_TMPCFG_DIR:-${OPCAGT_TMP_DIR}/conf}
OPCAGT_NEWCFG_DIR=${OPCAGT_NEWCFG_DIR:-${OV_NEWCFG_DIR}/OpC}
OPCAGT_OLDCFG_DIR=${OPCAGT_OLDCFG_DIR:-${OV_OLDCFG_DIR}/OpC}

# info files
OPCAGT_NDINFO_F=${OPCAGT_NDINFO_F:-${OPCAGT_CFG_DIR}/nodeinfo}
OPCAGT_INFO_F=${OPCAGT_INFO_F:-${OPCAGT_INST_DIR}/opcinfo}
OPCAGT_NETLS_F=${OPCAGT_NETLS_F:-${OPCAGT_CFG_DIR}/opcnetls}
OPCAGT_MGRCONF_F=${OPCAGT_MGRCONF_F:-${OPCAGT_CFG_DIR}/mgrconf}
OPCAGT_UPD_F=${OPCAGT_UPD_F:-${OPCAGT_TMP_DIR}/update}
OPCAGT_CLIENT_F=${OPCAGT_CLIENT_F:-${OPCAGT_INST_DIR}/cfg.clients}
OPCAGT_MGMTSV_F=${OPCAGT_MGMTSV_F:-${TMP_DIR}/mgmt_sv.dat}
#
# commands
#
OPCMON=${OPCAGT_BIN_DIR}/opcmon
OPCMSG=${OPCAGT_BIN_DIR}/opcmsg

VUE_DIR=/etc/vue
VUE_USR_DIR=/usr/vue
VUE_ETC_DIR=/etc/vue
VUE_VAR_DIR=/var/vue
CDE_USR_DIR=/usr/dt
CDE_ETC_DIR=/etc/dt
CDE_VAR_DIR=/var/dt

#-------------------------------------
# paths
#-------------------------------------
SYSTEM_PATH=${SYSTEM_PATH:-"/bin:/usr/bin:/sbin:/usr/sbin:/etc"}
OV_PATH=${OV_BIN_DIR}
OPCSVR_PATH=${OPCSVR_BIN_DIR}:${OPCSVR_AGTINST_DIR}:${OPCSVR_INST_DIR}
OPCAGT_PATH=${OPCAGT_BIN_DIR}:${OPCAGT_INST_DIR}:${OPCAGT_MON_DIR}:${OPCAGT_ACT_DIR}:${OPCAGT_CMD_DIR}

# program name
APPLNAME=ITO
LONG_APPLNAME="HP OpenView Operations for Linux 09.01.180"

# common umask value
umask 022

# location of the oratab file
ORATAB_LOC=/etc/oratab

# shared library suffix
SHLIBSUFFIX=so

#-------------------------------------
# Module name definition
#-------------------------------------

# define module name
    MODULE_NAME=ALIVE_CHECK

VERS_FILE=/opt/OV/bin/OpC/install/opcinfo
OPC_TMP_DIR=${OPC_TMP_DIR:-/tmp}
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/lbin:/usr/local/bin:/usr/etc:/usr/ucb:/usr/bsd
export PATH
package_extention="rpm.gz"
package_type="rpm"
ZIP="gzip"

OPCSVR_CTLM_F="/opt/OV/bin/OpC/opcctlm"

INST_LOG_FILE="/var/opt/OV/log/opc_inst.log"
INST_LOCK_FILE="/var/tmp/opc_inst.lock"
OVO_SETTINGS_BACKUP="/var/opt/OV/log/OVO_settings_backup.log"

   declare -a PACKAGES
   declare -a FS_PACKAGES
   declare -a V7_PACKAGES
   declare -a VERSIONS 
   declare -a DEINSTFROMSRV
   declare -a SELECTION

DCE_PKG="dce"

#old agent packages
V7_PACKAGES[0]="OPC"
V7_PACKAGES[1]="OPCCOMM" 
V7_PACKAGES[2]="OPCPERF"
V7_PACKAGES[3]="OPCSVCDISC" 

#BBC agent package names

BBC_PACKAGE=HPOvBbc
SECCO_PACKAGE=HPOvSecCo
XPL_PACKAGE=HPOvXpl
SECCC_PACKAGE=HPOvSecCC
CTRL_PACKAGE=HPOvCtrl
DEPL_PACKAGE=HPOvDepl
CONF_PACKAGE=HPOvConf
PERL_PACKAGE=HPOvPerlA
PACC_PACKAGE=HPOvPacc
PCO_PACKAGE=HPOvPCO
EA_PACKAGE=HPOvEaAgt
LCES_PACKAGE=HPOvLces
LCJA_PACKAGE=HPOvLcja
LCZC_PACKAGE=HPOvLczC
LCKO_PACKAGE=HPOvLcko
EAJA_PACKAGE=HPOvEaAja
EAES_PACKAGE=HPOvEaAes
EAZC_PACKAGE=HPOvEaAzC
EAKO_PACKAGE=HPOvEaAko
XERC_PACKAGE=HPOvXercesA
XANA_PACKAGE=HPOvXalanA
AGTR_PACKAGE=HPOvAgtEx

PACKAGES[0]="${XPL_PACKAGE}"
PACKAGES[1]="${SECCO_PACKAGE}" 
PACKAGES[2]="${BBC_PACKAGE}"
PACKAGES[3]="${SECCC_PACKAGE}" 
PACKAGES[4]="${CTRL_PACKAGE}" 
PACKAGES[5]="${DEPL_PACKAGE}" 
PACKAGES[6]="${CONF_PACKAGE}"
PACKAGES[7]="${PERL_PACKAGE}"
PACKAGES[8]="${PACC_PACKAGE}"
PACKAGES[9]="${PCO_PACKAGE}"
PACKAGES[10]="${EA_PACKAGE}"
PACKAGES[11]="${LCJA_PACKAGE}"
PACKAGES[12]="${LCZC_PACKAGE}"
PACKAGES[13]="${LCKO_PACKAGE}"
PACKAGES[14]="${EAJA_PACKAGE}"
PACKAGES[15]="${EAES_PACKAGE}"
PACKAGES[16]="${EAZC_PACKAGE}"
PACKAGES[17]="${EAKO_PACKAGE}"
PACKAGES[18]="${LCES_PACKAGE}"
PACKAGES[19]="${XERC_PACKAGE}"
PACKAGES[20]="${XANA_PACKAGE}"
PACKAGES[21]="${AGTR_PACKAGE}"

FS_BBC_PACKAGE=HPOvBbc
FS_SECCO_PACKAGE=HPOvSecCo
FS_XPL_PACKAGE=HPOvXpl
FS_SECCC_PACKAGE=HPOvSecCC
FS_CTRL_PACKAGE=HPOvCtrl
FS_DEPL_PACKAGE=HPOvDepl
FS_CONF_PACKAGE=HPOvConf
FS_PERL_PACKAGE=HPOvPerlA
FS_PACC_PACKAGE=HPOvPacc
FS_PCO_PACKAGE=HPOvPCO
FS_EA_PACKAGE=HPOvEaAgt
FS_LCES_PACKAGE=HPOvLces
FS_LCJA_PACKAGE=HPOvLcja
FS_LCZC_PACKAGE=HPOvLczC
FS_LCKO_PACKAGE=HPOvLcko
FS_EAJA_PACKAGE=HPOvEaAja
FS_EAES_PACKAGE=HPOvEaAes
FS_EAZC_PACKAGE=HPOvEaAzC
FS_EAKO_PACKAGE=HPOvEaAko
FS_XERC_PACKAGE=HPOvXercesA
FS_XANA_PACKAGE=HPOvXalanA
FS_AGTR_PACKAGE=HPOvAgtEx

FS_EAZS_PACKAGE=HPOvEaAzS
FS_LCZS_PACKAGE=HPOvLczS

FS_PACKAGES[0]="${FS_XPL_PACKAGE}"
FS_PACKAGES[1]="${FS_SECCO_PACKAGE}" 
FS_PACKAGES[2]="${FS_BBC_PACKAGE}"
FS_PACKAGES[3]="${FS_SECCC_PACKAGE}" 
FS_PACKAGES[4]="${FS_CTRL_PACKAGE}" 
FS_PACKAGES[5]="${FS_DEPL_PACKAGE}" 
FS_PACKAGES[6]="${FS_CONF_PACKAGE}"
FS_PACKAGES[7]="${FS_PERL_PACKAGE}"
FS_PACKAGES[8]="${FS_PACC_PACKAGE}"
FS_PACKAGES[9]="${FS_PCO_PACKAGE}"
FS_PACKAGES[10]="${FS_EA_PACKAGE}"
FS_PACKAGES[11]="${FS_LCJA_PACKAGE}"
FS_PACKAGES[12]="${FS_LCZC_PACKAGE}"
FS_PACKAGES[13]="${FS_LCKO_PACKAGE}"
FS_PACKAGES[14]="${FS_EAJA_PACKAGE}"
FS_PACKAGES[15]="${FS_EAES_PACKAGE}"
FS_PACKAGES[16]="${FS_EAZC_PACKAGE}"
FS_PACKAGES[17]="${FS_EAKO_PACKAGE}"
FS_PACKAGES[18]="${FS_LCES_PACKAGE}"
FS_PACKAGES[19]="${FS_XERC_PACKAGE}"
FS_PACKAGES[20]="${FS_XANA_PACKAGE}"
FS_PACKAGES[21]="${FS_AGTR_PACKAGE}"

XPL_VERSION=HPOvXpl.xml
SECCO_VERSION=HPOvSecCo.xml
BBC_VERSION=HPOvBbc.xml
SECCC_VERSION=HPOvSecCC.xml
CTRL_VERSION=HPOvCtrl.xml
DEPL_VERSION=HPOvDepl.xml
CONF_VERSION=HPOvConf.xml
PERL_VERSION=HPOvPerlA.xml
PACC_VERSION=HPOvPacc.xml
PCO_VERSION=HPOvPCO.xml
EA_VERSION=HPOvEaAgt.xml
LCES_VERSION=HPOvLces.xml
LCJA_VERSION=HPOvLcja.xml
LCZC_VERSION=HPOvLczC.xml
LCKO_VERSION=HPOvLcko.xml
EAJA_VERSION=HPOvEaAja.xml
EAES_VERSION=HPOvEaAes.xml
EAZC_VERSION=HPOvEaAzC.xml
EAKO_VERSION=HPOvEaAko.xml
XERC_VERSION=HPOvXercesA.xml
XANA_VERSION=HPOvXalanA.xml
AGTR_VERSION=HPOvAgtEx.xml

VERSIONS[0]="${XPL_VERSION}"
VERSIONS[1]="${SECCO_VERSION}"
VERSIONS[2]="${BBC_VERSION}"
VERSIONS[3]="${SECCC_VERSION}"
VERSIONS[4]="${CTRL_VERSION}"
VERSIONS[5]="${DEPL_VERSION}"
VERSIONS[6]="${CONF_VERSION}"
VERSIONS[7]="${PERL_VERSION}"
VERSIONS[8]="${PACC_VERSION}"
VERSIONS[9]="${PCO_VERSION}"
VERSIONS[10]="${EA_VERSION}"
VERSIONS[11]="${LCJA_VERSION}"
VERSIONS[12]="${LCZC_VERSION}"
VERSIONS[13]="${LCKO_VERSION}"
VERSIONS[14]="${EAJA_VERSION}"
VERSIONS[15]="${EAES_VERSION}"
VERSIONS[16]="${EAZC_VERSION}"
VERSIONS[17]="${EAKO_VERSION}"
VERSIONS[18]="${LCES_VERSION}"
VERSIONS[19]="${XERC_VERSION}"
VERSIONS[20]="${XANA_VERSION}"
VERSIONS[21]="${AGTR_VERSION}"

# Required ovc action if re-installed

XPL_ACTION="-kill"
SECCO_ACTION="-kill"
BBC_ACTION="-kill"
SECCC_ACTION="-kill"
CTRL_ACTION="-kill"
DEPL_ACTION="-kill"
CONF_ACTION="-kill"
PERL_ACTION="-stopagent"
PACC_ACTION="-stopagent"
PCO_ACTION="-stopagent"
EA_ACTION="-stopagent"
LCES_ACTION=""
LCJA_ACTION=""
LCZC_ACTION=""
LCKO_ACTION=""
EAJA_ACTION=""
EAES_ACTION=""
EAZC_ACTION=""
EAKO_ACTION=""
XERC_ACTION=""
XANA_ACTION=""
AGTR_ACTION="-stopagent"

OVCACTIONS[0]="${XPL_ACTION}"
OVCACTIONS[1]="${SECCO_ACTION}"
OVCACTIONS[2]="${BBC_ACTION}"
OVCACTIONS[3]="${SECCC_ACTION}"
OVCACTIONS[4]="${CTRL_ACTION}"
OVCACTIONS[5]="${DEPL_ACTION}"
OVCACTIONS[6]="${CONF_ACTION}"
OVCACTIONS[7]="${PERL_ACTION}"
OVCACTIONS[8]="${PACC_ACTION}"
OVCACTIONS[9]="${PCO_ACTION}"
OVCACTIONS[10]="${EA_ACTION}"
OVCACTIONS[11]="${LCJA_ACTION}"
OVCACTIONS[12]="${LCZC_ACTION}"
OVCACTIONS[13]="${LCKO_ACTION}"
OVCACTIONS[14]="${EAJA_ACTION}"
OVCACTIONS[15]="${EAES_ACTION}"
OVCACTIONS[16]="${EAZC_ACTION}"
OVCACTIONS[17]="${EAKO_ACTION}"
OVCACTIONS[18]="${LCES_ACTION}"
OVCACTIONS[19]="${XERC_ACTION}"
OVCACTIONS[20]="${XANA_ACTION}"
OVCACTIONS[21]="${AGTR_ACTION}"

XPL_SRVDEINST="NO"
SECCO_SRVDEINST="NO"
BBC_SRVDEINST="NO"
SECCC_SRVDEINST="NO"
CTRL_SRVDEINST="NO"
DEPL_SRVDEINST="NO"
CONF_SRVDEINST="NO"
PERL_SRVDEINST="YES"
PACC_SRVDEINST="YES"
PCO_SRVDEINST="YES"
EA_SRVDEINST="YES"
LCES_SRVDEINST="NO"
LCJA_SRVDEINST="NO"
LCZC_SRVDEINST="NO"
LCKO_SRVDEINST="NO"
EAJA_SRVDEINST="YES"
EAES_SRVDEINST="YES"
EAZC_SRVDEINST="YES"
EAKO_SRVDEINST="YES"
XERC_SRVDEINST="YES"
XANA_SRVDEINST="YES"
AGTR_SRVDEINST="YES"

DEINSTFROMSRV[0]="${XPL_SRVDEINST}"
DEINSTFROMSRV[1]="${SECCO_SRVDEINST}"
DEINSTFROMSRV[2]="${BBC_SRVDEINST}"
DEINSTFROMSRV[3]="${SECCC_SRVDEINST}"
DEINSTFROMSRV[4]="${CTRL_SRVDEINST}"
DEINSTFROMSRV[5]="${DEPL_SRVDEINST}"
DEINSTFROMSRV[6]="${CONF_SRVDEINST}"
DEINSTFROMSRV[7]="${PERL_SRVDEINST}"
DEINSTFROMSRV[8]="${PACC_SRVDEINST}"
DEINSTFROMSRV[9]="${PCO_SRVDEINST}"
DEINSTFROMSRV[10]="${EA_SRVDEINST}"
DEINSTFROMSRV[11]="${LCJA_SRVDEINST}"
DEINSTFROMSRV[12]="${LCZC_SRVDEINST}"
DEINSTFROMSRV[13]="${LCKO_SRVDEINST}"
DEINSTFROMSRV[14]="${EAJA_SRVDEINST}"
DEINSTFROMSRV[15]="${EAES_SRVDEINST}"
DEINSTFROMSRV[16]="${EAZC_SRVDEINST}"
DEINSTFROMSRV[17]="${EAKO_SRVDEINST}"
DEINSTFROMSRV[18]="${LCES_SRVDEINST}"
DEINSTFROMSRV[19]="${XERC_SRVDEINST}"
DEINSTFROMSRV[20]="${XANA_SRVDEINST}"
DEINSTFROMSRV[21]="${AGTR_SRVDEINST}"

  ECHOC="echo -e"

#-----------------------------------------------------------------------------
# Function: prepare_tmp_file
# Description:
#   Prepare a temporary file to be used in the script.
#   If a temporary file with this name (including PID) already exists, it is
#   removed.
#
# Params:
#   $1 - directory to use for the tmp file
#
# Returns: 0 - OK
#          1 - error occured
#-----------------------------------------------------------------------------
prepare_tmp_file()
{
  TMP_FILE=${1}/opc-$$.tmp
  if [ -f ${TMP_FILE} ]
  then
    #   remove temporary file
    #
    rm -f ${TMP_FILE}
    if [ $? -ne 0 ]
    then
      return 1
    fi
  fi

  return 0
}

#------------------------------------------------------------------------------
#  DESCRIPTION
#    check which processes are running on the managed node, determine if it
#    is possible to use deployment component to install agent software
# 
#  PARAMETERS
#    OPCSVR_BIN_DIR should be set (with init)
#    node name (node)
#
#  EXIT CODE
#   0 .. ok running
#   1 .. one or more components from CORE category isn't running
#   2 .. opcragt is missing
#
#COMMENT: what happens if managed node belongs to different mgmt server
#         is it possible to contact managed node without SSL 
#         communication established
#------------------------------------------------------------------------------
check_remote_core_status()
{
  typeset node="$1"
  typeset procs
  typeset OUTPUT_F

  #  check if the following processes are running:
  #  ovbbccb, ovcd, ovconfd
  #
  if [ -f ${OPCSVR_BIN_DIR}/opcragt ]
  then
    OUTPUT_F=opcragt.${node}.out
    rm -f /tmp/${OUTPUT_F}
    LANG=C LC_ALL=C ${OPCSVR_BIN_DIR}/opcragt -status ${node} \
                    >/tmp/${OUTPUT_F} 2>&1
    procs="ovbbccb ovcd ovconfd"
    for proc in $procs
    do
      cat /tmp/${OUTPUT_F} | grep $proc | grep "is running" \
                             2>/dev/null 1>/dev/null
      if [ $? -ne 0 ]
      then
        #  process $proc was not found in output file
        #  with status is running
        oi_log_file /tmp/${OUTPUT_F}
        rm -f /tmp/${OUTPUT_F}
        return 1
      fi
    done
    rm -f /tmp/${OUTPUT_F}
  else
    return 2
  fi
  return 0
}

#------------------------------------------------------------------------------
#  DESCRIPTION
#    Determine under which account agent is running on the managed node.
#    Native installer calls on most systems need native root privilege.
#
#  PARAMETERS
#    node name (node)
#    
#  EXIT CODE
#    0 .. running as privileged user
#    1 .. running as non privileged user
#    2 .. opcragt failed
#    3 .. opcragt missing
#------------------------------------------------------------------------------
get_remote_agent_user_id()
{
  typeset node="$1"
  typeset out _id
  if [ -f ${OV_BIN_DIR}/OpC/opcragt ]
  then
    out=`${OV_BIN_DIR}/OpC/opcragt -running_as_user ${node} | grep "USER_ID:"`
    if [ $? -eq 0 ]
    then
      _id=`${ECHOC} $out | awk '{print $2}'`
      if [ ${_id} = 0 ]
      then
        return 0
      else
        return 1
      fi
    else
      return 2
    fi
  else
    return 3
  fi
}

#------------------------------------------------------------------------------
#  DESCRIPTION
#    Get variable value from the managed node via opcragt. Value is
#    stored in variable confvalue which must be unset after it is used.
#  PARAMETERS
#    node name (node)
#    config variable (confvar)     
#    namespace (nspace)
#    OV_BIN_DIR should be set from shlib_opc_init
#  EXIT CODE
#    0 .. config variable is set
#    1 .. config variable confvar not set
#    2 .. opcragt call failed
#    3 .. opcragt not found
#         
#------------------------------------------------------------------------------
get_remote_config_var_value()
{
  if [ $# -eq 2 ]
  then
    typeset nspace="eaagt"
  else
    typeset nspace="$3"
  fi
  typeset node="$1"
  typeset confvar="$2"
  typeset out
  if [ -f ${OV_BIN_DIR}/OpC/opcragt ]
  then
    out="`${OV_BIN_DIR}/OpC/opcragt -get_config_var ${nspace}:${confvar} \
                                              ${node} | grep ${confvar}`"
    if [ $? -eq 0 ]
    then
      if [ "x" != "x${out}" ]
      then
        confvalue="`${ECHOC} ${out} | awk -F= '{print $2}' | sed 's/^ //g'`"
      else
        return 1
      fi
    else
      return 2
    fi
  else
    return 3
  fi
  return 0
}

#------------------------------------------------------------------------------
#  DESCRIPTION
#    Deploy and install/deinstall specified package to the managed node.
#  PARAMETERS
#    method (install or deinstall)
#    node name (node)
#    package descriptor (e.g. HPOv*.xml)
#  EXIT CODE
#    0 .. deployment and (de)installation successful
#    1 .. deployment and (de)installation failed
#    2 .. missing ovdeploy
#    3 .. wrong parameters
#------------------------------------------------------------------------------
deploy_package()
{
  typeset method="$1"
  typeset node="$2"
  typeset pkgdesc="$3"
  if [ -f ${OV_BIN_DIR}/ovdeploy ]
  then
    #all packages which should be installed must be prepared in TMP_DIR
    if [ "${method}" = "install" ]
    then
      ${OV_BIN_DIR}/ovdeploy -ovrg server -d -pkg ${TMP_DIR}/$pkgdesc -node $node
    fi
    if [ "${method}" = "deinstall" ]
    then
      ${OV_BIN_DIR}/ovdeploy -ovrg server -r -pkg ${TMP_DIR}/$pkgname -node $node
    else
      return 3
    fi
    if [ $? -ne 0 ]
    then
      return 1
    fi
  else
    return 2
  fi
  return 0
}

#------------------------------------------------------------------------------
#  DESCRIPTION
#    Create and transfer agent profile to the managed node.
#------------------------------------------------------------------------------
transfer_agent_profile()
{
# You can still find the code for it in versions of OVOU prior to A.08.23
  ${ECHOC} "QXCR1000294761: this function has been obsoleted!"
}

#------------------------------------------------------------------------------
#  DESCRIPTION
#    Transfer package and package descriptor from mgmt server to the 
#    managed node.
#  PARAMETERS
#    xml file (file)
#    xml key (key)
#  EXIT CODE
#    0 .. ok  (value is saved in xml_value, unset this variable when used)
#    1 .. file not found
#    xml_value (global)
#------------------------------------------------------------------------------
get_xml_value()
{
  typeset file="$1"
  typeset key="$2"
  if [ -f $file ]
  then
    xml_value=`cat $file | sed  -n -e "s/<$key>\(.*\)<\/$key>/\1/p"`
  else
    return 1
  fi
  return 0
}

#------------------------------------------------------------------------------
#  DESCRIPTION
#    Transfer package and package descriptor from mgmt server to the 
#    managed node.
#  PARAMETERS
#    node name (node)
#    package name ( *_pkg.Z )
#    package descriptor ( *_version )
#  EXIT CODE
#    0 .. transfer successful
#    1 .. transfer failed
#    2 .. missing ovdeploy
#------------------------------------------------------------------------------
transfer_package()
{
  typeset node="$1"
  typeset pkgname="$2"
  typeset pkgdesc="$3"
  if [ -f ${OV_BIN_DIR}/ovdeploy ]
  then
    #all packages which should be installed must be prepared in TMP_DIR
    ${OV_BIN_DIR}/ovdeploy -d -ovrg server \
			      -file ${TMP_DIR}/$pkgname \
                              -file ${TMP_DIR}/$pkgdesc -node $node
    if [ $? -ne 0 ]
    then
      return 1
    fi
  else
    return 2
  fi
  return 0
}

#------------------------------------------------------------------------------
#  DESCRIPTION
#    Checks if parameter is number
#  EXIT CODE
#    0 - parameter is number
#    1 - parameter is not a number
#------------------------------------------------------------------------------
is_number()
{
  typeset _string _len _sym
  typeset -i res res2 i
  typeset nums='0 1 2 3 4 5 6 7 8 9'

  _string=$1
  _len=`${ECHOC} $_string| wc -c`
  res=0

  i=1
  while [ $i -lt $_len ]
  do
    _char=`${ECHOC} $_string | cut -c $i`
    res2=0
    for _sym in $nums
    do
      if [ $_char = $_sym ]; then res2=1; fi
    done
    if [ $res2 -ne 1 ]; then res=1; fi
    i=`expr $i + 1`
  done

  return $res
}

#------------------------------------------------------------------------------
#  DESCRIPTION
#    Compare two versions - strings of chars or numbers devided by dot.
#  PARAMETERS
#    vers1, vers2 - versions to be compared
#  EXIT CODE
#    0 - vers1 is older than vers2
#    1 - versions are the same
#    2 - vers1 is newer/bigger
#------------------------------------------------------------------------------
cmp_vers()
{
 typeset _ret _vers1 _vers2 _go_on _subver1 _subver2
 typeset -i _counter

 _vers1=$1
 _vers2=$2
 _ret=1

 _vers_1st=`${ECHOC} $_vers1 | cut -c 1`
 _counter=1
 _go_on=true
 while ( $_go_on )
 do
   _subver1=`${ECHOC} $_vers1 | cut -f ${_counter} -d .`
   _subver2=`${ECHOC} $_vers2 | cut -f ${_counter} -d .`
   _counter=`expr $_counter + 1`
   if [ $_subver1 ]
   then
     is_number $_subver1
     if [ $? -eq 0 ]
     then
       if [ $_subver1 -gt $_subver2 ]; then _ret=2; _go_on=false; fi
       if [ $_subver1 -lt $_subver2 ]; then _ret=0; _go_on=false; fi
     else
       if [[ "$_subver1" < "$_subver2" ]]; then _ret=2; _go_on=false; fi
       if [[ "$_subver1" > "$_subver2" ]]; then _ret=0; _go_on=false; fi
     fi
   else
    _ret=1
    _go_on=false
   fi
 done

 return $_ret
}

#===============================================================================
#
# RETURN:
#------------------------------------------------------------------------------
#  DESCRIPTION
#    Checks existance of installation lock file on remote node
#  PARAMETERS
#    node    - name of remote node
#    timeout - max amount of time to perform operation
#  EXIT CODE
#    0 - remote lock file does not exist
#    1 - ovdeploy failed
#    2 - lock file exists but timeout occured 
#===============================================================================
check_remote_lockfile()
{
  typeset node=$1
  typeset timeout=$2
  typeset i cycles
  typeset lockcheck_cmd result
  typeset lockcheck_cmd2

  if [ "${OPC_PLATFORM}" = "ms/x86/winnt" -o "${OPC_PLATFORM}" = "ms/x64/win2k3" -o "${OPC_PLATFORM}" = "ms/ipf64/win2k3" ]
  then
    lockcheck_cmd="if exist %SystemRoot%\\Temp\\opc_inst.lock ${ECHOC} lock >lock_check.tmp"
    lockcheck_cmd2="if not exist %SystemRoot%\\Temp\\opc_inst.lock ${ECHOC} no lock >lock_check.tmp"
  else
    lockcheck_cmd="if [ -f ${INST_LOCK_FILE} ];then ${ECHOC} \"lock\">/tmp/lock_check.tmp;else ${ECHOC} \"no lock\">/tmp/lock_check.tmp;fi"
  fi

  ## number of cycles depends on sleep parameter
  cycles=`expr $timeout / 10`
  
  i=0
  while [ $i -lt $cycles ]
  do
    sleep 10

    ## run a command that will check existance of lock file
    ${OV_BIN_DIR}/ovdeploy -ovrg server \
			   -cmd "$lockcheck_cmd" \
			   -node "${node}" >/dev/null 2>&1
    if [ $? -ne 0 ]
    then
      # something wrong with deployment
      return 1
    fi
    ## download lockfile check results
    if [ "${OPC_PLATFORM}" = "ms/x86/winnt" -o "${OPC_PLATFORM}" = "ms/x64/win2k3" -o "${OPC_PLATFORM}" = "ms/ipf64/win2k3" ]
    then
      # Windows is special - there is no "else" in if statement
      ${OV_BIN_DIR}/ovdeploy -ovrg server \
                             -cmd "$lockcheck_cmd2" \
                             -node "${node}" >/dev/null 2>&1
      if [ $? -ne 0 ]
      then
        # something wrong with deployment
        return 1
      fi
      
      ${OV_BIN_DIR}/ovdeploy -download \
                             -file lock_check.tmp \
                             -host ${node} \
                             -ovrg server \
                             >/dev/null 2>&1
    else
      ${OV_BIN_DIR}/ovdeploy -download \
                             -file /tmp/lock_check.tmp \
                             -host ${node} \
                             -ovrg server \
                             >/dev/null 2>&1
    fi
    if [ $? -ne 0 ]
    then
      # something wrong with deployment
      return 1
    fi
    ## run a command that will cleanup check_results from node
    if [ "${OPC_PLATFORM}" = "ms/x86/winnt" -o "${OPC_PLATFORM}" = "ms/x64/win2k3" -o "${OPC_PLATFORM}" = "ms/ipf64/win2k3" ]
    then
      ${OV_BIN_DIR}/ovdeploy -ovrg server \
                             -cmd "del /f lock_check.tmp" \
                             -node "${node}" >/dev/null 2>&1
    else
      ${OV_BIN_DIR}/ovdeploy -ovrg server \
                             -cmd "rm -f /tmp/lock_check.tmp" \
                             -node "${node}" >/dev/null 2>&1
    fi

    result=`cat ${OV_VAR_DIR}/shared/server/installation/incoming/files/lock_check.tmp`
    rm -f ${OV_VAR_DIR}/shared/server/installation/incoming/files/lock_check.tmp
    if [ "${result}" = "lock" ]
    then
      ## script is still running on node - wait some more..
      i=`expr $i + 1`
    else
      ## Lock file does not exist.
      return 0
    fi
  done
  return 2
}

#===============================================================================
#
#  DESCRIPTION
#    Get agent/client name
#
#  PARAMETERS
#    Platform triplet (platform)
#  EXIT CODE
#    0 success (agent/client name is stored in agent_client_name, unset
#               this variable when used)
#    1 failure
#===============================================================================
get_agent_client_name()
{
  typeset platform=$1

  if [ -z "${platform}" ]
  then
     return 1
  fi
  
  if [ -f ${OPC_INST_DIR}/${platform}/OVO-Agent.xml ]
  then
    agent_client_name="OVO-Agent.xml"
  elif [ -f ${OPC_INST_DIR}/${platform}/OVO-Client.xml ]
  then
    agent_client_name="OVO-Client.xml"
  else
    return 1
  fi

  return 0
}

#===============================================================================
#
#  DESCRIPTION
#    Get packages names 
#
#  PARAMETERS
#    Platform triplet (platform)
#  EXIT CODE
#    0 success (package name is stored in package_name, unset
#               this variable when used)
#    1 failure
#===============================================================================
get_packages_name()
{

  typeset platform=$1

  if [ -z "${platform}" ]
  then
     return 1
  fi
  
  get_agent_client_name ${platform}
  if [ $? -ne  0 ] 
  then
    return 1
  fi

  typeset reg_file="${OPC_INST_DIR}/${platform}/${agent_client_name}"

  get_xml_value $reg_file "package"
  if [ $? = 0 ]
  then
    i=0
    for tmp_package in $xml_value
    do
      packages[i]=`${ECHOC} $tmp_package | cut -d '.' -f 1`
      versions[i]=${tmp_package}
      i=`expr $i + 1`
    done
    unset agent_client_name
    unset xml_value
  else
    unset agent_client_name
    unset xml_value
    return 1
  fi
  
  i=0
  while [ ${i} -lt ${#versions[*]} ]
  do
    get_xml_value "/var/opt/OV/share/databases/OpC/mgd_node/vendor/${platform}/${versions[i]}" "file"
    if [ $? = 0 ]
    then
      full_pkg_name[i]=`${ECHOC} $xml_value`
      unset xml_value
      i=`expr $i + 1`
    else
      unset xml_value
      return 1
    fi  
  done

  if [ "x${packages[*]}" != "x" ]
  then
    return 0
  else
    return 1
  fi

return 0
}

#===============================================================================
#
#  DESCRIPTION
#    Get file extension of a package.
#
#  PARAMETERS
#    Platform triplet (platform)
#  EXIT CODE
#    0 success (package extension is stored in package_extension, unset
#               this variable when used)
#    1 failure
#===============================================================================
get_package_extension()
{
  typeset platform=$1

  if [ -z "${platform}" ]
  then
    return 1
  fi

  ## 1. Try the OVO-Agent.xml & OVO-Client.xml first
  
  get_agent_client_name ${platform}
  if [ $? -eq  0 ]
    then

    typeset reg_file="${OPC_INST_DIR}/${platform}/${agent_client_name}"

    package_extension=""

    get_xml_value $reg_file "pkgext"
    if [ $? = 0 ]
    then
      package_extension=$xml_value
    fi
    unset agent_client_name
    unset xml_value
  fi
  
  if [ "x${package_extension}" != "x" ]
  then
    return 0
  fi

  ## 2. Then we use our default values.

  platform_local=`${ECHOC} ${platform} | cut -d '/' -f 1-3`

  case "${platform_local}" in
    "linux/x64/linux26")
      package_extension="rpm.gz"
      ;;
    "linux/ipf64/linux26")
      package_extension="rpm.gz"
      ;;
    "linux/x86/linux26")
      package_extension="rpm.gz"
      ;;
    "linux/zSeries/linux26")
      package_extension="rpm.gz"
      ;;
    "linux/x86/linux24")
      package_extension="rpm.gz"
      ;;
    "linux/x86/debian")
      package_extension="deb.gz"
      ;;
    "ms/x86/winnt")
      package_extension="msi"
      ;;
    "hp/pa-risc/hpux1100")
      package_extension="depot.Z"
      ;;
    "hp/ipf32/hpux1122")
      package_extension="depot.Z"
      ;;
    "sun/sparc/solaris7")
      package_extension="sparc.Z"
      ;;
    "sun/x86/solaris10")
      package_extension="i86pc.Z"
      ;;
    "ibm/rs6000/aix5")
      package_extension="bff.Z"
      ;;
    "ibm/rs6k64/aix5")
      package_extension="bff.Z"
      ;;
    "hp/alpha/tru64")
     package_extension="tar.Z"
     ;; 
    "hp/ipf64/ovms")
     package_extension="exe"
     ;;
    "hp/alpha/ovms")
     package_extension="exe"
     ;;
    "ms/x64/win2k3")
     package_extension="msi"
     ;;
    "ms/ipf64/win2k3")
     package_extension="msi"
     ;;
    *)
      package_extension="Z"
      ;;
  esac
  return 0
}

#===============================================================================
#  Remove packages from remote node.
#
#  PARAMETERS
#    Node from which to remove packages (node)
#    Platform triplet (platform)
#
#  EXIT CODE
#    0 .. removal successful
#    1 .. something wrong with deployment
#    2 .. ovdeploy missing
#    3 .. other error
#===============================================================================
remove_bbc_packages()
{
  typeset node=$1
  typeset platform=$2
  typeset PKG_LOC
  typeset deinst_params

  if [ -z "${node}" ]
  then
    return 3
  fi

  #  Shorter variable for package location
  #
  PKG_LOC=${OPC_INST_DIR}/${OPC_PLATFORM}/${OPC_VERSION}/${OPC_PACKAGE_TYPE}

  if [ ! -f ${OV_BIN_DIR}/ovdeploy ]
  then
    # ovdeploy missing
    return 2
  fi

  #  removal procedure is platform specific
  #
  if [ "${platform}" = "ms/x86/winnt" -o "${OPC_PLATFORM}" = "ms/x64/win2k3" -o "${OPC_PLATFORM}" = "ms/ipf64/win2k3" ]
  then
    # node is of Windows type
    #

    #  transfer installation script
    #
    ${OV_BIN_DIR}/ovdeploy -ovrg server \
			   -install \
			   -file ${PKG_LOC}/opc_inst.vbs \
			   -host "${node}" \
                           >/dev/null 2>&1
    if [ $? -ne 0 ]
    then
      # something wrong with deployment
      return 1
    fi

    #  execute installation script with parameters
    #  for removal
    #
    if [ "$OPC_BREAK_DEP" -eq 1 ]
    then
      if [ "$OPC_BREAK_DEP_LIST" = "ALL" ]
      then
        OPC_BREAK_DEP_LIST=""  # default behaviour is to process all packages
      fi
      deinst_params="-break_dep $OPC_BREAK_DEP_LIST"
    else
      deinst_params=""
    fi
    deinst_params="-ni -r $deinst_params"

    ${OV_BIN_DIR}/ovdeploy -cmd \
                           "cscript opc_inst.vbs $deinst_params" \
                           -host "${node}" \
			   -ovrg server \
                           >/dev/null 2>&1

    ## since connection to Deployment is lost, we cannot say if
    ## deinstallation succeeded/failed

    ##if [ $? -ne 0 ]
    ##then
    ##  # deployment failed
    ##  #
    ##  return 1
    ##fi
  else
    # node is of Unix type
    #

    #  transfer installation script
    #
    ${OV_BIN_DIR}/ovdeploy -ovrg server \
			   -install \
			   -file ${PKG_LOC}/opc_inst \
			   -trd absolute -td /tmp \
			   -node "${node}" >/dev/null 2>&1
    if [ $? -ne 0 ]
    then
      # something wrong with deployment
      return 1
    fi

    #  change execute permissions
    #
    ${OV_BIN_DIR}/ovdeploy -ovrg server \
			   -cmd "chmod +x opc_inst" \
                           -trd absolute -td /tmp \
			   -node "${node}" >/dev/null 2>&1
    if [ $? -ne 0 ]
    then
      # something wrong with deployment
      return 1
    fi

    #  execute installation script with parameters
    #  for removal
    #
    if [ "$OPC_BREAK_DEP" -eq 1 ]
    then
      if [ "$OPC_BREAK_DEP_LIST" = "ALL" ]
      then
        OPC_BREAK_DEP_LIST=""  # default behaviour is to process all packages
      fi
      deinst_params="-break_dep $OPC_BREAK_DEP_LIST"
    else
      deinst_params=""
    fi
    deinst_params="-ni -r $deinst_params"
    ${OV_BIN_DIR}/ovdeploy -ovrg server \
			   -exe -file opc_inst \
			   -par " $deinst_params" \
			   -trd absolute -td /tmp \
			   -node "${node}" >/dev/null 2>&1

    # if deployment was working so far.. we, until other implementation is
    # in place, suggest that it's executed ok. Thus ignoring reported error
    # when it removes itself.
  fi
  return 0  
}

#------------------------------------------------------------------------------
#  DESCRIPTION
#    Checks downloaded logfile for possible errors.
#  PARAMETERS
#    logfile (log_file)
#  EXIT CODE
#    0 .. no errors
#    1 .. warning(s)
#    2 .. error(s)
#    3 .. logfile not found
#------------------------------------------------------------------------------
check_bbc_logfile()
{
  typeset log_file=$1
  typeset log_file_last
  typeset -i retval=0

  if [ ! -f "${log_file}" ]
  then
    ## no log file
    return 3
  fi

  ## check if installation script successfully ended
  log_file_last=`tail -n 10 ${log_file}`
  ${ECHOC} "${log_file_last}"| grep "OVO Maintenance script ends" | grep -v "error"\
      >/dev/null 2>/dev/null
  if [ $? -eq 0 ]
  then
    retval=0
  else
    retval=2
  fi
  return $retval
}

#------------------------------------------------------------------------------
#  DESCRIPTION
#    Waits until remote LCore wakes-up or timeout occurs.
#  PARAMETERS
#    node name (node)
#    timeout (timeout) - timeout in minutes
#  EXIT CODE
#    0 .. remote node responded
#    1 .. timeout occured
#------------------------------------------------------------------------------
do_polling()
{
  if [ "${OPC_TRACE}" = "YES" ]
  then
    set -x
  fi

  typeset i
  typeset node=$1
  typeset timeout=$2
  typeset cycles

  i=0
  ## number of cycles depends on sleep parameter
  cycles=`expr $timeout \* 4`

  while [ $i -lt $cycles ]
  do
    sleep 15
    ## first stage: we ping bbc on remote node
    ${OV_BIN_DIR}/ovbbccb -ping $node >/dev/null 2>/dev/null
    if [ $? -eq 0 ]
    then
      ## second stage: we check for deployer available on remote node
      OPCLANG=C LANG=C LC_ALL=C ${OV_BIN_DIR}/ovdeploy -deployer \
                         -host $node  -ovrg server \
        | grep "Deployer is available." >/dev/null 2>/dev/null
      if [ $? -eq 0 ]
      then
        ## third stage: we check agent status
        ${OPCSVR_BIN_DIR}/opcragt -status $node >/dev/null 2>/dev/null
        if [ $? -eq 0 ]
        then
          ## everything succeeded
          return 0
        fi
      fi
    fi
    i=`expr $i + 1`
  done
  return 1
}

#------------------------------------------------------------------------------
#  DESCRIPTION
#    Checks if any of the packages from CORE category has to be installed.
#  PARAMETERS
#    node name (node)
#  EXIT CODE
#    0 .. at least one package from CORE category has to be updated
#    1 .. no package from CORE category has to be updated
#    2 .. no package at all has to be updated
#------------------------------------------------------------------------------
core_needs_update()
{
  typeset i
  typeset PACKAGE_LOCATION
  typeset _inst_ver
  typeset node=$1
 
  PACKAGE_LOCATION=${OPC_INST_DIR}/${OPC_PLATFORM}/${OPC_VERSION}/${OPC_PACKAGE_TYPE}

  get_packages_name $OPC_PLATFORM/$OPC_VERSION/$OPC_PACKAGE_TYPE
  if [ $? -ne 0 ]
  then
    i=0
    while [ ${i} -lt ${#PACKAGES[*]} ]
    do
      all_packages[i]=${PACKAGES[i]}
      i=`expr $i + 1`
    done
  else
    i=0
    while [ ${i} -lt ${#packages[*]} ]
    do
      all_packages[i]=${packages[i]}
      i=`expr $i + 1`
    done
  fi
  unset packages
  unset versions
  
  i=0
  while [ ${i} -le 5 ]
  do
    ## check if package has to be updated
    ##
    get_xml_value ${PACKAGE_LOCATION}/${all_packages[${i}]}.xml version
    _inst_ver=${xml_value}
    LANG=C LC_ALL=C ${OV_BIN_DIR}/ovdeploy -installed \
                      -pkgname "${all_packages[${i}]}" \
                      -m ehigher \
                      -ver ${_inst_ver} \
                      -host ${node} \
                      -ovrg server \
                      | grep "Object is not installed." \
                        2>/dev/null 1>/dev/null
    if [ $? -eq 0 ]
    then
      ## detected first CORE package to be updated
      return 0
    fi
    unset xml_value
    i=`expr ${i} + 1`
  done

  i=6
  while [ $i -lt ${#all_packages[*]} ]
  do
    ## check if package has to be updated
    ##
    get_xml_value ${PACKAGE_LOCATION}/${all_packages[${i}]}.xml version
    _inst_ver=${xml_value}
    LANG=C LC_ALL=C ${OV_BIN_DIR}/ovdeploy -installed \
                      -pkgname "${all_packages[${i}]}" \
                      -m ehigher \
                      -ver ${_inst_ver} \
                      -host ${node} \
                      -ovrg server \
                      | grep "Object is not installed." \
                        2>/dev/null 1>/dev/null
    if [ $? -eq 0 ]
    then
      ## detected first non-CORE package to be updated
      return 1
    fi
    unset xml_value
    i=`expr ${i} + 1`
  done
      
  ## no core packages to be updated
  return 2
}

#------------------------------------------------------------------------------
#  DESCRIPTION
#    Updates DB to a new status of software on target node
#    Turns on heartbeat monitoring/removes last distributes file timestamp
#  PARAMETERS
#    node   - nodename to update in DB
#    status - INSTALLED/DEINSTALLED
#  EXIT CODE
#    NONE
#------------------------------------------------------------------------------
update_node_status()
{
  typeset node=$1
  typeset new_status=$2
  typeset dsts

  if [ "${new_status}" = "INSTALLED" ]
  then
    $OPC_BIN_DIR/opcsw -installed ${node} 2>/dev/null >/dev/null
    if [ $? -ne 0 ]
    then
      oi_error opc 215 ${node}
    fi
    # switch on heartbeat monitoring
    $OPC_BIN_DIR/opchbp -start ${node} 2>/dev/null >/dev/null
  else
    dsts=`$OPC_BIN_DIR/opcsw -de_installed ${node} 2>/dev/null >/dev/null`
    if [ $? -ne 0 ]
    then
      oi_error opc 215 ${node}
    fi
    /bin/rm -f ${OPCSVR_CFG_DIR}/mgmt_sv/respmgrs/lastdistr/${dsts} 2>/dev/null
  fi
    
}

#------------------------------------------------------------------------------
#  DESCRIPTION
#    Updates / removes HTTPS based agent from node.
#    Phase I:
#      -checks for essential command-line utilities,
#      -checks for LCore running on remote node,
#      -checks if mgmt. server is authorized for remote node,
#      -check if LCore is running under administrative user account /
#       sudo is used.
#    Phase II:
#      -deinstallation: removes HTTPS based agent via LCore,
#      -installation: transfers packages and runs installation script.
#  REQUIREMENTS
#    The following variables have to be defined:
#     OPC_INST_DIR, OPC_PLATFORM, OPC_VERSION, OPC_PACKAGE_TYPE,
#     OV_BIN_DIR, OPCSVR_BIN_DIR, OV_VAR_DIR,
#     OPC_FORCE_UPD, OPC_MGMT_SV.
#    The following functions have to be available:
#     get_xml_value, check_remote_core_status, get_remote_config_var_value,
#     get_remote_agent_user_id, remove_bbc_packages,
#     core_needs_update, do_polling, check_bbc_logfile.
#    The following command-line utilities have to be available:
#     ovdeploy, ovbbccb, opcragt.
#      
#  PARAMETERS
#    node name (node)
#  EXIT CODE
#    0 .. update/remove successful (RET_SUCCESS)
#    1 .. update/remove failed but LCore exists (RET_FAILED)
#    2 .. LCore not available (RET_NO_LCORE)
#------------------------------------------------------------------------------
lcorechk()
{
  typeset node=$1
  typeset force_option
  typeset -i core_install
  typeset -i as_root
  typeset PACKAGE_LOCATION
  typeset opc_inst_args
  typeset OV_SUDO=""
  
  ## we define return value symbols to prevent mistakes
  typeset -i RET_SUCCESS=0
  typeset -i RET_FAILED=1
  typeset -i RET_NO_LCORE=2

  ## shorter variable for location of packages
  PACKAGE_LOCATION=${OPC_INST_DIR}/${OPC_PLATFORM}/${OPC_VERSION}/${OPC_PACKAGE_TYPE}

  ## Phase I.
  ## perform various checks

  ## check if essential command-line utilities are available
  if [ ! -f ${OV_BIN_DIR}/ovbbccb ]
  then
    ## ERROR: ${OV_BIN_DIR}/ovbbccb is missing
    return $RET_FAILED
  fi
  if [ ! -f ${OV_BIN_DIR}/ovdeploy ]
  then
    ## ERROR: ${OV_BIN_DIR}/ovdeploy is missing
    return $RET_FAILED
  fi
  if [ ! -f ${OPCSVR_BIN_DIR}/opcragt ]
  then
    ## ERROR: ${OPCSVR_BIN_DIR}/opcragt is missing
    return $RET_FAILED
  fi

  ## check if CORE is running on remote node
  ##
  ## "Checking for L-Core on system %1$s."
  oi_verbose "opc" 773 "${node}"
  check_remote_core_status ${node}
  if [ $? -ne 0 ]
  then
    ## add message to inform user that there is no l-core
    ## detected on remote node
    ##
    ## There is no l-core detected on remote node %1$s.
    oi_verbose "opc" 781 "${node}"
    return $RET_NO_LCORE
  fi

  ## QXCR1000426621: check_remote_core_status is sufficient, there is no
  ## need to check MANAGER_ID and OPC_PRIMARY_MGR on the managed node
  ## You can still find the old code in versions of HPOM prior to A.08.29

  #  check if CORE is running with administrative rights
  #
  get_remote_agent_user_id ${node}
  case "$?" in
    0)
      as_root=1
      # "L-Core is running under administrative account \n\"
      # "      on system %1$s."
      oi_verbose "opc" 774 "${node}"
      ;;
    1)
      get_remote_config_var_value ${node} OV_SUDO ctrl.sudo
      if [ $? -eq 0 -a ${confvalue} != "\"\"" ]
      then
        #  inform user that sudo is installed on
        #  remote node
        #
        #  SUDO is installed on remote node %1$s. \n\
        #  Administrative installation is assumed.
        oi_verbose "opc" 782 "${node}"
        as_root=1
        OV_SUDO="${confvalue}"
        unset confvalue
      else
        #write note about package transfer
        #"L-Core is not running under administrative account \n\"
        #"on system %1$s. Instead packages will be transferred to"
        #"the node."
#VMS Changes Starts
        #VMS change for OV Config and Deploy is not running under administrative user account
        return $RET_NO_LCORE
#VMS Changes Ends
        oi_verbose "opc" 725 "${node}"
        as_root=0
      fi
      ;;
    *)
      return $RET_FAILED
      ;;
  esac

  ## Phase II.
  ## (De-)installation
 
  ## if this is deinstallation
  if [ "${OPC_METHOD}" = "REMOVE" ]
  then
    oi_verbose "opc" 786
    remove_bbc_packages "${node}" "${OPC_PLATFORM}"
    if [ $? -eq 0 ]
    then
      ## everything ok
      ## there should be some message about it
      ## HPOM successfully removed from system %1$s.
      oi_verbose "opc" 124 "${node}"
      return $RET_SUCCESS
    else
      ## something wrong with deployment
      ## removal failed
      ## Error occurred during deinstallation of packages.
      oi_error "opc" 769
      return $RET_FAILED
    fi
  fi

  ## installation part
  opc_inst_args="-ni"
  if [ "${OPC_FORCE_UPD}" = "YES" ]
  then
    force_option="-f"
    ## connection to lcore will be lost
    core_install=1
    opc_inst_args="${opc_inst_args} -f"
  else
    ## check which packages will be installed
    core_needs_update ${node}
    case $? in
      0)
        ## at least one of CORE packages will be updated
        core_install=1
        ;;
      1)
        core_install=0
        ;;
      2)
        ## nothing to be done - no packages to update
        return $RET_SUCCESS
        ;;
    esac
  fi

  ## Get the name of the Agent Client file 
  get_agent_client_name ${OPC_PLATFORM}/${OPC_VERSION}/${OPC_PACKAGE_TYPE}
  if [ $? -ne  0 ]
  then
    oi_error "opc" 798
    return 1
  fi
  agt_clt_name=${agent_client_name}
  unset agent_client_name  

  ## Get deployment directory from the Agent Client file
  get_xml_value ${PACKAGE_LOCATION}/${agt_clt_name} "name"
  deployment_dir=`${ECHOC} "${xml_value}"|sed "s/ //g"`
  unset xml_value

  ## Workaround for Windows: there has to be always -f option
  if [ "${OPC_PLATFORM}" = "ms/x86/winnt" -o "${OPC_PLATFORM}" = "ms/x64/win2k3" -o "${OPC_PLATFORM}" = "ms/ipf64/win2k3" ]
  then
    if [ "${OPC_FORCE_UPD}" != "YES" ]
    then
      opc_inst_args="${opc_inst_args} -f"
    fi
  fi

  ## workaround because deployment doesn't do cleanup of 
  ## ../incoming/bundles/<deployment> directory
  if [ "${OPC_PLATFORM}" = "ms/x86/winnt" -o "${OPC_PLATFORM}" = "ms/x64/win2k3" -o "${OPC_PLATFORM}" = "ms/ipf64/win2k3" ]
  then
    getDir="ovpath -native -datadir installation incoming/bundles/${deployment_dir}"
    delDir="`${OV_BIN_DIR}/ovdeploy -ovrg server -cmd \"$getDir\" -host ${node}`"
    ## if we can not get data dir cleanup on windows will not be performed
    if [ "x${delDir}" != "x" ]
    then
      delCmd="if exist \"$delDir\" del /q \"$delDir\""
      ${OV_BIN_DIR}/ovdeploy -ovrg server -cmd \
                            "$delCmd" -host ${node} >/dev/null 2>/dev/null
    fi
  else 
    ${OV_BIN_DIR}/ovdeploy -ovrg server -cmd \
          "rm -rf /var/opt/OV/installation/incoming/bundles/${deployment_dir}" \
          -host ${node} >/dev/null 2>/dev/null
  fi

  ## transfer packages
  ## TMP_FILE is set inside prepare_tmp_file
  TMP_FILE=
  prepare_tmp_file ${TMP_DIR}
  if [ $? -ne 0 ]
  then
    EXIT 1
  fi

  ## advise user that transfer of packages has started
  ## Transferring packages...
  ## QXCR1000351749: save output of ovdeploy, display it if an error occurs
  oi_verbose "opc" 791
  
  ${OV_BIN_DIR}/ovdeploy -upload \
                  -bundle ${PACKAGE_LOCATION}/${agt_clt_name} \
                  -host ${node} ${force_option} \
                  -ovrg server > ${TMP_FILE} 2>&1
  if [ $? -eq 0 ]
  then
    ## advise user that transfer has been successful
    oi_verbose "opc" 797
  else
    ## produce error message regarding failed transfer
    oi_show_file ${TMP_FILE}
    oi_error "opc" 798
    return $RET_FAILED
  fi

  CHGJOB=chg00000.job
  rm -f ${TMP_FILE}

  ## Empty file required for opcactivate to succeed
  touch ${TMP_DIR}/chg00000.job 2> /dev/null
  ${OV_BIN_DIR}/ovdeploy -upload \
		  -file ${TMP_DIR}/${CHGJOB} \
		  -host ${node} \
		  -ovrg server > ${TMP_FILE} 2>&1
  if [ $? -ne 0 ]
  then
    oi_show_file ${TMP_FILE}
  else
    opc_inst_args="${opc_inst_args} -c ../../files/${CHGJOB}"
  fi
  rm -f ${TMP_DIR}/${CHGJOB}
  unset		     CHGJOB

  rm -f ${TMP_FILE}
  unset   TMP_FILE

  ## if OPC_START_OPC is not set then it must be 1 / default behaviour
  if [ "x${OPC_START_OPC}" = "x" ]
  then
    OPC_START_OPC=1
  fi
  if [ ${OPC_START_OPC} -eq 0 ]
  then
    ## handle (re)start option
    opc_inst_args="${opc_inst_args} -no_start"
  fi

  ## QXCR1000294761: Upload Installation Profile ONLY at bootstrap time!
  ## You can still find the old code in versions of OVOU prior to A.08.23

  ## execution of installation script
  if [ ${as_root} -eq 1 ]
  then
    ## LCore running under administrative account
    ## inform user that installation script is starting
    oi_verbose "opc" 799
    case ${core_install} in
      0)
        ## connection will not be lost
        if [ "${OPC_PLATFORM}" = "ms/x86/winnt" -o "${OPC_PLATFORM}" = "ms/x64/win2k3" -o "${OPC_PLATFORM}" = "ms/ipf64/win2k3" ]
        then
          ## if node is of Windows type
          ${OV_BIN_DIR}/ovdeploy -cmd \
             "cscript opc_inst.vbs ${opc_inst_args}" \
             -host ${node} \
             -trd data \
             -td installation/incoming/bundles/${deployment_dir} \
             -ovrg server \
             >/dev/null 2>/dev/null
        else
          ## if node is of Unix type
          ## assign executable permissions to opc_inst
          ${OV_BIN_DIR}/ovdeploy -cmd \
             "chmod 550 opc_inst" \
             -host ${node} \
             -trd data \
             -td installation/incoming/bundles/${deployment_dir} \
             -ovrg server \
             >/dev/null 2>/dev/null
          if [ $? -ne 0 ]
          then
            ## error occured while assigning executable permissions
            oi_error "opc" 801
            return $RET_FAILED
          fi

          ## execute installation script
          if [ "x${OV_SUDO}" != "x" ]
          then
            ## sudo is used on remote node
            ${OV_BIN_DIR}/ovdeploy -cmd ${OV_SUDO} \
               -par "/var/opt/OV/installation/incoming/bundles/${deployment_dir}/opc_inst ${opc_inst_args}" \
               -host ${node} \
               -ovrg server \
               >/dev/null 2>/dev/null
          else
            ${OV_BIN_DIR}/ovdeploy -exe -file opc_inst \
               -par "${opc_inst_args}" \
               -host ${node} \
               -trd data \
               -td installation/incoming/bundles/${deployment_dir} \
               -ovrg server \
               >/dev/null 2>/dev/null
          fi
        fi
        ## if connection to remote Deployment happens to be lost...
	## and the remote Agent is to be started
        ## wait for node to come back (timeout is 10 minutes)
        if [ ${OPC_START_OPC} -eq 0 ]
        then
	  ## Connection is lost and agent will not be started
	  ## - inform user to check logfile on node for result
          ## and call opc_sw -deinstalled in case installation failed
          return $RET_SUCCESS
        fi
        do_polling ${node} 10
        if [ $? -ne 0 ]
        then
          ## timeout occured
          oi_error "opc" 824 "${node}"
          return $RET_FAILED
        fi

	## The Agent is running, wait till the installation script finish
	## the work, criteria - existance of lock file
	## timeout (interval between startup and finish of script) is 80 sec

        check_remote_lockfile ${node} 80
	if [ $? -ne 0 ]
	then
	  # ovdeploy has failed
	  if [ $? -eq 1 ]
	  then
            oi_error "opc" 823 "${node}"
          else
          # lock file exists but timeout occured  
            if [ "${OPC_PLATFORM}" = "ms/x86/winnt" -o "${OPC_PLATFORM}" = "ms/x64/win2k3" -o "${OPC_PLATFORM}" = "ms/ipf64/win2k3" ]
            then
              oi_error "opc" 857 "${node}" "%SystemRoot%\Temp\opc_inst.lock" 
            else
              oi_error "opc" 857 "${node}" "${INST_LOCK_FILE}" 
            fi
          fi
          return $RET_FAILED
	fi

        ## download logfile
        ${OV_BIN_DIR}/ovdeploy -download \
           -file ../../../log/opc_inst.log \
           -host ${node} \
           -ovrg server \
           >/dev/null 2>/dev/null
        if [ $? -ne 0 ]
        then
          ## error while downloading logfile
          oi_error "opc" 802
          return $RET_FAILED
        fi
        ## check logfile if installation succeeded
        check_bbc_logfile \
          ${OV_VAR_DIR}/shared/server/installation/incoming/files/opc_inst.log
        if [ $? -eq 0 ]
        then
          ## inform user that installation was successful
	  ## System %1$s successfully installed as HPOM Managed Node.
          oi_verbose "opc" 618 ${node}
          return $RET_SUCCESS
        else
          ## inform user that installation script failed
          oi_error "opc" 800
          return $RET_FAILED
        fi
        ;;
      1)
        ## at least one of CORE packages will be updated
        ## connection to Deployment will be lost
        if [ "${OPC_PLATFORM}" = "ms/x86/winnt" -o "${OPC_PLATFORM}" = "ms/x64/win2k3" -o "${OPC_PLATFORM}" = "ms/ipf64/win2k3" ]
        then
          ## if node is of Windows type
          ${OV_BIN_DIR}/ovdeploy -cmd \
             "cscript opc_inst.vbs ${opc_inst_args}" \
             -host ${node} \
             -trd data \
             -td installation/incoming/bundles/${deployment_dir} \
             -ovrg server \
             >/dev/null 2>/dev/null
        else
          ## if node is of Unix type
          ## assign executable permissions to opc_inst
          ${OV_BIN_DIR}/ovdeploy -cmd \
             "chmod 550 opc_inst" \
             -host ${node} \
             -trd data \
             -td installation/incoming/bundles/${deployment_dir} \
             -ovrg server \
             >/dev/null 2>/dev/null
          if [ $? -ne 0 ]
          then
            ## error occured while assigning executable permissions
            oi_error "opc" 801
            return $RET_FAILED
          fi

          ## execute installation script
          if [ "x${OV_SUDO}" != "x" ]
          then
            ## sudo is used on remote node
            ${OV_BIN_DIR}/ovdeploy -cmd ${OV_SUDO} \
               -par "/var/opt/OV/installation/incoming/bundles/${deployment_dir}/opc_inst ${opc_inst_args}" \
               -host ${node} \
               -ovrg server \
               >/dev/null 2>/dev/null
          else
            ${OV_BIN_DIR}/ovdeploy -exe -file opc_inst \
               -par "${opc_inst_args}" \
               -host ${node} \
               -trd data \
               -td installation/incoming/bundles/${deployment_dir} \
               -ovrg server \
               >/dev/null 2>/dev/null
          fi
        fi
	## since Deployment does not detach script
	## this is commented for now
        ##if [ $? -ne 0 ]
        ##then
        ##  ## start of installation script failed
        ##  oi_error "opc" 800
        ##  return $RET_FAILED
        ##fi

	## connection to Deployment is lost...
	## wait for node to come back (timeout is 10 minutes)
        if [ ${OPC_START_OPC} -eq 0 ]
        then
	  ## Connection is lost and agent will not be started
	  ## - inform user to check logfile on node for result
          ## and call opc_sw -deinstalled in case installation failed
          oi_verbose "opc" 618 ${node}
          return $RET_SUCCESS
	fi
        do_polling ${node} 10
        if [ $? -ne 0 ]
        then
          ## timeout occured
          oi_error "opc" 824 "${node}"
          return $RET_FAILED
	fi

	## The Agent is running, wait till the installation script finish
	## the work, criteria - existance of lock file
	## timeout (interval between startup and finish) is 80 sec

        check_remote_lockfile ${node} 80
	if [ $? -ne 0 ]
	then
          # ovdeploy has failed
          if [ $? -eq 1 ]
          then
            oi_error "opc" 823 "${node}"
          else
          # lock file exists but timeout occured
            if [ "${OPC_PLATFORM}" = "ms/x86/winnt" -o "${OPC_PLATFORM}" = "ms/x64/win2k3" -o "${OPC_PLATFORM}" = "ms/ipf64/win2k3" ]
            then
              oi_error "opc" 857 "${node}" "%SystemRoot%\Temp\opc_inst.lock"
            else
              oi_error "opc" 857 "${node}" "${INST_LOCK_FILE}"
            fi
          fi
          return $RET_FAILED
	fi

        ## download logfile
        ${OV_BIN_DIR}/ovdeploy -download \
           -file ../../../log/opc_inst.log \
           -host ${node} \
           -ovrg server \
           >/dev/null 2>/dev/null
        if [ $? -ne 0 ]
        then
          ## error while downloading logfile
          oi_error "opc" 802
          return $RET_FAILED
        fi
        ## check logfile if installation succeeded
        check_bbc_logfile \
          ${OV_VAR_DIR}/shared/server/installation/incoming/files/opc_inst.log
        if [ $? -eq 0 ]
        then
          ## inform user that installation was successful
	  ## System %1$s successfully installed as HPOM Managed Node.
          oi_verbose "opc" 618 ${node}
          return $RET_SUCCESS
        else
          ## inform user that installation script failed
          oi_error "opc" 800
          return $RET_FAILED
        fi
        ;;
    esac
  else
    ## LCore running under normal user account
    ## message to user to manually start installation script
    ## press <RETURN> to confirm this step
    if [ "${OPC_PLATFORM}" = "ms/x86/winnt" -o "${OPC_PLATFORM}" = "ms/x64/win2k3" -o "${OPC_PLATFORM}" = "ms/ipf64/win2k3" ]
    then
      oi_verbose "opc" 729 "${node}" \
             "<DataDir>\installation\incoming\bundles\${deployment_dir}\opc_inst.vbs"
    else
      oi_verbose "opc" 729 "${node}" \
             "<DataDir>/installation/incoming/bundles/${deployment_dir}/opc_inst"
    fi
    echo
    oi_verbose "opc" 368
    line
  fi

  ## inform user that installation was successful
  ## System %1$s successfully installed as HPOM Managed Node.
  oi_verbose "opc" 618 ${node}
  return $RET_SUCCESS

}

OPC_AGTINST_DIR=${OPCSVR_AGTINST_DIR}
OPC_CMD_DIR=${OPC_CMD_DIR:-${OPCSVR_INST_DIR}}
L_CORE_CHECK=lcorechk

#VMS Changes Starts
. "$OPC_AGTINST_DIR"/opcvmsproc
. "$OPC_AGTINST_DIR"/opcvmsprocs
#VMS Changes Ends

#===============================================================================
#
# STATIC VARIABLES
#
DATE_FORMAT="+%j/%Y %m%d%y %H:%M:%S"		    # date format
AL_DATE_FORMAT="+%j/%y %m%d%y %H:%M:%S"		    # date format without %Y
						    # used because SunOS 
OUTPUT_F=${OPC_TOPC}/output.data		    # tmp filename
SPKG=5						    # number of packets
						    # $OPC_NODE and server time

#===============================================================================
#
#  MAIN script body
#

# display start module
if [ "${OPC_ADEBUG}" = "OPCAYES" ]
then
    set +x
    DEBUG_CNT=`expr $DEBUG_CNT + 1`
fi
if [ "${OPC_TRACE}" = "YES" ]
then
    CALLOUT_NAME=$0
    CALLOUT_PARAMS="$*"
    #oi_trace opc 588 "${MODULE_NAME}" ${CALLOUT_NAME} "\"${CALLOUT_PARAMS}\""
    echo "TRACE:    ${TRACE_HEADER}" | \
    sed -e "s=MODULxxxxxxx=${MODULE_NAME}=" \
        -e "s=SCRIPTxxxxxx=${CALLOUT_NAME}=" \
    >> ${OPC_TRACE_FILE}
fi
if [ "${OPC_DEBUG}" = "YES" ]
then
    # enable tracing in all functions
    eval `typeset -f | awk '/^function/ { printf "typeset -tf %s\n", $2 }'`
fi
if [ "${OPC_ADEBUG}" = "OPCAYES" ]
then
    if [ ${DEBUG_CNT} -gt 0 ]
    then
	DEBUG_CNT=`expr $DEBUG_CNT - 1`
    fi
    if [ ${DEBUG_CNT} -gt 0 ]
    then
	set -x
    fi
fi

#   input parameters
#
if [ $# -ne 1 ]
then
    #	usage error
    #
    oi_error "opc" 511 "opcping"
    EXIT 2
fi
OPC_OUTPUT_FILE=$1

OPC_STATUS="NOTREADY"

#
#   first check, if node equals current system,
#   because this script can be also used for HP-UX series 700 or 800
#
opcux_localhost ${OPC_NODE}
if [ $? -eq 0 ]
then
    # this is local Managed Node: of course it is alive
    #
    OPC_STATUS="READY"
    ACTIVATE_ONLY=0
    OPC_PASSWD_REQ=0
    OPC_PASSWD=OPC_UNKNOWN_PASSWORD
    KERBEROS_BYPASS=NO
    produce_password
    EXIT 0
fi

#    node is remote; 
#    1. check, if system is responding, by using ping utility
#    NOTE: ping utility can handles only ARPA name or IP Address
#
## Bugfix for QXCR1000335622: only try to resolve the name if no
## IP address is known yet (preferred or from OVO DB)

if [ ${OPC_NODE} = ${OPC_IP_ADDRESS} ]
then
  resolve_name ${OPC_NODE} ${OPC_IP_ADDRESS}
  if [ $? -ne 0 ]
  then
    oi_error "opc" 020 ${OPC_NODE}
    produce_password
    EXIT 1
  fi
else
  PING_NODE=${OPC_IP_ADDRESS}
fi

## Bugfix for QXCR1000326046: high secure agent install waits
## forever trying SSH.
## ALWAYS try ping/bbcutil -ping first, even if SSH is configured.
## When trying SSH, display a message so that the user can
## cancel if it hangs.

do_ping ${PING_NODE}
if [ $? != 0 ]
then
  if [ "$OPC_ACCESS_METHOD" = "$SSH_METHOD" ]
  then
    oi_note "opc" 852 ${PING_NODE}
    ssh -l ${OPC_INST_USER} ${PING_NODE} date 1>/dev/null 2>&1
    if [ $? != 0 ]
    then
      ## Failed to establish SSH session. 
      ## Later on in case the remote node is reachable and SSH session
      ## still can not be stablished, the user will be prompted to use
      ## insecure FTP-based installation.
      oi_warning "opc" 841 ${PING_NODE}
    fi
  else
    EXIT 1
  fi
fi

## Note: on 06.03.97 was decided to not activate node from server
##       SO following code is put in comments
##       If it will be (hope not) activated again, also opcragt utility
##       must be corrected as well.
## START_OF_COMM
##    2. check if opcclta is alive first 
##
##do_opcragt ${OPC_NODE}
##if [ $? -eq 0 ]
##then
##    # opcctla is alive; if ITO agent is in preinstall state and 
##    # ITO version equals selected version, only activation of node
##    # is enought
##    if [ "${CA_STATE}" = "preinstalled" ]
##    then
##	if [ "${OPC_METHOD}" = "INSTALL" ]
##	then
##	    if [ "${CA_VERSION}" = "${OPC_VERSION}" ]
##	    then
##		oi_note opc 678 ${OPC_NODE}
##		OPC_STATUS="READY"
##		ACTIVATE_ONLY=1
##		OPC_PASSWD_REQ=2
##		KERBEROS_BYPASS="NO"
##		produce_password 
##		EXIT 0
##	    else
##		oi_note "opc" 684 ${CA_VERSION} ${OPC_NODE} ${OPC_VERSION}
##	    fi
##	else
##	    oi_note "opc" 683 ${CA_VERSION} ${OPC_NODE} 
##	fi
##    fi
##fi
## END_OF_COMM

ACTIVATE_ONLY=0

if [ "${OPC_PACKAGE_TYPE}" = "RPC_BBC" ]
then
  #  BBC communication type
  #
  ## Here we check if l-core is running on managed node.
  ${L_CORE_CHECK} "${OPC_NODE}"
  case "$?" in
    0)
      #  L-Core handled the installation - quit
      OPC_STATUS="INSTALLED"
      if [ "${OPC_METHOD}" = "REMOVE" ]
      then
        update_node_status ${OPC_NODE} "DEINSTALLED"
      else
        update_node_status ${OPC_NODE} "INSTALLED"
      fi
      produce_password
      EXIT 0
      ;;
    1)
      #  there is something wrong with L-Core
      #  - abort with error
      if [ "${OPC_METHOD}" = "INSTALL" ]
      then
        update_node_status ${OPC_NODE} "DEINSTALLED"
      fi
      produce_password
      EXIT 1
      ;;
#VMS Changes Starts
#     2) 
#	if [ "${OPC_METHOD}" = "REMOVE"  ] 
#	then
#		oi_error "opc" 787 ${OPC_MGMT_SV} ${OPC_NODE}
#		produce_password
#		EXIT 1
#        fi
#	;;
#VMS Changes Ends
    3)
      # there is something wrong with L-Core
      # - abort with error
      if [ "${OPC_METHOD}" = "INSTALL" ]
      then
        update_node_status ${OPC_NODE} "DEINSTALLED"
      fi
      produce_password
      EXIT 1
      ;;
  esac

else
  #  DCE/NCS communication type
  #
  if [ -f ${OV_BIN_DIR}/ovrc ]
  then
    #  check if there is lcore on remote system
    #
    ${OV_BIN_DIR}/ovrc -node "${OPC_NODE}" -status \
                  1>/dev/null 2>/dev/null
    if [ $? -eq 0 ]
    then
      #  lcore detected on remote system.
      #  Inform user that downgrade to DCE/NCS is not supported.
      #
      oi_error "opc" 783 "${OPC_NODE}"
      produce_password
      EXIT 1
    fi
  fi
fi

#    3. check password
#

#VMS Changes Starts
opcvmspwget
#VMS Changes Ends

PWRET=$?
if [ $PWRET -eq 1 ]
then
    #	invalid password specified; skip this node
    #
    oi_error "opc" 165 ${OPC_NODE}
    produce_password 
    unset PWRET
    EXIT 1
elif [ $PWRET -eq 2 ]
then
    #   remsh or rexec services not enabled
    #
    oi_note "opc" 021  ${OPC_NODE}
    produce_password
    unset PWRET
    EXIT 1
fi

#    produce password line
#
OPC_STATUS="READY"
produce_password 

if [ ${OPC_METHOD} != "INSTALL" ]
then 
    EXIT 0
fi

#    4. check, if date/time of Management Server and Managed Node are in sync.
#    If difference is greater than 10 secondes, a warning message is displayed.
#    NOTE: %Y date parameter can't be used, because SunOS doesn't support it.
#

# get remote time
#
TMP_FILE=${OPC_TOPC}/opc-$$.tmp
if [ -f ${TMP_FILE} ]
then 
    #	remove temporary file
    #
    rm -f ${TMP_FILE}
    if [ $? -ne 0 ]
    then
	#	can't write to TMP_FILE
	#
    	oi_error "opc" 526 "${TMP_FILE}"
	EXIT 1
    fi
fi
## since the server's TZ may not be known on remote node,
## GMT (UTC) will be used for calculations & reporting.

REMOTE_COMM="echo ${OIRMSG_AARGH}R \`date -u \"${AL_DATE_FORMAT}\" 2>/dev/null\`"
${OPC_REXEC_SCRIPT} -r ${TMP_FILE} "${REMOTE_COMM}"
if [ -f ${TMP_FILE} ]
then
    REMOTE_DATE=`cat ${TMP_FILE}`
    rm -f ${TMP_FILE}
    if [ -z "${REMOTE_DATE}" ]
    then
        EXIT 1
    fi
else
    EXIT 1
fi
unset TMP_FILE

# get local time
#
LOCAL_DATE=`date -u "${AL_DATE_FORMAT}" 2>/dev/null`

# get the difference
#
LOC_DATE=`echo ${LOCAL_DATE}  | awk '{print $1}'`
LOC_TIME=`echo ${LOCAL_DATE}  | awk '{print $3}'`
REM_DATE=`echo ${REMOTE_DATE} | awk '{print $1}'`
REM_TIME=`echo ${REMOTE_DATE} | awk '{print $3}'`

J_TMP=`echo ${LOC_DATE} | cut -d / -f 1`
YY_TMP=`echo ${LOC_DATE} | cut -d / -f 2`
if [ ${YY_TMP} -gt 50 ]
then
    LOC_DATE="${J_TMP}/19${YY_TMP}"
else
    LOC_DATE="${J_TMP}/20${YY_TMP}"
fi
J_TMP=`echo ${REM_DATE} | cut -d / -f 1`
YY_TMP=`echo ${REM_DATE} | cut -d / -f 2`
if [ ${YY_TMP} -gt 50 ]
then
    REM_DATE="${J_TMP}/19${YY_TMP}"
else
    REM_DATE="${J_TMP}/20${YY_TMP}"
fi

time_diff ${LOC_DATE} ${LOC_TIME} ${REM_DATE} ${REM_TIME} 10
if [ $? -eq 1 ]
then
    LOC_DATE=`echo ${LOCAL_DATE}  | awk '{print $2}'`
    REM_DATE=`echo ${REMOTE_DATE} | awk '{print $2}'`
    oi_warning "opc" 132 ${OPC_NODE} \
	       ${REM_DATE} ${REM_TIME} ${LOC_DATE} ${LOC_TIME}
fi
unset REMOTE_COMM LOCAL_DATE REMOTE_DATE
unset LOC_DATE LOC_TIME REM_DATE REM_TIME
unset J_TMP YY_TMP

EXIT 0

################################################################################
# end of opcping
################################################################################
