################################################################################
#
# File:         opcprocs.sh
# Description:  Common ITO Software Maintenance procedures 
# Language:     Bourne Shell
# Package:      HP OpenView IT/Operations
#
# (c) Copyright 1993 - 2009 Hewlett-Packard Development Company, L.P. 
#
################################################################################

  NSLOOKUP="/bin/nslookup"
  IFCONFIG="/usr/sbin/ifconfig"
  GREP="/bin/grep"
  AWK="/bin/awk"
  
###############################################################################
# get_short_hostname_from_ov
#
# uses long hostname and cuts off the domain
###############################################################################
get_short_hostname_from_ov()
{
  get_long_hostname_from_ov | cut -f1 -d"."
}

###############################################################################
# get_long_hostname_from_ov
#
# tries to do just this
###############################################################################
get_long_hostname_from_ov()
{
  echo "${HOSTNAME_OVR:-`${OV_BIN_DIR}/ovconfget -ovrg 'server' 'opc' 'OPC_MGMT_SERVER'`}"
}

###############################################################################
# get_short_hostname_from_os
#
# uses long hostname and cuts off the domain
###############################################################################
get_short_hostname_from_os()
{
  get_long_hostname_from_os | cut -f1 -d"."
}

###############################################################################
# get_long_hostname_from_os
#
# tries to do just this
###############################################################################
get_long_hostname_from_os()
{
  ###
  # there are a few items which can hold information about the FQDN:
  #
  #   `hostname`
  #   `domainname`
  #   /etc/resolv.conf: search + domain
  #   
  #   to use the standard way we try the following:
  #     use nslookup to get the whole name (see hostname(5) why)
  #
  #   to override the standard hostname detection HOSTNAME_OVR can be used
  #   or some name can be passed as an argument (e.g. used by opcdeploy)
  #   We use nslookup replacement opcsvnsl, because nslookup does not work
  #   in non DNS environment.

  _hostname=${HOSTNAME_OVR:-$1}
  _hostname=${_hostname:-`hostname`}
  if [ -n "${_hostname}" ]; then
    if [ -x ${OPCSVR_BIN_DIR}/utils/opcsvnsl ]
    then
      glhfo_fqdn=`${OPCSVR_BIN_DIR}/utils/opcsvnsl ${_hostname}|${GREP} -v "Not found"|${AWK} -F : '{print $1}'`
    else
      glhfo_fqdn=`${NSLOOKUP} "${_hostname}" 2>/dev/null | ${GREP} "Name:" | ${AWK} '{ print $2 }'`
    fi
    if [ $? -ne 0 ] || [ -z "$glhfo_fqdn" ]; then
      # no way how to use the server without running DNS
      echo "Unable to query DNS" >&2
    else
      echo ${glhfo_fqdn}
    fi
  else
    NETCONFIGFILE="/etc/rc.config.d/netconf"
    if [ -f ${NETCONFIGFILE} ]; then
      glhfo_ip="`${GREP} ^IP_ADDRESS.*= ${NETCONFIGFILE} | \
                 head -1`"
      # let's try again
      if [ -x ${OPCSVR_BIN_DIR}/utils/opcsvnsl ]
      then
        glhfo_fqdn=`${OPCSVR_BIN_DIR}/utils/opcsvnsl ${_hostname}|${GREP} -v "Not found"|${AWK} -F : '{print $1}'`
      else
        glhfo_fqdn=`${NSLOOKUP} "${_hostname}" 2>/dev/null | ${GREP} "Name:" | ${AWK} '{ print $2 }'`
      fi
      if [ $? -ne 0 ] || [ -z "$glhfo_fqdn" ]; then
        # no way how to use the server without running DNS
        echo "Hostname not set and unable to query DNS" >&2
      else
        echo ${glhfo_fqdn}
      fi
      
    else
      # no idea what to to now
      echo "Unable to determine local node address/hostname" >&2
    fi
  fi
}

###############################################################################
# get_ip_from_hostname
#
# uses long hostname and cuts off the domain
###############################################################################
get_ip_by_hostname()
{
  if [ -x ${OPCSVR_BIN_DIR}/utils/opcsvnsl ]
  then
    ${OPCSVR_BIN_DIR}/utils/opcsvnsl $1 |${GREP} -v "Not found" |${AWK} '{print $2}'
  else
    ${NSLOOKUP} $1 2>/dev/null | \
                      ${AWK} 'BEGIN      { found = 0 }
			   /^Name:/   { found = 1 }
			   /^Address/ { sub(",","")
					if ( found == 1 )
					for ( i = 2; i <= NF; i++ ) 
					  printf "%s ", $i
                                      }'
  fi
}

###############################################################################
# get_ip_from_hostname
#
# uses long hostname and cuts off the domain
###############################################################################
get_hostname_by_ip()
{
  if [ -x ${OPCSVR_BIN_DIR}/utils/opcsvnsl ]
  then
    ${OPCSVR_BIN_DIR}/utils/opcsvnsl $1 |${GREP} -v "Not found"|${AWK} -F : '{print $1}'
  else
    ${NSLOOKUP} $1 2>/dev/null | ${GREP} "Name:" | ${AWK} '{ print $2 }'
  fi
}

# get long hostname

get_hostname()
{
  _node=`uname -n 2>/dev/null`

  _host=`hostname_s2l $_node`

  unset _node
  echo "$_host"
  return 0
}

hostname_s2l()
{
  _node=$1
  _host=$_node

  # get full system name
  if [ -z "`echo $_node|sed -e 's/[^.]//g'`" ]
  then
    # hostname is only short; call nslookup to get fully
    # qualified hostname
    if [ -x ${OPCSVR_BIN_DIR}/utils/opcsvnsl ]
    then
      _host=`${OPCSVR_BIN_DIR}/utils/opcsvnsl ${_node}|${GREP} -v "Not found"|${AWK} -F : '{print $1}'`
    else
      _host=`${NSLOOKUP} "${_node}" 2>/dev/null | ${GREP} "Name:" | ${AWK} '{ print $2 }'`
    fi
    if [ -z "$_host" ]
    then
      # unexpected problems calling nslookup
      echo "$_node"
      return 1
    else
      if [ "$_host" = "$_node" ]
      then
         # WARNING !!! Long hostname for Management Server not available
         echo "$_node"
         return 2
      fi
    fi
  fi
  # else uname delivers already long hostname
 
  unset _node
  echo "$_host"
  return 0
}

#==============================================================================
#
#  DESCRIPTION
#    This file should be read by all shell scripts which
#    are part of ITO software maintenance for Managed Nodes and execute
#    on Management Server.
#
#==============================================================================
#
#  DESCRIPTION
#    Debugging handling part
#    At first is the common code which must be at start of each
#    callout shell script to enamble local (server) debug mode.
#   
#    Global environment variables are set ht inst.sh script and are 
#    used here to determinate if debug is required.
#    To make debug if decisions simple environmnet variables from
#    debug config file are translated into variables which can be inhertated
#    (except MODULE_NAME):
#
#      MODULE_NAME .. names module of this script. When this variable match
#                     the value of OPC_DEBUG_CALLOUT variable, then this
#                     script, all callout scripts called by this script and
#                     all by this script remote executed scripts 
#                     are canditates for debugging.
#
#      OPC_DEBUG   .. controls local debugging
#      OPC_ADEBUG  .. suspend some functions from local debugging
#      DEBUG_CNT   .. counter used for correct handling of
#                     nested functions which are suspend from debugging
#
#      OPC_RDEBUG  .. controls remote debugging
#
#      OPC_2DEVNULL .. this variable should be used when standard error of
#                      local command is redirected to /dev/null.  
#                      If "> /dev/null" is hard coded, then local 
#                      debugging is inpossible for such command,
#      OPC_R2DEVNULL ..This variable should be used in remote executed command
#                      string when stderr is redirected to /dev/null.
#      ECHO_RDEBUG  .. defines how echo will be placed on debug logfile
#      REMSH_RDEBUG .. defines how stderror of remote shell will go on debug
#                      logfile
#                     
#    For details, see code of opcuxrexec()
#
#

# at first set clobber here, since several script redirect output on
# (existing) tmp files.
set +C                              # set clobber

#
# is this script in the list of debugging modules
#
if [ -n "${MODULE_NAME}" ]
then
    DEBUG_MOD=`echo ${OPC_DEBUG_CALLOUT} | grep -c "${MODULE_NAME}"`
else
    DEBUG_MOD=0
fi

if [ $DEBUG_MOD -gt 0 ]
then
    #
    # Yes, it is! Now set some variables according to global ones
    #
    if [ "${OPC_DEBUG_LOCAL}" = "YES" ]
    then
	# Local debug is required
	#
	if [ "${OPC_DEBUG}" = "YES" ]
	then
	    # mark the fact that this is the first time to redirect stderr
	    #
	    DEBUG_REDIRECTED="TRUE"
	fi
	OPC_DEBUG="YES"; export OPC_DEBUG
	if [ "${OPC_DEBUG_ALL}" = "NO" ]
	then
	    # message handling functions are suspended from debugging
	    #
	    OPC_ADEBUG="OPCAYES"; export OPC_ADEBUG
	fi
    fi
    if [ "${OPC_DEBUG_REMOTE}" = "YES" ]
    then
	# Remote debug is required
	#
	OPC_RDEBUG="YES"; export OPC_RDEBUG
    fi
else
    # do nothing ; just preserve the inheritated environment
    DEBUG_REDIRECTED="TRUE"
fi

#
# OPC_DEBUG , OPC_ADEBUG and OPC_RDEBUG
# are set in previous code or are inheritated from calling script
#
if [ "${OPC_DEBUG}" = "YES" ]
then
    # local debug is enabled -> display starting message and set -x 
    # stderr redirection to /dev/null is disabled
    # stderr redirected to file 
    OPC_2DEVNULL=""
    SCRIPT_NAME=$0
    if [ "$DEBUG_REDIRECTED" != "TRUE" ]
    then 
	exec 2>>${OPC_DEBUG_FILE}
    fi
    echo "*** START LOCAL ${MODULE_NAME} [${SCRIPT_NAME}]" 1>&2
    echo "${OPC_DEBUG_LINE}" 1>&2
    set -x
    
else
    # stderr goes to /dev/null
    OPC_2DEVNULL="2>/dev/null"
fi
if [ "${OPC_ADEBUG}" = "OPCAYES" ]
then
    # no debugging of some functions -> set counter to starting value 
    #
    DEBUG_CNT=${DEBUG_CNT:-1}
    export DEBUG_CNT
fi
if [ "${OPC_RDEBUG}" = "YES" ]
then
    # remote debug is enabled 
    OPC_R2DEVNULL=""
    if [ "${OPC_DEBUG}" = "YES" ]
    then
	# also local debug is on -> no need to redirect stderr for remote 
	# code
	ECHO_RDEBUG=">&2"
	REMSH_RDEBUG=""
    else
	# only remote debug is on -> redirect stderr for remote operations
	ECHO_RDEBUG=">>${OPC_DEBUG_FILE}"
	# Note: no space in following string (eval does not work otherwise)
	REMSH_RDEBUG="2>>${OPC_DEBUG_FILE}"
    fi
else
    # remote redirection to /dev/null
    OPC_R2DEVNULL="2>/dev/null"
fi

#==============================================================================
#
#  Read the debug configuration file and set global debug environment variables
#  Configuration file is read only when owner is root.
#  After the file is read, following environment variables are checked and
#  exported:
#  Environment variables for command tracing
#    OPC_DEBUG_LOCAL  .. enables local debugging
#    OPC_DEBUG_REMOTE .. enables remote debugging
#    OPC_DEBUG_FILE   .. debug logfile name, set to /dev/null when file
#                        can't be created
#    OPC_DEBUG_APPEND .. controls if debug file will be appended or overwritten
#    OPC_DEBUG_ALL    .. suspends local debugging of message handling
#                        common shell functions (oi_errror..)
#  Environment variables for event tracing
#    OPC_TRACE        .. enables event tracing
#    OPC_TRACE_FILE   .. trace logfile name; equals installation logfile
#
oi_init_debug()
{
    OPC_DEBUGF=${OPC_TMPFILES_DIR}/inst_debug.conf
    if [ -f ${OPC_DEBUGF} ]
    then
	# check is debug configuration file is is owned by root
	T_USER=`id -u -n`
	T_FILE=`find ${OPC_TMPFILES_DIR} -name inst_debug.conf -user ${T_USER}`
	if [ -n "${T_FILE}" ]
	then
	    . ${OPC_DEBUGF}
	fi
	unset T_USER T_FILE
    fi

    if [ -n "${OPC_DEBUG_LOCAL}" -o -n "${OPC_DEBUG_REMOTE}" ]
    then 
	if [ -z "${OPC_DEBUG_FILE}" ]
	then
	    OPC_DEBUG_FILE=/dev/null
	else
	    if [ "${OPC_DEBUG_APPEND}" != "YES" ]
	    then
		rm -f ${OPC_DEBUG_FILE}
	    fi
	    touch ${OPC_DEBUG_FILE}
	    if [ ! -w ${OPC_DEBUG_FILE} ]
	    then
		OPC_DEBUG_FILE=/dev/null
	    else
		chmod 600 ${OPC_DEBUG_FILE}
		chown `id -u -n` ${OPC_DEBUG_FILE}
	    fi
	fi
	if [ -z "${OPC_DEBUG_CALLOUT}" ]
	then
	    OPC_DEBUG_CALLOUT="dummy"
	fi
	if [ -z "${OPC_DEBUG_ALL}" ]
	then
	    OPC_DEBUG_ALL="NO"
	fi
	I_USR=root              # has been checked previously
	I_DATE_STR=`TZ=${OPC_TIME_ZONE} date +${I_DATE}`
	I_TIME_STR=`TZ=${OPC_TIME_ZONE} date +${I_TIME}`
	oi_debug opc 012 >> ${OPC_DEBUG_FILE}
	oi_debug opc 013 ${CMD_NAME} ${I_USR} ${I_DATE_STR} ${I_TIME_STR} \
		 >> ${OPC_DEBUG_FILE}
	oi_debug opc 014 >> ${OPC_DEBUG_FILE}
	OPC_DEBUG_LINE=`oi_debug opc 598`
	export OPC_DEBUG_LINE
    fi

    export OPC_DEBUG_FILE OPC_DEBUG_LOCAL OPC_DEBUG_REMOTE
    export OPC_DEBUG_CALLOUT OPC_DEBUG_ALL

    if [ "${OPC_TRACE}" = "YES" ]
    then
        LANG=$OPC_CALL_LANG TRACE_HEADER=`oi_msg opc 588 MODULxxxxxxx \
	                                                 SCRIPTxxxxxx`
	export TRACE_HEADER
    fi
    OPC_TRACE_FILE=${OPC_LOGF}
    export OPC_TRACE OPC_TRACE_FILE

    return 0
}

#==============================================================================
#
#  Print debug  message on stdout. 
#  No message qualifier is used.
#
oi_debug()
{

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

    CAT_DEB=$1; shift
    MSG_DEB=$1; shift
    PAR_DEB="$*"
    LANG=$OPC_CALL_LANG echo `oi_msg "$CAT_DEB" "$MSG_DEB" "$PAR_DEB"`
    unset PAR_DEB CAT_DEB MSG_DEB

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    return 0
}

#==============================================================================
#
#  Print TRACE message on trace logfile
#  dependent on of OPC_TRACE mode.
#
oi_trace()
{

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

    if [ "${OPC_TRACE}" = "YES" ]
    then
	CAT_DEB=$1; shift
	MSG_DEB=$1; shift
	PAR_DEB=""
	while [ $# != 0 ]
	do
	    PAR_DEB="${PAR_DEB} "`echo $1 | sed -e 's/ /_/g'`
	    shift
	done
	LANG=$OPC_CALL_LANG TEXT_L=`oi_msg "$CAT_DEB" "$MSG_DEB" "$PAR_DEB"`
	echo "TRACE:    $TEXT_L" >> $OPC_TRACE_FILE
	unset PAR_DEB CAT_DEB MSG_DEB TEXT_L
    fi

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

}

#==============================================================================
#
#  Before exit from callout shell script, record this on debug logfile
#  and generate event tracing message too.
#
EXIT()
{
    EXIT_VAL=$1
    oi_trace opc 589 ${MODULE_NAME} ${CALLOUT_NAME} ${EXIT_VAL}

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

    if [ "${OPC_DEBUG}" = "YES" ]
    then
	echo "${OPC_DEBUG_LINE}" 1>&2
	echo "*** END   LOCAL ${MODULE_NAME} [${SCRIPT_NAME}]" 1>&2
        # disable tracing in all functions
        eval `typeset -f | awk '/^function/ { printf "typeset +tf %s\n", $2 }'`
    fi

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    exit ${EXIT_VAL}
}

#==============================================================================
#
#  DESCRIPTION
#    Message handling functions display messages to standard output with
#    respect to requested platform message catalog. All messages are uniquelly
#    formatted according to the standard imposed by SD for control scripts.
#    Environment shell variables are set according to specification in
#    oi_environ(1m).
#    Parameters passed to shell scripts are decoded as far as necessary and 
#    always quoted to avoid problems with empty parameters during shell
#    function calls.
#    Local shell function variables are distinguished by function name to
#    avoid unsetting problems when functions are from other functions.
#
#  PARAMETERS
#    All messaging shell functions have common parameter interface.
#
#    OPC_OS_NICKNAME or
#    "opc"             When message from platform dependent catalog is required,
#                      nickname of that platform must be specified.
#                      Catalog named <OPC_OS_NICKNAME>inst.cat will be used.
#                      When message from global catalog (opcinst.cat) is
#                      required this parameter must be set to string "opc".
#    message_number .. Message number in corresponding catalog.
#    parameters     .. List of parameters needed by message. may be empty.
#    
#  EXIT CODE
#    0 .. always 0
#

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=/usr/bin/grep

# 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=A.08.34

# 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}}
# /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}/lib/hpux32}
# /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}/lib/hpux32}
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:/usr/etc:/usr/ucb"}
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 Unix A.08.34"

# common umask value
umask 022

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

# shared library suffix
SHLIBSUFFIX=so

#------------------------------------------------------------------------------#
# Function    : ha_is_active_on_localhost
# Description : Check if HA OVO Server is running on this host 
# Output      : 0 - HA OVO Server is running on this host
#               1 - HA OVO Server is not running on this host
#------------------------------------------------------------------------------#
ha_is_active_on_localhost()
{
  # get resource group name from OVO Server configuration, if OVO Server is not
  # active on current node, resource group name is empty
  _ha_resource_group=`/opt/OV/bin/ovconfget -ovrg 'server' 'opc' 'HA_RESOURCE_GROUP'`
  if [ "${_ha_resource_group}" != "" ]
  then
    # check the status of the resource group
    _status=$(/opt/OV/bin/ovclusterinfo -group ${_ha_resource_group} -localState)
    if [ "${_status}" = "Online" ]
    then
      return 0
    else
      return 1
    fi
  else 
    return 1
  fi
}

get_info()
{
  _file=$1
  _field=$2

  # Use XPL config for opc[sv]info
  if [ "`basename ${_file}`" = "opcinfo" ]
  then
    ${OV_BIN_DIR}/ovconfget "eaagt" "${_field}" 2>/dev/null
    return $?
  elif [ "`basename ${_file}`" = "opcsvinfo" ]
  then
    ${OV_BIN_DIR}/ovconfget -ovrg "server" "opc" "${_field}" 2>/dev/null
    return $?
  fi

  # Not opc[sv]info, use classic format
  if [ ! -f $_file ]
  then
    return 1
  else
    awk '$1 == field {for ( i=2; i<=NF; i++ ){printf("%s",$i);if (i < NF) printf(" ")}}' field=$_field $_file
  fi

  return $?
}

# also PATH must be re-set in callout scripts
PATH=$SYSTEM_PATH:$OPC_CMD_DIR:$OPC_AGTINST_DIR	    #callouts are accessible
export PATH

#==============================================================================
#
#  Initialize error, warning and note counters for OPC_NODE in 
#  OPC_RESULT_FILE by including "zero" entry into OPC_RESULT_FILE 
#  (needed when no message is displayed for node).
#
oi_init_result()
{
    echo "$OPC_MNGD_NODE $OPC_NODE 0 0 0" >>$OPC_RESULT_FILE
}

#==============================================================================
#
#  Print ERROR message on stderr and on installation and error logfile.
#  Then ask for error confirmation.
#  If node specific error counting is enabled, write error record.
#
oi_error()
{

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

    CAT_ERR=$1; shift
    MSG_ERR=$1; shift
    LANG=$OPC_CALL_LANG PAR_ERR="$*"

    LANG=$OPC_CALL_LANG ERROR_TXT=`oi_msg "$CAT_ERR" "$MSG_ERR" "$PAR_ERR"`

    LANG=$OPC_CALL_LANG echo "${OPC_ERROR_KWD}$ERROR_TXT"   #1>&2
    LANG=$OPC_CALL_LANG echo "${OPC_ERROR_KWD}$ERROR_TXT"   >> $OPC_LOGF
    LANG=$OPC_CALL_LANG echo "${OPC_ERROR_KWD}$ERROR_TXT"   >> $OPC_ERR_LOGF

    ELC_SILENT=$OPC_SILENT
    OPC_SILENT=0
    echo                                          #1>&2
    oi_s_echo "          " "opc" 004              #1>&2
    TRASH=`line -t $OPC_TIME_OUT`
    if [ $? -ne 0 ]
    then				# time out has been occurred
    	echo  				# generate new line
    	oi_warning "opc" 133 $OPC_TIME_OUT
    fi
    OPC_SILENT=$ELC_SILENT
  
    if [ $OPC_ENABLE_ERRCNT = 1 ]
    then
	echo "$OPC_MNGD_NODE $OPC_NODE 1 0 0" >>$OPC_RESULT_FILE
    fi
    unset CAT_ERR MSG_ERR ERROR_TXT TRASH PAR_ERR ELC_SILENT

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    return 0
}

#==============================================================================
#
#  Print ERROR message on stderr and on installation and error logfile,
#  but don't ask for error confirmation; intended for remote script's error
#  messages, generated via oi_rmessage(1m) interface.
#
#  If node specific error counting is enabled, write error record.
#
oi_errmsg()
{

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

    CAT_ERR=$1; shift
    MSG_ERR=$1; shift
    LANG=$OPC_CALL_LANG PAR_ERR="$*"

    LANG=$OPC_CALL_LANG ERROR_TXT=`oi_msg "$CAT_ERR" "$MSG_ERR" "$PAR_ERR"`

    LANG=$OPC_CALL_LANG echo "${OPC_ERROR_KWD}$ERROR_TXT"   #1>&2
    LANG=$OPC_CALL_LANG echo "${OPC_ERROR_KWD}$ERROR_TXT"   >> $OPC_LOGF
    LANG=$OPC_CALL_LANG echo "${OPC_ERROR_KWD}$ERROR_TXT"   >> $OPC_ERR_LOGF

    if [ $OPC_ENABLE_ERRCNT = 1 ]
    then
	echo "$OPC_MNGD_NODE $OPC_NODE 1 0 0" >>$OPC_RESULT_FILE
    fi
    unset CAT_ERR MSG_ERR ERROR_TXT TRASH PAR_ERR

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    return 0
}

#==============================================================================
#
#  Print WARNING message on stderr and on logfile
#  independent of OPC_SILENT mode.
#
oi_warning()
{

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

    CAT_WARN=$1; shift
    MSG_WARN=$1; shift
    LANG=$OPC_CALL_LANG PAR_WARN="$*"
  
    LC_SILENT=$OPC_SILENT
    OPC_SILENT=0
    oi_s_echo "$OPC_WARNING_KWD" "$CAT_WARN" "$MSG_WARN" "$PAR_WARN"  #1>&2
    OPC_SILENT=$LC_SILENT

    WARNING_TXT=`oi_msg "$CAT_WARN" "$MSG_WARN" "$PAR_WARN"`
    LANG=$OPC_CALL_LANG echo "${OPC_WARNING_KWD}$WARNING_TXT"      >> $OPC_LOGF
  
    if [ $OPC_ENABLE_ERRCNT = 1 ]
    then
	echo "$OPC_MNGD_NODE $OPC_NODE 0 1 0" >>$OPC_RESULT_FILE
    fi
    unset CAT_WARN MSG_WARN PAR_WARN WARNING_TXT LC_SILENT

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    return 0
}
  
#==============================================================================
#
#  Print NOTE message on stderr and on logfile
#  independent of OPC_SILENT mode.
#
oi_note()
{

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

    CAT_NOTE=$1; shift
    MSG_NOTE=$1; shift
    LANG=$OPC_CALL_LANG PAR_NOTE="$*"
    NOTE_TXT=`oi_msg "$CAT_NOTE" "$MSG_NOTE" "$PAR_NOTE"`
  
    NOTE_SILENT=$OPC_SILENT
    OPC_SILENT=0
    oi_s_echo "$OPC_NOTE_KWD" "$CAT_NOTE" "$MSG_NOTE" "$PAR_NOTE"  #1>&2
    OPC_SILENT=$NOTE_SILENT
    LANG=$OPC_CALL_LANG echo "${OPC_NOTE_KWD}$NOTE_TXT"    >> $OPC_LOGF
  
    if [ $OPC_ENABLE_ERRCNT = 1 ]
    then
	echo "$OPC_MNGD_NODE $OPC_NODE 0 0 1" >>$OPC_RESULT_FILE
    fi
    unset PAR_WARN NOTE_TXT PAR_WARN MSG_WARN NOTE_SILENT

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    return 0
}

#==============================================================================
#
#  Print progress message on stdout and on logfile
#  depending on OPC_SILENT mode. Blank message qualifier is used.
#  When called without any parameters empty line is produced on log and
#  on screen.
#
oi_verbose()
{

    if [ $# = 0 ]
    then
	echo "          "
	echo "          " >>$OPC_LOGF
   	return 0 
    fi

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

    CAT_VER=$1; shift
    MSG_VER=$1; shift
    LANG=$OPC_CALL_LANG PAR_VER="$*"
    oi_s_echo "          " "$CAT_VER" "$MSG_VER" "$PAR_VER"
    LANG=$OPC_CALL_LANG TEXT=`oi_msg "$CAT_VER" "$MSG_VER" "$PAR_VER"`
    LANG=$OPC_CALL_LANG echo "          $TEXT" >> $OPC_LOGF
    unset PAR_VER CAT_VER MSG_VER TEXT

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    return 0
}

#==============================================================================
#
#  Print progress message on stdout and on logfile without NewLine
#  depending on OPC_SILENT mode. Blank message qualifier is used.
#
oi_verbose_no_nl()
{

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

    CAT_VER=$1; shift
    MSG_VER=$1; shift
    LANG=$OPC_CALL_LANG PAR_VER="$*"
    oi_s_echo_no_nl "          " "$CAT_VER" "$MSG_VER" "$PAR_VER"
    LANG=$OPC_CALL_LANG TEXT=`oi_msg "$CAT_VER" "$MSG_VER" "$PAR_VER"`
    LANG=$OPC_CALL_LANG echo "          $TEXT \c" >> $OPC_LOGF
    unset PAR_VER CAT_VER MSG_VER TEXT

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    return 0
}

#==============================================================================
#
#  Print progress message only on logfile.
#
oi_log()
{

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

    CAT_LOG=$1; shift
    MSG_LOG=$1; shift
    LANG=$OPC_CALL_LANG PAR_LOG="$*"
    LANG=$OPC_CALL_LANG TEXT_L=`oi_msg "$CAT_LOG" "$MSG_LOG" "$PAR_LOG"`
    LANG=$OPC_CALL_LANG echo "          $TEXT_L" >> $OPC_LOGF
    unset PAR_LOG CAT_LOG MSG_LOG TEXT_L

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    return 0
}

#==============================================================================
#
#  Display contents of file on stderr and write it to logfile.
#
oi_show_file()
{
    FL=$1
    cat  $FL                  #1>&2
    LANG=$OPC_CALL_LANG echo "`cat $FL`"  >>  $OPC_LOGF
    unset FL
    return 0
}

#==============================================================================
#
#  Write contents of file only to logfile, not to stderr
#
oi_log_file()
{
    FL=$1
    LANG=$OPC_CALL_LANG echo "`cat $FL`"  >>  $OPC_LOGF
    unset FL
    return 0
}

#==============================================================================
#
#  Displays message on stdout.
#  First parameter is message severity string.
#  Echoing depends on the OPC_SILENT mode.
#
oi_s_echo()
{
    if [ $OPC_SILENT -eq 0 ]
    then

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

    SEVERITY=$1; shift
	CAT_ECHO=$1; shift
	MSG_ECHO=$1; shift
	LANG=$OPC_CALL_LANG PAR_ECHO="$*"
    	LANG=$OPC_CALL_LANG TEXT_E=`oi_msg "$CAT_ECHO" "$MSG_ECHO" "$PAR_ECHO"`
    	LANG=$OPC_CALL_LANG echo "${SEVERITY}$TEXT_E"
	unset SEVERITY CAT_ECHO MSG_ECHO TEXT_E PAR_ECHO

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    fi
    return 0
}

#==============================================================================
#
#  Displays message on stdout without new line.
#  First parameter is message severity string.
#  Echoing depends on the OPC_SILENT mode.
#  This function is necessary for prompting etc.
#  Note that PARMS must be extracted to allow empty parameters.
#
oi_s_echo_no_nl()
{
    if [ $OPC_SILENT -eq 0 ]
    then

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

	SEVERITY=$1; shift
	CAT_ECHON=$1; shift
	MSG_ECHON=$1; shift
	PAR_ECHON="$*"
    	LANG=$OPC_CALL_LANG TEXT_EN=`oi_msg "$CAT_ECHON" "$MSG_ECHON" "$PAR_ECHON"`
    	LANG=$OPC_CALL_LANG echo "${SEVERITY}$TEXT_EN \c"
	unset SEVERITY CAT_ECHON MSG_ECHON TEXT_EN PAR_ECHON

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    fi
    return 0
}

#==============================================================================
#
#  Get corresponding message from message catalog and display it on stdout.
#  Message catalog is selected according to the value of the first parameter.
#  If first parameter is -s, then message wrapparound formatting is suppressed.
#
oi_msg()
{

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

    NICE_FLAG=n
    if [ "$1" = "-s" ]
    then
	shift
	NICE_FLAG=""
    fi 
    CATALOG=${1}inst.cat; shift

    if [ "x$LC_MESSAGES" != "x" ]
    then
	if [ -f ${OPCSVR_NLS_DIR}/${LC_MESSAGES}/${CATALOG} ]
        then
	 CATALOG=${OPCSVR_NLS_DIR}/${LC_MESSAGES}/${CATALOG}
        else
	 CATALOG=${OPCSVR_NLS_DIR}/C/${CATALOG}
        fi
    else
        if [ -f ${OPCSVR_NLS_DIR}/${OPC_CALL_LANG}/${CATALOG} ]
        then 
          CATALOG=${OPCSVR_NLS_DIR}/${OPC_CALL_LANG}/${CATALOG}
        else
          CATALOG=${OPCSVR_NLS_DIR}/C/${CATALOG}
        fi
    fi

    MSG_MSG=$1; shift
    PAR_MSG="$*"
    LANG=$OPC_CALL_LANG $OPC_CMD_DIR/opc_getmsg "-"${NICE_FLAG}c "$CATALOG" 10 "$MSG_MSG" $PAR_MSG
    if [ $? -ne 0 ]
    then
    	echo "          unexpected problem accessing the message catalogue" 
    	echo "          message set: 10; message number: $MSG_MSG"         

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

	return 1
    fi
    unset CATALOG MSG_MSG PAR_MSG

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    return 0
}

#==============================================================================
#
#  Get corresponding message from message catalog and display it on stdout
#  without new line. Message catalog is selected according to the value
#  of the first parameter. 
#
oi_msg_no_nl()
{

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

    CATALOG=${1}inst.cat; shift

    if [ "x$LC_MESSAGES" != "x" ]
    then
	if [ -f ${OPCSVR_NLS_DIR}/${LC_MESSAGES}/${CATALOG} ]
        then
	 CATALOG=${OPCSVR_NLS_DIR}/${LC_MESSAGES}/${CATALOG}
        else
	 CATALOG=${OPCSVR_NLS_DIR}/C/${CATALOG}
        fi
    else
        if [ -f ${OPCSVR_NLS_DIR}/${OPC_CALL_LANG}/${CATALOG} ]
        then 
          CATALOG=${OPCSVR_NLS_DIR}/${OPC_CALL_LANG}/${CATALOG}
        else
          CATALOG=${OPCSVR_NLS_DIR}/C/${CATALOG}
        fi
    fi

    MSG_MSG=$1; shift
    PAR_MSG="$*"
    MSG=`LANG=$OPC_CALL_LANG $OPC_CMD_DIR/opc_getmsg \
	      -nc "$CATALOG" 10 "$MSG_MSG" $PAR_MSG`
    if [ $? -ne 0 ]
    then
    	echo "          unexpected problem accessing the message catalogue" 
    	echo "          message set: 10; message number: $MSG_MSG"         

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

	return 1
    fi
    LANG=${OPC_CALL_LANG} echo "${MSG}\c"
    unset CATALOG MSG_MSG MSG PAR_MSG

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    return 0
}

#==============================================================================
#
#  DESCRIPTION
#    Prompt and ask for answer.
#    If timeout on input has occurred, display warning and inform
#    operator that execution has continued.
#    Return entered string or string "defauuult" if timeout occurred.
#    When empty sting is entered, it is converted to "defauuult".
#
#  PARAMETERS
#    OPC_OS_NICKNAME or "opc"  .. catalog selection
#    warn_msgn             .. message number of warning in case of time out
#    wpar1                 .. first parameter to warning message: use "" for em.
#    wpar2                 .. second parameter for warning message
#    prompt_msgn           .. message number of prompt line
#    prompt_parameters     .. the rest of line are prompt message parameters
#
oi_prompt_and_ask()
{

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

    PR_CATALOG=$1; shift
    PRW_MSG=$1; shift
    PRW_PAR1="$1"; shift
    PRW_PAR2="$1"; shift
    PROMPT_MSG=$1; shift
    PR_PARMSG="$*"

    PLC_SILENT=$OPC_SILENT
    OPC_SILENT=0
    oi_msg_no_nl $PR_CATALOG $PROMPT_MSG $PR_PARMSG 
    OPC_ANSWER=`line -t $OPC_TIME_OUT`
    if [ $? -ne 0 ]
    then
	# time out has been occurred
	echo  # generate new line
	oi_warning "opc" 133 $OPC_TIME_OUT
	oi_warning $PR_CATALOG $PRW_MSG $PRW_PAR1 $PRW_PAR2
	OPC_ANSWER=$OPC_OIANSWER_DEF
    else
	if [ "$OPC_ANSWER" = "" ]
	then 
	    OPC_ANSWER=$OPC_OIANSWER_DEF
	fi
    fi
    OPC_SILENT=$PLC_SILENT
    unset PLC_SILENT

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    return 0
}

#==============================================================================
#
#   Global definitions
#
OIRMSG_AARGH="@@@"			# Escape string prefix.
OIRMSG_NOEXIT=3				# Default exit code in case of no EXIT

#==============================================================================
#
#  DESCRIPTION
#    Check if specified name designates local host (i.e. Management Server)
#    Long and short name of the local host are kept in global variables
#    OPC_MGMT_SV and OPC_MGMT_SHORT_SV, respectively.
#
#  PARAMETERS
#    name .. Host name to be checked.
#
#  ENVIRONMENT
#    OPC_MGMT_SHORT_SV  Short name of the Management Server.
#                   remains set upon exit for subsequent use.
#    OPC_MGMT_SV    Long name of the Management Server.
#                   Set to the output of `hostname` when nonexistent.
#                   Remains set upon exit for subsequent use.
#
#  EXIT CODE
#    0 .. Name designates local host (i.e. Management Server)
#    1 .. Name designates remote host.
#

opcux_localhost()
{
    HNAME=$1                	    # Name to check...

    #  FIX SMDcs03810 - too many (21 or more) warning for short name
    #  warning is displayed only when variables are not set and equal
    #
    if [ -z "${OPC_MGMT_SV}" ]; then # Get MGMT_SV and MGMT_SHORT_SV
	    if ha_is_active_on_localhost; then
	      # get the long mgmt server name from the opcsvinfo file
	      OPC_MGMT_SV=`get_long_hostname_from_ov` 
	    else
	      OPC_MGMT_SV=`get_long_hostname_from_os`
	    fi
    fi
    if [ -z "${OPC_MGMT_SHORT_SV}" ]; then
	    if ha_is_active_on_localhost;	then
        OPC_MGMT_SHORT_SV=`get_short_hostname_from_ov`
	    else
	      OPC_MGMT_SHORT_SV=`get_short_hostname_from_os`
	    fi
    fi

    if [ "${HNAME}" = "${OPC_MGMT_SV}" -o "${HNAME}" = "${OPC_MGMT_SHORT_SV}" ]
    then
        unset HNAME
        return 0
    else
        unset HNAME
        return 1
    fi
}

#==============================================================================
#
#  DESCRIPTION
#    Remote execution for installation scripts already on Managed Nodes.
#    Script execution utility is either Bourne shell, remsh(1) or opc_rexec(1m).
#    Bourne shell is used when destination node is actually the local host;
#    remsh(1) is used when remote node doesn't require root password,
#    opc_rexec(1m) is used when root password must be supplied.
#    
#    Remote script's output is filtered through opcrmf (see below) to handle
#    escape strings, generated by oi_rmessage(1m) functions, which enable
#    remote script to output generic and platform-specific installation messages
#    append lines to a result file and specify exit values.
#    All other output is left unchanged.
#
#  ATTENTION
#    Text flow is undeterministic when you print messages on standard error
#    output in the remote scripts.
#    A hint: opc_rexec merges standard and error output on the same stream;
#    remsh uses separate streams.
#
#  PARAMETERS
#    -r[esult] <file> .. Specifies file to be used as a result output
#    cmd [<args> ...] .. Command to be executed
#
#  EXIT CODE
#    Normaly exit code from the remote command;  local exit codes include:
#      1 .. Unrecognized option or not enough arguments
#      2 .. Problems with remsh(1) or opc_rexec(1m)
#      3 .. Default exit code, when remote script terminated without EXIT.
#

opcuxrexec()
{

    while [ $# != 0 -a "${URX_CMD}" = "" ]
    do
	case "$1" in
	    -r*) URX_RESULT=$2;  shift; shift
	    	 ;;
	    *)   URX_CMD="$*"
		 ;;
	esac
    done
    if [ $# = 0 ]
    then
    	return 1
    fi

    if [ "${OPC_PASSWD_REQ}" = 2 ]
    then
	# for this node only remote script can be execute.
	# Use opcuxrcpsh() for this purpose.
    	return 1
    fi

    #
    #  Determine pathname to the Bourne shell on the Managed node.
    #
    case "${OPC_PLATFORM}" in
    	hp/*/hp-ux1[01])	OPC_RSHELL=/sbin/sh;;	# No compatibility links
    	hp/*/hpux1100)	        OPC_RSHELL=/usr/bin/sh;;	
	*)			OPC_RSHELL=/bin/sh
    esac

    #   Print trace record 
    #
    NICK_CMD=`echo "${URX_CMD}" | \
	      awk '{ if ( $1 == rsh ) { 
		       if ( $2 == "-x" ) print $3
		       else              print $2
		     }
		     else                print $1
		   }' \
                  rsh=${OPC_RSHELL} | \
	      sed -e "s='==g" -e "s=\"==g" -e "s=\\\`==g"`
    oi_trace opc 590 ${MODULE_NAME} "${NICK_CMD}" \
		     ${OPC_NODE} ${OPC_IP_ADDRESS} 

    #  Add couple of defines in front of the remote command.
    #  This enables oi_rmessage(1m) functions to correctly distinguish
    #  local and remote execution.
    #
    FURX_CMD="OPC_REMOTE=1; export OPC_REMOTE; ${URX_CMD}"

    #  Determinate which utility will be used to 
    #  execute remotely command:
    #     1) sh        : for local node
    #     2) remsh     : for remote node with host equivalence
    #     3) opc_rexec : for remote note - password required 
    #
    LOCAL_MODE=0
    if opcux_localhost "${OPC_NODE}"
    then
        #    Destination host is Management Server... Use 'sh'
	#
    oi_trace opc 650 ${OPC_IP_ADDRESS} sh ${KERBEROS_BYPASS}
	if [ "${OPC_RDEBUG}" = "YES" ]
	then
    	    REMSH_UTIL="sh -cx"
	    REMSH_OUTPUT=${REMSH_RDEBUG}
	else
    	    REMSH_UTIL="sh -c"
	    REMSH_OUTPUT="2>&1"
    	fi
	LOCAL_MODE=1

    elif [ "${OPC_PASSWD_REQ}" = 0 ]
    then
        #    No password required...  Use remsh(1) or ssh
	#

        if [ "$OPC_ACCESS_METHOD" = "$SSH_METHOD" ]
	then
          oi_trace opc 650 ${OPC_IP_ADDRESS} ssh ${KERBEROS_BYPASS}
	else
	  oi_trace opc 650 ${OPC_IP_ADDRESS} remsh ${KERBEROS_BYPASS}
	fi
    if [ ${KERBEROS_BYPASS} = "YES" ]
    then
        if [ "$OPC_ACCESS_METHOD" = "$SSH_METHOD" ]
        then
          # For ssh it is used OPC_NODE because 
          # what if the managed node is multi-homed?!
          REMSH_UTIL="ssh ${OPC_NODE} -l ${OPC_INST_USER} -P"
        else
          REMSH_UTIL="remsh ${OPC_IP_ADDRESS} -l ${OPC_INST_USER} -P"
        fi
    else
        if [ "$OPC_ACCESS_METHOD" = "$SSH_METHOD" ]
        then
          REMSH_UTIL="ssh ${OPC_NODE} -l ${OPC_INST_USER}"
        else
          REMSH_UTIL="remsh ${OPC_IP_ADDRESS} -l ${OPC_INST_USER}"
        fi
    fi
	if [ "${OPC_RDEBUG}" = "YES" ]
	then
            FURX_CMD="${OPC_RSHELL} -cx '$FURX_CMD'"
	    REMSH_OUTPUT=${REMSH_RDEBUG}
	else
            FURX_CMD="${OPC_RSHELL} -c '$FURX_CMD'"
	    REMSH_OUTPUT="2>&1"
    	fi

    else
        #    Password needed...  Enter opc_rexec
	#
        oi_trace opc 650 ${OPC_IP_ADDRESS} opc_rexec ${KERBEROS_BYPASS}
        OPC_RBTTXE=`$OPC_AGTINST_DIR/opccrpt -x qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm -d "$OPC_PASSWD"`
        REMSH_UTIL="${OPC_CMD_DIR}/opc_rexec\
        -l ${OPCSVR_INST_DIR}/inst.lock\
        -s ${OV_VAR_DIR}/sockets/rexec\
        -d ${OPCSVR_OPC_DIR}/defaults/C/INSTRUCTIONS/rexec\
        -c ${OPCSVR_OPC_DIR}/config/INSTRUCTIONS/rexec\
        ${OPC_IP_ADDRESS}\
        ${OPC_INST_USER} ${OPC_RBTTXE}"

	if [ "${OPC_RDEBUG}" = "YES" ]
	then
	    FURX_CMD="${OPC_RSHELL} -cx '{ $FURX_CMD ; } 2>&1'"
	    REMSH_OUTPUT="2>&1 | tee -a ${OPC_DEBUG_FILE}"
	    #   Also '----' trace line is printed, because
	    #   strerror goes to tracning file too
	    #
	    oi_trace opc 641
	    TRACE_MINUS="YES"
	else
	    FURX_CMD="${OPC_RSHELL} -c '$FURX_CMD'"
	    REMSH_OUTPUT="2>&1"
    	fi

    fi

    if [ "${OPC_RDEBUG}" = "YES" ]
    then
	#
	#  Print start string also in stderror
	#

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

        if [ "${OPC_DEBUG}" = "YES" ]
	then
	    eval echo "\"${OPC_DEBUG_LINE}\"" ${ECHO_RDEBUG}
	fi
	eval echo "\"*** START REMOTE ${MODULE_NAME} [${NICK_CMD}] on ${OPC_NODE} (${OPC_IP_ADDRESS})\"" ${ECHO_RDEBUG}
	eval echo "\"                 ${REMSH_UTIL} \"" '${FURX_CMD}' ${ECHO_RDEBUG}
	eval echo "\"${OPC_DEBUG_LINE}\"" ${ECHO_RDEBUG}

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    fi
       
    #
    #  Execute command when error echo string
    #
    if [ $LOCAL_MODE = 0 ]
    then

        #eval '( ${REMSH_UTIL} ''${FURX_CMD}''; echo RETRSH=$? >${TMP_DIR}/return )' ${REMSH_OUTPUT} |tee ${REMSH_TMPFILE}|opcrmf 


  #      ${REMSH_UTIL} @${URC_DEST}
         eval '( ${REMSH_UTIL} ''@${URC_DEST}''; echo RETRSH=$? >${TMP_DIR}/return )' ${REMSH_OUTPUT} |tee ${REMSH_TMPFILE}|opcrmf
  

    
        RETRMF=$?
        . ${TMP_DIR}/return
        if [ $RETRSH -eq 0 ] 
        then
           URX_EXIT_CODE=$RETRMF
        else
           eval echo "${OIRMSG_AARGH}E ${RETRSH}" ${REMSH_OUTPUT} |opcrmf
           URX_EXIT_CODE=$?
        fi
        unset RETRSH RETRMF
        rm -f ${TMP_DIR}/return
    else
	{
	    eval "${REMSH_UTIL} '${FURX_CMD}'" ${REMSH_OUTPUT} || \
	    eval echo "${OIRMSG_AARGH}E $?"  ${REMSH_OUTPUT} 
	} | opcrmf
        URX_EXIT_CODE=$?
    fi

    if [ "${OPC_RDEBUG}" = "YES" ]
    then

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

	if [ "${TRACE_MINUS}" = "YES" ]
	then
	    #   Print '----' record to mark end of remote stderror stuff
	    #
	    oi_trace opc 642
	fi
	
	#
	#  Print end string also in stderror
	#
	eval echo "\"${OPC_DEBUG_LINE}\"" ${ECHO_RDEBUG}
	eval echo "\"*** END   REMOTE ${MODULE_NAME} [${NICK_CMD}] on ${OPC_NODE} (${OPC_IP_ADDRESS})\"" ${ECHO_RDEBUG}
        if [ "${OPC_DEBUG}" = "YES" ]
	then
	    eval echo "\"${OPC_DEBUG_LINE}\"" ${ECHO_RDEBUG}
	fi

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    fi
    
    unset RETRSH RETRMF
    unset REMSH_UTIL REMSH_OUTPUT TRACE_MINUS
    unset URX_CMD FURX_CMD URX_RESULT OPC_RBTTXE NICK_CMD
    return $URX_EXIT_CODE
}
#==============================================================================
#
#  DESCRIPTION
#    Remote execution for installation scripts already on Managed Nodes.
#    Script execution utility is nnm.
#    
#    Remote script's output is filtered through ? (see below) to handle
#    escape strings, generated by oi_rmessage(1m) functions, which enable
#    remote script to output generic and platform-specific installation messages
#    append lines to a result file and specify exit values.
#    All other output is left unchanged.
#
#  ATTENTION
#    Text flow is undeterministic when you print messages on standard error
#    output in the remote scripts.
#
#  PARAMETERS
#    -r[esult] <file> .. Specifies file to be used as a result output
#    cmd [<args> ...] .. Command to be executed
#
#  EXIT CODE
#    Normaly exit code from the remote command;  local exit codes include:
#      1 .. Unrecognized option or not enough arguments
#      2 .. Problems with opctranm(1)
#      3 .. Default exit code, when remote script terminated without EXIT.
#

opcsarexec()
{
    while [ $# != 0 -a "${URX_CMD}" = "" ]
    do
	case "$1" in
	    -r*) URX_RESULT=$2;  shift; shift
	    	 ;;
	    *)   URX_CMD="$*"
		 ;;
	esac
    done
    if [ $# = 0 ]
    then
    	return 1
    fi

    #
    #  Determine pathname to the Bourne shell on the Managed node.
    #
    case "${OPC_PLATFORM}" in
    	hp/*/hp-ux1[01])	OPC_RSHELL=/sbin/sh;;	# No compatibility links
	*)			OPC_RSHELL=/bin/sh
    esac

    #   Print trace record 
    #
    NICK_CMD=`echo "${URX_CMD}" | \
	      awk '{ if ( $1 == rsh ) { 
		       if ( $2 == "-x" ) print $3
		       else              print $2
		     }
		     else                print $1
		   }' \
                  rsh=${OPC_RSHELL} | \
	      sed -e "s='==g" -e "s=\"==g" -e "s=\\\`==g"`
    oi_trace opc 590 ${MODULE_NAME} "${NICK_CMD}" \
		     ${OPC_NODE} ${OPC_IP_ADDRESS} 
    
    # firts do some tests
    if [ -s ${JOBFILE} ]
    then
	JOB_NODE=`awk '{
		if ( (NF > 0) && ($1 != "#" )) {
		   printf "%s", $1; 
		   exit 0 
		} }' $JOBFILE`
	if [ "${JOB_NODE}" != "${OPC_IP_ADDRESS}" ]
	then
	    rm -f ${JOBFILE}
	    echo "${OPC_IP_ADDRESS}" >> ${JOBFILE}
	fi
    else
	echo "${OPC_IP_ADDRESS}" >> ${JOBFILE}
    fi
    echo "!${URX_CMD}" >>${JOBFILE}

    REMSH_UTIL=opctranm

    oi_trace opc 650 ${OPC_IP_ADDRESS} ${REMSH_UTIL} ${KERBEROS_BYPASS}
    if [ "${OPC_RDEBUG}" = "YES" ]
    then
	REMSH_OUTPUT="2>&1 | tee -a ${OPC_DEBUG_FILE}"
	#   Also '----' trace line is printed, because
	#   strerror goes to tracning file too
	#
	oi_trace opc 641
	TRACE_MINUS="YES"
    else
	REMSH_OUTPUT=""
    fi

    if [ "${OPC_RDEBUG}" = "YES" ]
    then
	#
	#  Print start string also in stderror
	#

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

        if [ "${OPC_DEBUG}" = "YES" ]
	then
	    eval echo "\"${OPC_DEBUG_LINE}\"" ${ECHO_RDEBUG}
	fi
	eval echo "\"*** START REMOTE ${MODULE_NAME} [${NICK_CMD}] on ${OPC_NODE} (${OPC_IP_ADDRESS})\"" ${ECHO_RDEBUG}
	eval echo "\"                 ${REMSH_UTIL} \"" '${URX_CMD}' ${ECHO_RDEBUG}
	eval echo "\"${OPC_DEBUG_LINE}\"" ${ECHO_RDEBUG}

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    fi
       
    OPC_TRANM_CODE=0
    {
	${REMSH_UTIL} -jobfile ${JOBFILE} || \
	(OPC_TRANM_CODE=$?;
	 if [ $OPC_TRANM_CODE -lt 0 ]
	 then
	    echo "${OIRMSG_AARGH}MEG 651 ${OPC_TRANM_CODE} ${OPC_NODE} ${REMSH_UTIL}" 
	 elif [ $OPC_TRANM_CODE -gt 0 ]
	 then
	    echo "${OIRMSG_AARGH}MEG 652 ${OPC_TRANM_CODE} ${OPC_NODE} ${REMSH_UTIL}" 
	 fi;
	 echo "${OIRMSG_AARGH}E 2" )
    } > $TMP_OUT_FILE
    if [ "${OPC_RDEBUG}" = "YES" ]
    then
	cat $TMP_OUT_FILE >> ${OPC_DEBUG_FILE}
    fi
    cat $TMP_OUT_FILE | opcrmf
    URX_EXIT_CODE=$?
    rm -f $JOBFILE $TMP_OUT_FILE
	
    if [ "${OPC_RDEBUG}" = "YES" ]
    then

#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

	if [ "${TRACE_MINUS}" = "YES" ]
	then
	    #   Print '----' record to mark end of remote stderror stuff
	    #
	    oi_trace opc 642
	fi
	
	#
	#  Print end string also in stderror
	#
	eval echo "\"${OPC_DEBUG_LINE}\"" ${ECHO_RDEBUG}
	eval echo "\"*** END   REMOTE ${MODULE_NAME} [${NICK_CMD}] on ${OPC_NODE} (${OPC_IP_ADDRESS})\"" ${ECHO_RDEBUG}
        if [ "${OPC_DEBUG}" = "YES" ]
	then
	    eval echo "\"${OPC_DEBUG_LINE}\"" ${ECHO_RDEBUG}
	fi

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    fi

    if [ $OPC_TRANM_CODE -lt 0 ]
    then
	oi_error "opc" 651 ${OPC_TRANM_CODE} ${OPC_NODE} ${REMSH_UTIL}
    elif [ $OPC_TRANM_CODE -gt 0 ]
    then
	oi_error "opc" 652 ${OPC_TRANM_CODE} ${OPC_NODE} ${REMSH_UTIL}
    fi
    unset REMSH_UTIL REMSH_OUTPUT TRACE_MINUS RV RV_1
    unset URX_CMD FURX_CMD URX_RESULT OPC_TRANM_CODE NICK_CMD
    return $URX_EXIT_CODE
}
	
#==============================================================================
#
#   Catch and interpret remote script's escape strings.
#   Used only by opcuxrexec.

opcrmf()
{


#   BEGIN of function: disable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
    	set +x
	DEBUG_CNT=`expr $DEBUG_CNT + 1`
    fi

    URX_EXIT_CODE=$OIRMSG_NOEXIT           # default exit code

    if [ "$URX_RESULT" != "" ]
    then
    	rm -f $URX_RESULT
    	touch $URX_RESULT
    else
    	URX_RESULT=/dev/null
    fi

    while read URX_LINE
    do
        case "$URX_LINE" in
        ${OIRMSG_AARGH}MEG*)
            oi_errmsg opc `expr "$URX_LINE" : "${OIRMSG_AARGH}MEG \(.*\)"`
            ;;
        ${OIRMSG_AARGH}MEP*)
            oi_errmsg "$OPC_OS_NICKNAME" \
                `expr "$URX_LINE" : "${OIRMSG_AARGH}MEP \(.*\)"`
	    ;;
        ${OIRMSG_AARGH}MWG*)
            oi_warning opc `expr "$URX_LINE" : "${OIRMSG_AARGH}MWG \(.*\)"`
            ;;
        ${OIRMSG_AARGH}MWP*)
            oi_warning "$OPC_OS_NICKNAME" \
                `expr "$URX_LINE" : "${OIRMSG_AARGH}MWP \(.*\)"`
	    ;;
        ${OIRMSG_AARGH}MNG*)
            oi_note opc `expr "$URX_LINE" : "${OIRMSG_AARGH}MNG \(.*\)"`
            ;;
        ${OIRMSG_AARGH}MNP*)
            oi_note "$OPC_OS_NICKNAME" \
                `expr "$URX_LINE" : "${OIRMSG_AARGH}MNP \(.*\)"`
            ;;
        ${OIRMSG_AARGH}MVG*)
            oi_verbose opc `expr "$URX_LINE" : "${OIRMSG_AARGH}MVG \(.*\)"`
            ;;
        ${OIRMSG_AARGH}MVP*)
            oi_verbose "$OPC_OS_NICKNAME" \
                `expr "$URX_LINE" : "${OIRMSG_AARGH}MVP \(.*\)"`
            ;;
        ${OIRMSG_AARGH}R*)
            TMP_RES=`expr "$URX_LINE" : "${OIRMSG_AARGH}R \(.*\)"`
            if [ $? -eq 1 ]
            then
                # return string is null or zero
                # add extra 0 at the end of string to determinate zero
                expr "${URX_LINE}0" : "${OIRMSG_AARGH}R \(.*\)" > /dev/null
                if [ $? -eq 1 ]
                then
                    # return string is null
                    TMP_RES=""
                fi
            fi
            echo "$TMP_RES" >> $URX_RESULT
	    oi_trace opc 592 ${MODULE_NAME} "${NICK_CMD}" \
	                     ${OPC_NODE} ${OPC_IP_ADDRESS} "\"$TMP_RES\""
            ;;
        ${OIRMSG_AARGH}E*)
            URX_EXIT_CODE=`expr "$URX_LINE" : "${OIRMSG_AARGH}E \(.*\)"`
	    oi_trace opc 591 ${MODULE_NAME} "${NICK_CMD}" \
	                     ${OPC_NODE} ${OPC_IP_ADDRESS} "${URX_EXIT_CODE}"
            ;;
        ${OIRMSG_AARGH}MMG*)
            ## handle RMSG messages
            oi_msg opc `expr "$URX_LINE" : "${OIRMSG_AARGH}MMG \(.*\)"`
            ;;
        *)  if [ $OPC_RCAPTURE = 1 ]
	    then
	    	LANG=${OPC_CALL_LANG} echo "$URX_LINE"
	    fi
	    LANG=${OPC_CALL_LANG} echo "$URX_LINE" >> ${OPC_LOGF}
        esac
    done
    unset URX_LINE URX_RESULT URX_STR

#   END   of function: enable debugging if OPC_DEBUG_ALL not set
    if [ "${OPC_ADEBUG}" = "OPCAYES" ]
    then
	if [ ${DEBUG_CNT} -gt 0 ]
	then
	    DEBUG_CNT=`expr $DEBUG_CNT - 1`
	fi
	if [ ${DEBUG_CNT} -le 1 ]
	then
	    set -x
	fi
    fi

    return $URX_EXIT_CODE
}

#==============================================================================
#
#  DESCRIPTION
#    Remote copy generic entry point.  Specified source file is copied to
#    destination file on the specified node, if possible.
#    Standard UNIX copy utility cp(1) is used if destination node equals
#    local host (i.e. Management Server acts as Managed Node);
#    rcp(1) is used when no password is required (OPC_PASSWD_REQ == 0) and
#    ftp(1) is tried when password is required for root on remote node.
#
#  PARAMETERS
#    src    .. Source file on the Management Server.
#    dest   .. Destination path/file on Managed Node.
#              Note that destination path must exist before calling opcuxrcopy.
#
#  EXIT CODE
#      0 .. File successfully copied.
#      1 .. Problems with copy utility.
#      2 .. Problems with root ftp(1) access on remote node.
#

opcuxrcopy()
{
    URC_SRC=$1;	    shift;		# Source file to be copied
    URC_DEST=$1;    shift;		# Destination path/file name





    if [ "${OPC_PASSWD_REQ}" = 2 ]
    then
	# Use opcsarcopy() for this purpose.
	opcsarcopy $URC_SRC $URC_DEST
    	return $?
    fi

    #DEBUG
    #
    #  Additional debugging output - print source and destination
    #
    oi_trace opc 593 ${MODULE_NAME} `basename ${URC_SRC}` \
		     ${OPC_NODE} ${OPC_IP_ADDRESS} $URC_SRC $URC_DEST

    if opcux_localhost "${OPC_NODE}"
    then
        oi_trace opc 650 ${OPC_IP_ADDRESS} cp ${KERBEROS_BYPASS}
        cp "${URC_SRC}" "${URC_DEST}"
        if [ $? != 0 ]
        then
            #    Problems transferring package via cp...
            #
            oi_error opc 107 "${OPC_NODE}" `basename "${URC_SRC}"` cp
            unset URC_SRC URC_DEST
            return 1
        fi
    elif [ "${OPC_PASSWD_REQ}" = 0 -a "$OPC_ACCESS_METHOD" = "$SSH_METHOD" ] 
    then
        oi_trace opc 650 ${OPC_IP_ADDRESS} scp ${KERBEROS_BYPASS}
        scp -q "${URC_SRC}" "${OPC_NODE}:${URC_DEST}"
        if [ $? != 0 ]
        then
            #    Problems transferring package via scp...
            #
            oi_error opc 107 "${OPC_NODE}" `basename "${URC_SRC}"` scp
            unset URC_SRC URC_DEST
            return 1
        fi
    elif [ "${OPC_PASSWD_REQ}" = 0 ]
    then
        oi_trace opc 650 ${OPC_IP_ADDRESS} rcp ${KERBEROS_BYPASS}
        if [ "${KERBEROS_BYPASS}" = "YES" ]
        then
          if [ ${OPC_IP_ADDRESS#*:} != $OPC_IP_ADDRESS ]
	  then
                
	  	rcp -P "${URC_SRC}" "${OPC_INST_USER}@[${OPC_IP_ADDRESS}]:${URC_DEST}"
	  else
	  	rcp -P "${URC_SRC}" "${OPC_INST_USER}@${OPC_IP_ADDRESS}:${URC_DEST}"
          fi
	else
          if [ ${OPC_IP_ADDRESS#*:} != $OPC_IP_ADDRESS ]
	  then
	  	rcp "${URC_SRC}" "${OPC_INST_USER}@[${OPC_IP_ADDRESS}]:${URC_DEST}"
	  else
              
	  	rcp "${URC_SRC}" "${OPC_INST_USER}@${OPC_IP_ADDRESS}:${URC_DEST}"
          fi
	fi
        if [ $? != 0 ]
        then
            #    Problems transferring package via rcp...
            #
            oi_error opc 107 "${OPC_NODE}" `basename "${URC_SRC}"` rcp
            unset URC_SRC URC_DEST
            return 1
        fi
    else
        if opcux_ftp_setup
	then
            #	Make FTP transfer...
	    #
	    oi_trace opc 650 ${OPC_IP_ADDRESS} ftp ${KERBEROS_BYPASS}
	    OPC_RBTTXE=`$OPC_AGTINST_DIR/opccrpt -x qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm -d "$OPC_PASSWD"`
            # quote it in a second step because sed needs a newline at the end
            OPC_RBTTXE=`echo ${OPC_RBTTXE} | sed 's/ /\\\\ /g' | sed 's/"/\\\\"/g'`
    
	    if [ "${KERBEROS_BYPASS}" = "YES" ]
	    then
		KERB_PAR="-Pn"
	    else
		KERB_PAR="-n"
	    fi
            ftp $KERB_PAR <<-EOF >>${OPC_TOPC}/ftp.stdout 2>${OPC_TOPC}/ftp.out
open ${OPC_IP_ADDRESS}
user ${OPC_INST_USER} ${OPC_RBTTXE}
binary
put ${URC_SRC} ${URC_DEST}
bye
EOF
            # check for errors on stdout also - in case ftp has problems because
            # the root shell is not in /etc/shells, we get no stderr output
            if [ `grep -c '^Login failed.$' ${OPC_TOPC}/ftp.stdout` -ne 0 ]
            then
                cat ${OPC_TOPC}/ftp.stdout >> ${OPC_TOPC}/ftp.out
            fi
            rm -f ${OPC_TOPC}/ftp.stdout

	    FTP_OUT=`cat ${OPC_TOPC}/ftp.out`
	    if [ -n "${FTP_OUT}" -a "${KERBEROS_BYPASS}" = "NO" ]
	    then
            	#   Problems transferring package via ftp...
            	#
		oi_show_file ${OPC_TOPC}/ftp.out
            	oi_error opc 107 "${OPC_NODE}" `basename "${URC_SRC}"` ftp
		unset URC_SRC URC_DEST
		return 1
	    fi
	    FTP_NODE=${OPC_IP_ADDRESS}
	    echo "${FTP_NODE}" >${OPC_TOPC}/ftp.node
	else
	    #	Initial FTP checks failed...
	    #
	    return 2
	fi
    fi
    return 0
}

#==============================================================================
#
#  DESCRIPTION
#    
#    Remote copy generic entry point.  Specified source file is copied to
#    destination file on the specified node, if possible.
#
#  PARAMETERS
#    src    .. Full path name of source file on the Management Server.
#    dest   .. Destination path/file on Managed Node.
#              Note that destination path must exist before calling opcsarcopy.
#
#  EXIT CODE
#      0 .. File successfully updated.
#      1 .. Problems with file update.
#

opcsarcopy()
{
    URC_SRC=$1;	    shift;		# Source file to be copied
    URC_DEST=$1;    shift;		# Destination path/file name

    #DEBUG
    #
    #  Additional debugging output - print source and destination
    #
    oi_trace opc 593 ${MODULE_NAME} `basename ${URC_SRC}` \
		     ${OPC_NODE} ${OPC_IP_ADDRESS} $URC_SRC $URC_DEST

    # firts do some tests
    if [ -s ${JOBFILE} ]
    then
	JOB_NODE=`awk '{
		if ( (NF > 0) && ($1 != "#" )) {
		   printf "%s", $1; 
		   exit 0 
		} }' $JOBFILE`
	if [ "${JOB_NODE}" != "${OPC_IP_ADDRESS}" ]
	then
	    rm -f ${JOBFILE}
	    echo "${OPC_IP_ADDRESS}" >> ${JOBFILE}
	fi
    else
	echo "${OPC_IP_ADDRESS}" >> ${JOBFILE}
    fi
    echo "  $URC_SRC   $URC_DEST" >>${JOBFILE}
    unset JOB_NODE
    return 0
}

#-------------------------------------------------------------------------------
#
#   Setup FTP transfer...
#

opcux_ftp_setup()
{
    if [ -z "${FTP_NODE}" ]
    then
	FTP_NODE=UNKNOWN
	if [ -s ${OPC_TOPC}/ftp.node ]
	then
	    read FTP_NODE < ${OPC_TOPC}/ftp.node
    	fi
    fi
    if [ "${FTP_NODE}" != "${OPC_IP_ADDRESS}" ]
    then
	#   Destination host changed...  Make initial checks.
	#
	#   1. Root password should not be empty string, otherwise
	#      ftp's autologin procedure will fail.
	#
        #   2. Check if user OPC_INST_USER is mentioned in /etc/ftpusers.
    	#      If true, ftp transfer is not possible.
	#
        #   3. Check user OPC_INST_USER login shell : if it is non-standard or
	#      if it is not listed in /etc/shell file
        #
	if [ -z "${OPC_PASSWD}" ]
	then
	    oi_error opc 512 "${OPC_NODE}"
	    return 1
	fi

	opcuxrexec "if [ -f /etc/ftpusers ]; \
		    then \
			NN=\`grep -c \"^${OPC_INST_USER}$\" /etc/ftpusers\`; \
			echo \"${OIRMSG_AARGH}E \$NN\"; \
		    else \
			echo \"${OIRMSG_AARGH}E 0\"; \
		    fi"
        if [ $? != 0 ]
        then
	    oi_error opc 111 "${OPC_NODE}"
	    return 1
        fi

	#  NSMbb71382: check /etc/ftpd/ftpusers as well.
	#
	opcuxrexec "if [ -f /etc/ftpd/ftpusers ]; \
		    then \
			NN=\`grep -c \"^${OPC_INST_USER}$\" /etc/ftpd/ftpusers\`; \
			echo \"${OIRMSG_AARGH}E \$NN\"; \
		    else \
			echo \"${OIRMSG_AARGH}E 0\"; \
		    fi"
        if [ $? != 0 ]
        then
	    oi_error opc 840 "${OPC_NODE}"
	    return 1
        fi

	#  Fix for DTS NSMbb13022
	#
        #   3. First get OPC_INST_USER login shell ;
	#      Then check /etc/shell file; exit values of remote command are
    	#      1 - no /etc/shell file 
    	#      2 - /etc/shells exists, but no OPC_INST_USER login shell entry
    	#      3 - /etc/shells exists and includes OPC_INST_USER login shell 
    	#
	#      What will work:
	#      1 - should work if login shell is one of default platform shells
	#          (we will check this only for SunOS !ksh -) historic reasons)
	#      2 - should not work on almost all plaforms (AIX and IRIX are
	#          exceptions)
	#      3 - will work on all platforms
	#

	TMP_FILE=${OPC_TOPC}/opc-$$.tmp
	if [ -f ${TMP_FILE} ]
	then 
	    #	remove temporary file
	    rm -f ${TMP_FILE}
	fi

	FTP_CMD="echo ${OIRMSG_AARGH}R \`grep \"^${OPC_INST_USER}\" /etc/passwd\`"
    	opcuxrexec -r ${TMP_FILE} "${FTP_CMD}"
	if [ -f ${TMP_FILE} ]
	then
	    if [ `grep -c "^${OPC_INST_USER}.*:$" ${TMP_FILE}` -ne 0 ]
	    then
		LOGIN_SHELL="/" #default shell -ftp must work
	    else
		LOGIN_SHELL=`cat ${TMP_FILE} | ${AWK} -F ":" '{print $NF}'`
	    fi
	    rm -f ${TMP_FILE}
	else
            oi_error   opc 206 "${OPC_NODE}";  return 1 
	fi
	unset TMP_FILE

    	FTP_CMD=" \
	    if [ ! -f /etc/shells ]; \
	    then \
		echo \"${OIRMSG_AARGH}E 1\"; \
	    elif [ \`grep -c \"^${LOGIN_SHELL}\" /etc/shells\` = 0 ]; \
	    then \
		echo \"${OIRMSG_AARGH}E 2\"; \
            else \
		echo \"${OIRMSG_AARGH}E 3\"; \
	    fi"

    	opcuxrexec "${FTP_CMD}"
    	case $? in
            1)  if [ "${OPC_PLATFORM}" = "sun/sparc/sunos" ]
	        then
		    if [ `echo "${LOGIN_SHELL}" | grep -c "ksh"` -ne 0 ]
		    then
			oi_error opc 203 "${OPC_NODE}"
			return 1
	            fi
		else
		    if [ "$LOGIN_SHELL" = "/" ]
		    then
			LOGIN_SHELL="default"
		    fi
	            oi_warning opc 205 "${OPC_NODE}" "${LOGIN_SHELL}"
		fi
	        ;;
            2)  if [ "${OPC_PLATFORM}" != "ibm/rs6000/aix" -a \
                     "${OPC_PLATFORM}" != "sgi/mips/irix" ]
	        then
		    if [ "$LOGIN_SHELL" = "/" ]
		    then
			LOGIN_SHELL="default"
		    fi
	            oi_error   opc 204 "${OPC_NODE}" "${LOGIN_SHELL}"
		    return 1
	        fi
	        ;;
            3)  ;;
            *)  oi_error   opc 206 "${OPC_NODE}";  return 1 ;;
        esac
    fi

    #  Fix for DTS NSMbb08867
    #  No more .netrc file is used to set  user and password
    #
    return 0
}

#==============================================================================
#
#  DESCRIPTION
#    Remote copy-and-execute generic entry point.
#
#  PARAMETERS
#    -r[esult] <file> .. Specify result output file (see opcrexec).
#    -d[ir] <path>    .. Directory path on Managed node, where script
#                        will be copied; CL_TMP_DIR is used by default.
#    -c[leanup]       .. Remove script after execution.
#
#    script [args]    .. Local path name of the script to be executed.
#                        Convention `#!<invocation command>' is honored.
#                        When there's no invocation command /[s]bin/sh is used.
#
#  EXIT CODE
#    Normally result of the remote execution.  Local codes include:
#      1 .. Unrecognized option, missing arguments...
#      2 .. Couldn't copy script
#

opcuxrcpsh()
{


    UCX_DIR=${CL_TMP_DIR}            # Default destination directory on MN

    while [ $# != 0 -a -z "${UCX_SCRIPT}" ]
    do
        case "$1" in
            -r*) UCX_RESULT="$1 $2"; shift    # Script result output
                 ;;
            -d*) UCX_DIR=$2; shift            # Destination directory
                 ;;
            -c*) UCX_CLEANUP=1                # Remove script after use
                 ;;
            *)   UCX_SCRIPT=$1                # Pathname to the local script
        esac
        shift
    done
    UCX_ARGS="$*"

    if [ -z "${UCX_SCRIPT}" ]
    then
	#    Should I print Usage string?  Guess no.
	#
	unset UCX_SCRIPT UCX_ARGS UCX_RESULT UCX_DIR UCX_CLEANUP
	return 1
    fi
    
    if [ "${OPC_PASSWD_REQ}" = 2 ]
    then
	# NO need to copy remote executed script on node
	# It must be present there
	UCX_DEST_SCRIPT="`basename ${UCX_SCRIPT}`"
       
       
	opcsarexec ${UCX_RESULT} ${UCX_DEST_SCRIPT} "${UCX_ARGS}"
	UCX_EXIT_CODE=$?
    else

	UCX_DEST_SCRIPT="[.${UCX_DIR}]`basename ${UCX_SCRIPT}`"

	#
	#  Determine pathname to the Bourne shell on the Managed node.
	#
	case "${OPC_PLATFORM}" in
	    hp/*/hp-ux1[01])	OPC_RSHELL=/sbin/sh;;	# No compatibility links
	    *)			OPC_RSHELL=/bin/sh
	esac


	if opcuxrcopy ${UCX_SCRIPT} ${UCX_DEST_SCRIPT}
	then
	    #  Get the invocation command.
	    #  Use OPC_RSHELL if it isn't specified.  CHECK ALL THE FILES FIRST!
	    #
	    XCMD=`sed -n '1s/^#!\(.*\)$/\1/p' ${UCX_SCRIPT}`
	    if [ -z "${XCMD}" ]
	    then
		XCMD=${OPC_RSHELL}
	    fi
	    #DEBUG
	    if [ "${OPC_RDEBUG}" = "YES" ]
	    then
		opcuxrexec ${UCX_RESULT} ${XCMD} -x ${UCX_DEST_SCRIPT} "${UCX_ARGS}"
	    else
		opcuxrexec ${UCX_RESULT} ${XCMD} ${UCX_DEST_SCRIPT} "${UCX_ARGS}"
	    fi
	    UCX_EXIT_CODE=$?
	    if [ "${UCX_CLEANUP}" = 1 ]
	    then
		opcuxrexec rm -f ${UCX_DEST_SCRIPT}
	    fi
	else
	    #    Couldn't copy.  Diagnostic already given by opcuxrcopy.
	    #
	    UCX_EXIT_CODE=2
	fi
    fi

    unset UCX_SCRIPT UCX_ARGS UCX_DEST_SCRIPT XCMD
    unset UCX_RESULT UCX_DIR UCX_CLEANUP

    return $UCX_EXIT_CODE
}

#==============================================================================
#
#  DESCRIPTION
#    Remote copy-and-execute generic entry point.
#
#  PARAMETERS
#    -r[esult] <file> .. Specify result output file (see opcsarexec).
#    -d[ir] <path>    .. Directory path on Managed node, where script
#                        will be copied; CL_TMP_DIR is used by default.
#    -c[leanup]       .. Remove script after execution.
#
#    script [args]    .. Local path name of the script to be executed.
#                        Convention `#!<invocation command>' is honored.
#                        When there's no invocation command /[s]bin/sh is used.
#
#  EXIT CODE
#    Normally result of the remote execution.  Local codes include:
#      1 .. Unrecognized option, missing arguments...
#      2 .. Couldn't copy script
#

opcsarcpsh()
{
    return 2
}

#==============================================================================

#==============================================================================
#
#  DESCRIPTION
#    Determinate appropriate remote execution utility (remsh or opc_rexec)
#    and acquire correct superuser password if opc_rexec is right one,
#
#  PARAMETERS
#
#  ENVIRONMENT
#    OPC_PASSWD_REQ .. Password required flag: when 1 password is required, 0 
#                  otherwise
#    OPC_PASSWD     .. Password string (encrypted).
#
#  EXIT CODE
#      0 .. task successfully done
#      1 .. problems with opc_rexec utility; user give up.
#

opcuxpwget()
{
    
    # Check for Kerberos
    # (For ssh we use OPC_NODE because 
    #  what if the managed node is multi-homed?!)

    REMSH_TMPFILE=${TMP_DIR}/remsh.out
    if [ -f $REMSH_TMPFILE ]
    then 
      rm -f $REMSH_TMPFILE
    fi

    if [ "$OPC_ACCESS_METHOD" = "$SSH_METHOD" ]
    then
      ssh ${OPC_NODE} -l ${OPC_INST_USER} date >/dev/null 2>${REMSH_TMPFILE}
      if [ $? -ne 0 ]; then
        oi_warning "opc" 841 ${OPC_NODE}
        oi_prompt_and_ask "opc" 842 "" "" 843
        case "${OPC_ANSWER}" in
          [${OPC_YES_LIT}]*)
              ;;
          *)
              return 1
              ;;
        esac
      fi
    else
      LC_ALL=C remsh ${OPC_IP_ADDRESS} -l ${OPC_INST_USER} date >/dev/null 2>${REMSH_TMPFILE}
      # Check if rshd is running and is accessible on remote node
      REMSH_OUT="`grep -iE 'connection refused|timed out' ${REMSH_TMPFILE}`"
      rm -f ${REMSH_TMPFILE}
      if [ -n "${REMSH_OUT}" ]
      then
          oi_error "opc" 854 ${OPC_NODE}
          return 2
      fi

      remsh ${OPC_IP_ADDRESS} -l ${OPC_INST_USER} date >/dev/null 2>${REMSH_TMPFILE}
    fi

    # We need to bypass Kerberos (means to switch to the standard behavior)
    # when the string "Kerberos" occurs in stderr
    REMSH_OUT=`grep -i -E "kerberos|kcmd" ${REMSH_TMPFILE}`
    rm -f ${REMSH_TMPFILE}
    if [ -n "${REMSH_OUT}" ]
    then
       KERBEROS_BYPASS="YES"
       oi_trace opc 648 ${OPC_IP_ADDRESS}
    else
       KERBEROS_BYPASS="NO"
    fi
    export KERBEROS_BYPASS

					# Simple command for remote execution
    REM_CMD="date ${OPC_R2DEVNULL} 1>&2 ; echo ${OIRMSG_AARGH}E \$?"
    
    #   try with remsh(1) first by using REXEC_SCRIPT callout script,
    #	set environment variables before script is called
    #
    OPC_PASSWD_REQ=0; export OPC_PASSWD_REQ
    eval ${OPC_REXEC_SCRIPT} '${REM_CMD}' ${OPC_2DEVNULL} 1>&2
    if [ $? -eq 0 ]
    then
	#   REXEC_SCRIPT works
	#
	unset REM_CMD
    	OPC_PASSWD=""; export OPC_PASSWD
	return 0
    fi
    rm -f ${REMSH_TMPFILE}

    #	opc_rexec(1m) is appropriate one,
    ##  if OPC_PASSWD is already set, try this one first
    #
    OPC_PASSWD_REQ=1; export OPC_PASSWD_REQ
    
	eval ${OPC_REXEC_SCRIPT} '${REM_CMD}' ${OPC_2DEVNULL} 1>&2
	if [ $? -eq 0 ]
	then
	    #   REXEC_SCRIPT works
	    #
	    unset REM_CMD
	    return 0
	fi
	#oi_warning "opc" 164 "${OPC_NODE}"
        # Check if rexecd is running and is accessible on remote node
	REMSH_OUT="`grep -iE 'connection refused|timed out' ${REMSH_TMPFILE}`"
        rm -f ${REMSH_TMPFILE}
	if [ -n "${REMSH_OUT}" ]
        then
	    oi_error "opc" 855 ${OPC_NODE}
	    return 2
	fi

    #	acquire password and set environment variables before REXEC_SCRIPT 
    #	is called
    #	do this in endless loop until REXEC_SCRIPT works or user gives up
    #
    ASK_MSG_NUM="031 ${OPC_NODE}"

    while true
    do
	stty -echo
	oi_prompt_and_ask "opc" 139 root "${OPC_NODE}" ${ASK_MSG_NUM}
	stty echo
	echo
	if [ "${OPC_ANSWER}" = "${OPC_OIANSWER_DEF}" ]
	then
	    #   user did not answer
	    #
	    OPC_PASSWD=""; export OPC_PASSWD
    	    RETURN_VAL=1
	    break
	  
	fi
        OPC_PASSWD=`$OPC_AGTINST_DIR/opccrpt -x qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm -c "$OPC_ANSWER"` ; export OPC_PASSWD
	
	eval ${OPC_REXEC_SCRIPT} '${REM_CMD}' ${OPC_2DEVNULL} 1>&2
	if [ $? -eq 0 ]
	then
	    #   REXEC_SCRIPT works
	    #
    	    RETURN_VAL=0
	    break
	fi

        # Check if rexecd is running and is accessible on remote node
        REMSH_OUT="`grep -iE 'connection refused|timed out' ${REMSH_TMPFILE}`"
        rm -f ${REMSH_TMPFILE}
        if [ -n "${REMSH_OUT}" ]
        then
            oi_error "opc" 855 ${OPC_NODE}
            return 2
        fi
     	
	#   password is wrong
	#   ask, if root password should be re-entered once more
	#
	oi_verbose opc 034 ${OPC_NODE}
	oi_prompt_and_ask "opc" 191 "${OPC_PLATFORM}" "${OPC_NODE}" \
				313 ${OPC_NODE}
	case "${OPC_ANSWER}" in
	    [${OPC_YES_LIT}]*)
	    #	try again with new password
	    #
		ASK_MSG_NUM="033"
	    ;;
	    *)
    	    #	user give up
	    #
		OPC_PASSWD=""; export OPC_PASSWD
		RETURN_VAL=1
		break
	    ;;
	esac

    done

    unset ASK_MSG_NUM REM_CMD
    if [ ${RETURN_VAL} -eq 0 ]
    then 
	unset RETURN_VAL
	return 0
    else
	unset RETURN_VAL
	return 1
    fi

}

#==============================================================================
#
#  DESCRIPTION
#    Save/Restore OPC_NODE and its corresponding environment variables to
#    OPC_NODE's cluster server ones. 
#    Use this function to simplify environment switching between OPC_NODE and
#    OPC_NODE's cluster server.
#
#  PARAMETERS
#    mode .. mode of operation: SAVE to save ,RESTORE to restore OPC_NODE's 
#            variables.
#
#  ENVIRONMENT
#    OPC_NODE, OPC_IP_ADDRESS, OPC_PASSWD_REQ, OPC_PASSWD 
#             .. OPC_NODE variables 
#    OPC_CL_NODE, OPC_CL_IP_ADDRESS, OPC_CL_PASSWD_REQ, OPC_CL_PASSWD 
#             .. OPC_NODE's cluster server variables 
#    OPC_CL_M .. variable to prevent successive RESTORE operations
#
#  EXIT CODE
#      0 .. task successfully done
#      1 .. usage error
#

opcuxenvsave()
{
    if [ $# -ne 1 ]
    then
	#   usage error
	#
	return 1
    fi

    MODE=$1

    case ${MODE} in
    	SAVE)
	    #	save OPC_NODE variables 
	    #   switch values of OPC_NODE and OPC_NODE's cluster server
	    #   save error counters
	    OPC_CL_M=1
	;;
	RESTORE)
	    #	restore OPC_NODE variables
	    #   switch values of OPC_NODE and OPC_NODE's cluster server
	    #   write current error counters and reestablish old ones
	    if [ "${OPC_CL_M}" = "1" ]
	    then
		#   restore operation is possible
		#
		OPC_CL_M=0
	    else
		#   restore operation is not possible
		#
		unset MODE
		return 1
	    fi
	;;
	*)
	    #   usage error
	    #
	    unset MODE
	    return 1
	;;
    esac
    
    TSV_NODE=${OPC_NODE}
    TSV_IP_ADDRESS=${OPC_IP_ADDRESS}
    TSV_PASSWD_REQ=${OPC_PASSWD_REQ}
    TSV_PASSWD="${OPC_PASSWD}"
    #TSV_ACT_ONLY=${ACTIVATE_ONLY}

    OPC_NODE=${OPC_CL_NODE}
    OPC_IP_ADDRESS=${OPC_CL_IP_ADDRESS}
    OPC_PASSWD_REQ=${OPC_CL_PASSWD_REQ}
    OPC_PASSWD="${OPC_CL_PASSWD}"
    #ACTIVATE_ONLY=${OPC_ACT_ONLY}

    OPC_CL_NODE=${TSV_NODE}
    OPC_CL_IP_ADDRESS=${TSV_IP_ADDRESS}
    OPC_CL_PASSWD_REQ=${TSV_PASSWD_REQ}
    OPC_CL_PASSWD="${TSV_PASSWD}"
    #CL_ACT_ONLY=${TSV_ACT_ONLY}
    
    export OPC_NODE OPC_IP_ADDRESS OPC_PASSWD_REQ OPC_PASSWD 
    export OPC_CS_NODE OPC_CL_IP_ADDRESS OPC_CS_PASSWD_REQ OPC_CS_PASSWD
    unset MODE TSV_NODE TSV_IP_ADDRESS TSV_PASSWD_REQ TSV_PASSWD  TSV_ACT_ONLY
    #export ACTIVATE_ONLY CL_ACT_ONLY
    
    return 0
    
}

#===============================================================================
#
#  Produce password_file(5) line for actual $OPC_NODE.
#  Used by opcping and opcmpeping scripts.
#
produce_password()
{
    if [ "${OPC_STATUS}" = "NOTREADY" -o "${OPC_STATUS}" = "INSTALLED" ]
    then
	#   skip this OPC_NODE
	#
	# skipping warning issued in opc_inst.sh
	echo "${OPC_NODE} ${OPC_STATUS}" > $OPC_OUTPUT_FILE
    else
	#   produce real password_file(5) line for this node
	#

	if [ -z "${OPC_PASSWD}" ]
	then 
	    #	zero length password reset to default value
	    #
	    OPC_PASSWD=OPC_UNKNOWN_PASSWORD
	fi

	PASSWORD_LINE="${OPC_NODE}"
	PASSWORD_LINE="${PASSWORD_LINE} ${OPC_STATUS}"
	PASSWORD_LINE="${PASSWORD_LINE} ${OPC_PASSWD_REQ}"
	PASSWORD_LINE="${PASSWORD_LINE} ${OPC_PASSWD}"
	PASSWORD_LINE="${PASSWORD_LINE} ${KERBEROS_BYPASS}"
	PASSWORD_LINE="${PASSWORD_LINE} ${ACTIVATE_ONLY}"
        if [ ! -z "${WNT_ACCOUNT_PASSWD}" ]
        then
          PASSWORD_LINE="${PASSWORD_LINE} `${OPCSVR_INST_DIR}/opcpwcrpt \"${WNT_ACCOUNT_PASSWD}\"`"
        fi

	echo "${PASSWORD_LINE}" > $OPC_OUTPUT_FILE
	unset PASSWORD_LINE
    fi
}

#===============================================================================
#
#  DESCRIPTION
#    determinate the difference of two date given in input parameters and
#    compare the result with the maximum value allowed
#
#  PARAMETERS
#    date_1 .. day and year values of first date in format %j/%Y
#    date_2 .. day and year value of second date in format %j/%Y
#    time_1 .. hours, minutes and seconds values of first date in
#              format %H:%M:%S
#    time_2 .. hours, minutes and seconds values of second date in
#              format %H:%M:%S
#    max_diff .. max difference still allowed
#
#  EXIT_CODE
#    0 .. OK
#    1 .. programing error or time difference too big
#
time_diff()
{
    T1_DATE=$1         # first date value
    T1_TIME=$2         # first time value
    T2_DATE=$3         # second date value
    T2_TIME=$4         # second time value
    MAX_DIFF=$5        # max. time difference in seconds
 
    # split date/time in its components
    #
    T1_DD=`echo ${T1_DATE} | cut -d / -f 1`
    T1_YY=`echo ${T1_DATE} | cut -d / -f 2`
    T1_H=`echo  ${T1_TIME} | cut -d : -f 1`
    T1_M=`echo  ${T1_TIME} | cut -d : -f 2`
    T1_S=`echo  ${T1_TIME} | cut -d : -f 3`

    T2_DD=`echo  ${T2_DATE} | cut -d / -f 1`
    T2_YY=`echo  ${T2_DATE} | cut -d / -f 2`
    T2_H=`echo   ${T2_TIME} | cut -d : -f 1`
    T2_M=`echo   ${T2_TIME} | cut -d : -f 2`
    T2_S=`echo   ${T2_TIME} | cut -d : -f 3`

    # a) check, date/time is greater
    #
    if [ ${T1_YY} -eq ${T2_YY} ]
    then
	# same year
	#
	if [ ${T1_DD} -eq ${T2_DD} ]
	then
	    # same day of year
	    #
	    GREATER=0
	elif [ ${T1_DD} -gt ${T2_DD} ]
	then
	    # day of T1 is greater
	    #
	    GREATER=1
	else
	    # day of T2 is greater
	    #
	    GREATER=2
	fi 
    elif [ ${T1_YY} -gt ${T2_YY} ]
    then
	# year of T1 is greater
	#
	GREATER=1
    else
	# year of T2 is greater
	#
	GREATER=2
    fi

    if [ ${GREATER} -eq 0 ]
    then
	# day of year and year are equal, now check time
	#
	if [ ${T1_H} -eq ${T2_H} ]
	then
	    # same hour
	    #
	    if [ ${T1_M} -eq ${T2_M} ]
	    then
                # same minute
	        #
                if [ ${T1_S} -eq ${T2_S} ]
                then
                    # date/time are totally equal
	            #
                    return 0
                elif [ ${T1_S} -gt ${T2_S} ]
                then
                    # seconds of T1 is greater
	            #
                    GREATER=1
                else
                    # seconds of T2 is greater
	            #
                    GREATER=2
                fi 
	    elif [ ${T1_M} -gt ${T2_M} ]
	    then
                # minutes of T1 is greater
	        #
                GREATER=1
	    else
                # minutes of T2 is greater
	        #
                GREATER=2
	    fi
	elif [ ${T1_H} -gt ${T2_H} ]
	then
	    # hours of T1 is greater
	    #
	    GREATER=1
	else
	    # hours of T2 is greater
	    #
	    GREATER=2
	fi
    fi    

    if [ ${GREATER} -eq 1 ]
    then
	# exchanges date/times, that greater date/time is available in T2_XXX
	#
	TMP_DD=${T1_DD}; TMP_YY=${T1_YY}
	TMP_H=${T1_H};   TMP_M=${T1_M};   TMP_S=${T1_S}
	T1_DD=${T2_DD};  T1_YY=${T2_YY}
	T1_H=${T2_H};    T1_M=${T2_M};    T1_S=${T2_S}
	T2_DD=${TMP_DD}; T2_YY=${TMP_YY}
	T2_H=${TMP_H};   T2_M=${TMP_M};   T2_S=${TMP_S}
    fi

    if [ ${T2_S} -lt ${T1_S} ]
    then
	T2_S=`expr ${T2_S}   \+ 60`
	T1_M=`expr ${T1_M} \+  1`
    fi

    if [ ${T2_M} -lt ${T1_M} ]
    then
	T2_M=`expr ${T2_M} \+ 60`
	T1_H=`expr ${T1_H} \+  1`
    fi
 
    if [ ${T2_H} -lt ${T1_H} ]
    then
	T2_H=`expr ${T2_H} \+ 24`
	T1_DD=`expr ${T1_DD} \+ 1`
    fi

    if [ ${T2_DD} -lt ${T1_DD} ]
    then
	# add one more day, if year of T1 is leap-year
	#
	if [ \( `expr ${T1_YY} \%   4` -eq 0 \
		-o `expr ${T1_YY} \% 400` -eq 0 \) \
             -a `expr ${T1_YY} \% 100` -ne 0 ]
	then
	    T2_DD=`expr ${T2_DD} \+ 366`
	else
	    T2_DD=`expr ${T2_DD} \+ 365`
	fi
	T1_YY=`expr ${T1_YY} \+ 1`
    fi

    DIFF_S=`expr ${T2_S} \- ${T1_S}`
    DIFF_M=`expr ${T2_M} \- ${T1_M}`
    DIFF_H=`expr ${T2_H} \- ${T1_H}`
    DIFF_DD=`expr ${T2_DD} \- ${T1_DD}`
    DIFF_YY=`expr ${T2_YY} \- ${T1_YY}`

    T_DIFF=`expr ${DIFF_S} \+ \( ${DIFF_M} \* 60 \) \+ \( ${DIFF_H} \* 3600 \)`
    T_DIFF=`expr ${T_DIFF} \+ \( ${DIFF_DD} \* 86400 \)`
    
    unset T1_DATE T1_TIME T2_DATE T2_TIME  GREATER
    unset DIFF_S  DIFF_M  DIFF_H  DIFF_DD  DIFF_YY
    unset T1_D    T1_M    T1_H    T1_DD    T1_YY
    unset T2_D    T2_M    T2_H    T2_DD    T2_YY
    unset TMP_D   TMP_M   TMP_H   TMP_DD   TMP_YY
  
    if [ ${T_DIFF} -lt 0 ]
    then
	# time difference negative - PROGRAMMING ERROR
	oi_error "opc" 154 ${T_DIFF}
	unset T_DIFF MAX_DIFF
	return 1
    fi

    if [ ${T_DIFF} -gt ${MAX_DIFF} ]
    then
	unset T_DIFF MAX_DIFF
	return 1
    fi

    unset T_DIFF MAX_DIFF
    return 0
}

#===============================================================================
#
#  DESCRIPTION
#    resolve UNIX node Internet address, by using nslookup utility, from
#    node Name, if ip_address is not set (ip_address equals node_name).
#
#  PARAMETERS
#    node_name  .. node Name
#    ip_address .. node IP address
#
#  ENVIRONMENT VARIABLES
#    PING_NODE .. IP address of node
#
#  EXIT_CODE
#    0 .. ok, node is known
#    1 .. error, node is not known
#    2 .. usage error
#

resolve_name()
{
  ND_NAME=`get_hostname_by_ip $1`
  if [ -z "${ND_NAME}" ]; then
    unset ND_NAME
    return 1
  else
    ND_NAME=`get_ip_by_hostname $1`
    if [ -z "${ND_NAME}" ]; then
      unset ND_NAME
      return 1
    else
      PING_NODE=${ND_NAME}
      unset ND_NAME
      return 0
    fi
  fi
}

#===============================================================================
#
#  DESCRIPTION
#    Generic ping function used by UNIX and MPE.
#    This script checks if node specified as parameter is reachable.
#    Node specified as first parameter must already be in a form suitable
#    for the corresponding platform.
#
#  PARAMETERS
#    PING_NODE .. addres of OPC_NODE suitable for ping
#
#  EXIT_CODE
#    0 .. ok, node is reachable via ping utility
#    1 .. error, node is not reachable
#
do_ping()
{
    PING_NODE=$1

  if [ "${OPC_PACKAGE_TYPE}" = "RPC_BBC" ]
  then

    /opt/OV/bin/ovbbccb -ping https://${PING_NODE} > /dev/null 2>&1

    if [ $? -eq 0 ]
    then
      return 0
    fi 
  fi

    START_T=`TZ=${TIME_ZONE} date "${DATE_FORMAT}"`

    ping ${PING_NODE} 64 ${SPKG} >${OUTPUT_F} 2>&1
    RET_V=$?

    STOP_T=`TZ=${TIME_ZONE} date "${DATE_FORMAT}"`

    if [ ${RET_V} -ne 0 ]
    then
	# maybe host unknown or something else went wrong
	#
	oi_error "opc" 603 ${OPC_NODE} ${PING_NODE}
	oi_show_file ${OUTPUT_F}
	produce_password
	return 1
    fi
    unset RET_V

    START_DATE=`echo ${START_T} | awk '{print $1}'`
    START_TIME=`echo ${START_T} | awk '{print $3}'`
    STOP_DATE=`echo ${STOP_T} | awk '{print $1}'`
    STOP_TIME=`echo ${STOP_T} | awk '{print $3}'`

    time_diff ${START_DATE} ${START_TIME} ${STOP_DATE} ${STOP_TIME} 10
    TIME_DIFF=$?

    unset START_DATE START_TIME STOP_DATE STOP_TIME START_T STOP_T

    if [ ${TIME_DIFF} -eq 1 ]
    then
	# system did not respond correctly
	#
	oi_error "opc" 603 ${OPC_NODE} ${PING_NODE}
	produce_password
        unset PING_NODE 
	return 1
    fi

    RPKG=`cat ${OUTPUT_F} | awk '/received/ {print $4}'`
    if [ ${RPKG} -eq 0 ]
    then
	# system did not respond 
	#
	oi_error "opc" 603 ${OPC_NODE} ${PING_NODE}
	produce_password
        unset PING_NODE 
	return 1
    fi
    
    unset PING_NODE 

    if [ ${RPKG} -ne ${SPKG} ]
    then
	# 2. system did not send back all packets 
	# print message that ${SPKG} packets was sent and ${RPKG} was received
	#
	oi_note "opc" 238 ${OPC_NODE} ${SPKG} ${RPKG}

	# ask if work will continue on such communication
	#
	oi_prompt_and_ask "opc" 240 ${OPC_NODE} "" 239 ${OPC_NODE}
	case "${OPC_ANSWER}" in
            [${OPC_YES_LIT}]*)
                return 0
                ;;
            *)
		produce_password
		return 1
                ;;
        esac
    fi

    return 0
}

#===============================================================================
#
#  DESCRIPTION
#    Generic check opcclta on node function.
#    This script checks if on node specified as parameter opcclta is alive.
#
#  PARAMETERS
#    OPCRAGT_NODE .. addres of OPC_NODE suitable for ping
#
#  ENVIRONMENT
#    CA_VERSION .. version of ITO preinstalled agent
#    CA_STATE   .. state of ITO agent "preinstalled" or ""
#
#  EXIT_CODE
#    0 .. ok, node is reachable via opcragt utility
#    1 .. error, node is not reachable
#
do_opcragt()
{
    OPCRAGT_NODE=$1

    #
    #   call opc_ragt -status ${OPCRAGT_NODE} and check output
    #
    LANG=C LC_ALL=C ${OPCSVR_BIN_DIR}/opcragt -status ${OPCRAGT_NODE} 2>/dev/null > $OUTPUT_F
    CA_STATUS=`cat $OUTPUT_F | awk '/^Control Agent/ {print $5 $6}'`
    MA_STATUS=`cat $OUTPUT_F | awk '/^Message Agent  / {print $(NF-1) $(NF) }'`
    MA_STATUS_BUFF=`cat $OUTPUT_F | awk '/^Message Agent  / {print $(4) }'`
    AA_STATUS=`cat $OUTPUT_F | awk '/^Action Agent/ {print $5 $6}'`

    #TEMPORARY: correct awk program when output of opcragt will be known
    #

## 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
##    CA_STATE=`cat $OUTPUT_F | awk '/^ITO Managed Node status/ {print $6 }'`
##    CA_VERSION=`cat $OUTPUT_F | awk '/^ITO Managed Node status/ {print $7 }'`
##    if [ "$CA_STATE" = "preinstalled" ]
##    then
##	# ITO preinstall agent configuration is on this node
##	# Only Control Agent is running and opcinfo file isn\'t right one
##	# Configure ITO agent on node
##	#
##	${OPCSVR_BIN_DIR}/opcragt -prepare_installation ${OPCRAGT_NODE} 
##	if [ $? -ne 0 ]
##	then
##	    oi_error "opc" 682 $OPCRAGT_NODE
##	    unset OPCRAGT_NODE CA_STATUS
##	    return 1
##	fi
##	CA_STATUS=`${OPCSVR_BIN_DIR}/opcragt -status ${OPCRAGT_NODE} | \
##		    awk '/^Control Agent/ {print $4 $5}'`
##    fi
## START_OF_COMM

    unset OPCRAGT_NODE
    #all three agents must be active
    if [ "$MA_STATUS" = "isrunning" -o "$MA_STATUS_BUFF" = "(buffers)" ]
    then
      if [ "$CA_STATUS" = "isrunning" -a "$AA_STATUS" = "isrunning" ]
      then
  	  return 0
      else
	  return 1
      fi
    else
      return 1
    fi

}

#==============================================================================
#
#  DESCRIPTION
#   check, if OS version of Managed Node is supported by ITO software 
#   OPC_VERSION version.
#
#  ENVIRONMENT
#    OPC_VERSION .. is possible to be changed, if actual OPC_VERSION version
#                   ITO software doesn't support OS version of Managed Node
#
#  EXIT_CODE
#    0 .. OK
#    1 .. ERROR  
#
chk_os_support()
{
    REQ_FILE=${OPC_INST_DIR}/${OPC_PLATFORM}/require.dat
    ACT_OS_VER=$1	    	    # OS version of Managed Node

    #	check require.dat file for getting all supported versions
    #
    CHK_OS_VER=""
    for SUP_OS_VER in `awk "/^${OPC_VERSION}/ {for (x=3; x<=NF; x++) print\\$(x)}" \
		      ${REQ_FILE}`
    do
	if [ "`echo ${ACT_OS_VER} | awk /${SUP_OS_VER}/`" != "" ]
	then
	    CHK_OS_VER=${ACT_OS_VER}
	    break
	fi
    done

    if [ -n "${CHK_OS_VER}" ]
    then
	#   OS version of Managed Node is supported by OPC_VERSION
	#   version ITO software
	#
	unset ACT_OS_VER CHK_OS_VER
	return 0
    fi

    ##	if OS version of Managed Node isn't supported by this OPC_VERSION ,
    #	find lates one. This operation relay on fact, that callout scripts
    #	for new OPC_VERSION ITO software are equivalent to callout scripts of
    #	current version ITO software.
    #
    if [ "${OPC_SUBPRODUCT}" = "${OPC_PRODUCT}" ]
    then
        CUR_SUBPRODUCT="OVO"
    else
        CUR_SUBPRODUCT="${OPC_SUBPRODUCT}"
    fi

    oi_warning "opc" 036 ${OPC_VERSION} ${OPC_PLATFORM} \
			 ${ACT_OS_VER} ${OPC_NODE} ${CUR_SUBPRODUCT}

    # find the lates installed ITO Agent software, which support ACT_OS_VER
    #
    CHK_OPC_VER=`awk '{if ($1 != "#") \
                        {for (x=3; x<=NF; x++) \
                          {if (index(vers,$(x)) != 0) \
                            {print $1}
			  } \
		        } \
		      }' \
                  vers=${ACT_OS_VER} ${REQ_FILE} |
                  sort | awk 'BEGIN {L=""} {L=$1} END{print L}'`

    if [ -z "${CHK_OPC_VER}" ]
    then
    	#   no ITO version found supporting the OS release
	#
        oi_error "opc" 155 ${OPC_PLATFORM} ${ACT_OS_VER} ${CUR_SUBPRODUCT}
	unset ACT_OS_VER CHK_OS_VER CHK_OPC_VER
        return 1
    else
        #   ask if ITO software  maintenance using this version should be
        #   continued
        oi_warning "opc" 156 ${CHK_OPC_VER} ${OPC_VERSION} \
			     ${OPC_PLATFORM} ${ACT_OS_VER} ${CUR_SUBPRODUCT}
	oi_prompt_and_ask "opc" 143 "${OPC_NODE}" "" \
				157 ${OPC_NODE} ${CHK_OPC_VER} ${CUR_SUBPRODUCT}
	if [ "${OPC_ANSWER}" = "${OPC_OIANSWER_DEF}" ]
	then
	    OPC_ANSWER=${OPC_YES_LIT}
	fi
	case "${OPC_ANSWER}" in
	    [${OPC_YES_LIT}]*)
	    #	continue with new OPC_VERSION
	    #
          	OPC_VERSION=${CHK_OPC_VER}; export OPC_VERSION
		EXIT_VAL=0
	    ;;
	    *)
          	EXIT_VAL=1
	    ;;
	esac
    fi

    unset ACT_OS_VER CHK_OS_VER CHK_OPC_VER
    if [ ${EXIT_VAL} -eq 0 ]
    then 
	unset EXIT_VAL
	return 0
    else
	unset EXIT_VAL
	return 1
    fi

}    

#==============================================================================
#
#  DESCRIPTION
#    Set environment variables for N-th node
#
#  ENVIRONMENT
#    OPC_VERSION    Current version
#
#  EXIT CODE
#    0 .. Ok
#    1 .. Out of scope
#

oi_node_entry()
{
    ND_CNT=$1
    if [ $ND_CNT -gt $NODES -o $ND_CNT -le 0 ]
    then
	return 1
    fi
    
    eval `awk 'BEGIN { i = 1 }
    {
	if ( (NF > 0) && ($1 != "#" )) {
	    if ( i == COUNT ) {
		printf "OPC_NODE=%s\n",            $1
		printf "OPC_MNGD_NODE=%s\n",       $1
		printf "OPC_IP_ADDRESS=%s\n",      $2
		printf "OPC_PLATFORM=%s\n",        $3
		printf "OPC_VERSION=%s\n",         $6
		printf "OPC_NODE_LOGD=%s\n",       $7
		printf "OPC_FORCE_UPD=%s\n",       $8
		printf "OPC_DEPOT_NODE=%s\n",      $9
		printf "OPC_ACCESS_METHOD=%s\n",   $10
		printf "OPC_PACKAGE_NAME=%s\n",    $11
		printf "OPC_DEPOT_NAME=%s\n",      $12
		printf "OPC_COMPR_PKG_TRANS=%s\n", $13
		printf "OPC_COMPR_PKG=%s\n",       $14
		printf "OPC_INST_METHOD=%s\n",     $15
		printf "OPC_INST_USER=%s\n",       $16
	    }
	    i++
	}
    }' COUNT=$ND_CNT <$INFO_FILE`
    
    if [ $OPC_VERSION = "LATEST" ]
    then
	OPC_VERSION=`awk '{ if( $1 == PLATFORM ) print $2 }' \
	PLATFORM=$OPC_PLATFORM <$VERSIONS_CACHE_FILE`
    fi

    PASSWORD_LINE=`awk '{
	if ( $1 == NODE_KEY ) print $0
    }' NODE_KEY=$OPC_NODE <$PASSWORD_FILE`
    if [ `echo "$PASSWORD_LINE" | wc -w` -lt 2 ] 
    then
	echo "$OPC_NODE NOTREADY" >> $PASSWORD_LINE
    fi
    eval `echo "$PASSWORD_LINE" | awk '{
	printf "OPC_STATUS=%s\n", $2
    }'`

    return 0
}
#==============================================================================
#
#  DESCRIPTION
#    Check if current ITO version to be installed on Managed Node is 
#    Voyager Sound Track one.
#
#  ENVIRONMENT
#    OPC_VERSION    Current version
#
#  EXIT CODE
#    0     .. This is VST or later release ( >= A.04.00 )
#    1,2,3 .. This is older version than VST ( < A.04.00)
#

vst_release()
{
    case ${OPC_VERSION} in
    ?.00.*)  return 1;;
    ?.01.*)  return 1;;
    ?.02.*)  return 2;;
    ?.03.*)  return 3;;
    *)       return 0;;
    esac
}

#==============================================================================
#
#  DESCRIPTION
#    dummy function
#

VOID_SCRIPT()
{
    return 1
}

