#!/sbin/sh
# OPC_WHAT_STRING="@(#)HP OpenView Operations A.07.10 (05/07/02)"
################################################################################
#
# File:         opcvmsprlg
# Description:  OpenVMS PROLOG_SCRIPT script
# Language:     Bourne Shell
# Package:      HP OpenView IT/Operations
#
# (c) Copyright Hewlett-Packard Co. 1993 - 2004
#
################################################################################

#
#  DESCRIPTION
#    OpenVMS PROLOG_SCRIPT script.
#    This script does all the actions, which are prescribed for callout
#    PROLOG_SCRIPT. Additionaliy it performs actions to support generic 
#    unix cluster.
#    Following is list of all actions (actions for generic unix cluster support
#    are marked with *):
#
#      1. create OPC_TMP_DIR directory on Managed Node, if it doesn't exist.
#         Mark this fact and remove OPC_TMP_DIR from Managed Node when
#         prolog actions are done. The same procedure is performed for
#         Managed Node's cluster server system, if it is involved.
#         
#      2. check OS of Managed Node.
#         This does callout $OPC_OS_CHECK script (see OS_CHECK(1m))
#         Parameters:
#           result_file
#         Return code:
#           0 .. OK
#           1 .. ERROR in OS
#         If return code equals 1, display error and skip this node.
#         Otherwise get the actual OS version from <result_file>,
#         use actual OS version to determinate if ITO Managed Node
#         software support this OS version. If no, skip this node.
#         Remove temporary result_file.
#
#     *3. check Managed Node kind.
#         This does callout $OPC_NODE_CHECK script ( see NODE_CHECK(1m))
#         Parameters:
#           result_file
#         Return code:
#           0 .. OK
#           1 .. ERROR in Node kind
#         If return code equals 1, display error and skip this node.
#         Otherwise get the cluster server system name from <result_file>.
#         If it is empty, Managed Node is stand-alone system; 
#         go to action 7.
#
#     *4. Managed Node is cluster client system. Perform test for cluster
#         server system. Before that, save Managed Node values.
#         This does unix common function: opcuxenvsave (see ??)
#         Parameters :
#           SAVE
#         Output environment variables:
#           OPC_NODE OPC_PASSWD_REQ OPC_PASSWD 
#         Return code:
#           0 .. OK
#           1 .. ERROR  
#         If return code equals 1, display error and skip this node.
#
#     *5. check password for cluster server system.
#         This is unix common function: opcuxpwget (see opcuxpwget(1m))
#         Parameters :
#         Output environment variables:
#           OPC_PASSWD_REQ OPC_PASSWD
#         Return code:
#           0 .. OK
#           1 .. ERROR in password 
#         If return code equals 1, display error and skip this node.
#
#
#     *6. check OS of Managed Node's cluster server system.
#         This does callout $OPC_CLUSTER_CHECK script ( see CLUSTER_CHECK(1m))
#         Parameters:
#         Return code:
#           0 .. OK
#           2 .. ERROR
#         If return code equals 1, display error and skip this node.
#         NOTE: environment variable OPC_NODE is still set on Managed Node's
#               cluster server system.
#
#      7. check ITO software version.
#         This does callout $OPC_VERSION_CHECK script ( see VERSION_CHECK(1m))
#         Parameters:
#           result_file
#         Return code:
#           0 .. ITO software not yet installed
#           1 .. ITO software not yet installed, but installation not
#                possible
#           2 .. ITO software already installed, but Managed Node belongs
#                to different Management server;
#                the name of actual Management Server system 
#                is delivered in result_file
#           3 .. same version of ITO software already installed
#           4 .. different version of ITO software already installed;
#                the installed version is delivered in result_file
#         
#         If environment variable OPC_METHOD equals "INSTALL" (installation 
#         part),
#         then return code is evaluated. 
#         Following is list of appropriate actions:
#           0 :  continue 
#           1 :  skip this node
#           2 :  if actual Management Server system name equals "unknown"
#                ask user for confirmation of installation;
#                otherwise skip this node
#           3 :  ask user for confirmation of installation
#           4 :  ask user for confirmation of installation, if
#                installed ITO version is greater then actual one
#         Following environment variables will be set:
#           OPC_PREV_VERS, OPC_OVERRIDE
#
#         If environment variable OPC_METHOD equals "REMOVE" (removal part),
#         then return code is evaluated. 
#         Following is list of appropriate actions:
#           0 :  skip this node
#           1 :  skip this node
#           2 :  if actual Management Server system name equals "unknown"
#                ask user for confirmation of installation and go to action 10;
#                otherwise skip this node
#           3 :  go to action 10 
#           4 :  go to action 10 
#
#      8. check disks space on Managed node.
#         Required disks space for ITO software depends on whether or not ITO 
#         software is already installed on Managed Node. If ITO software is
#         already installed, only delta between installed and actual
#         ITO software's size is required.
#         Check disks space  does callout $OPC_SPACE_CHECK 
#         script ( see SPACE_CHECK(1m))
#         Parameters:
#           ITO_tape_size
#	    ITO_sw_size
#           result_file
#         Return code:
#           0 .. OK
#           1 .. not enough free disk space
#         If return code equals 1, display error and skip this node.
#         Otherwise get the actual OPC_TAPE_DIR from <result_file>.
#     
#
#     x.  check if Management Server system is reachable from Managed Node
#         When server has several IP addresses , select (ask user)
#         appropriate one and store it into work file (install script shoud
#         passed it to remote script, which will patch opcinfo file).
#         If test fails skip this node.
#
#     9.  If Managed Node is cluster client system, save cluster server
#         environment variables and restore Managed Node ones.
#         This does unix common function: opcuxenvsave (see oi_??)
#         Parameters :
#           RESTORE
#         Output environment variables:
#           OPC_NODE OPC_PASSWD_REQ OPC_PASSWD 
#           OPC_CL_NODE OPC_CL_PASSWD_REQ OPC_CL_PASSWD
#         Return code:
#           0 .. OK
#           1 .. ERROR  
#         If return code equals 1, display error and skip this node.
#
#     10. Produce <work_line>.
#     
     
#
#  PARAMETERS
#    output_file .. name of temporary file where line of work_file(5) for
#                   actual node will be placed
#
#  EXIT_CODE
#    0 .. OK
#    1 .. ERROR  
#

# defined in every script - the version of this script
INST_OPCVERS=A.07.10

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

# define module name

. "$OPC_AGTINST_DIR"/opcprocs
. "$OPC_AGTINST_DIR"/opcvmsprocs

#==============================================================================
#
#  Produce work_file(5) line for actual $OPC_NODE.
#
produce_work()
{
    if [ "${OPC_STATUS}" = "NOTREADY" ]
    then
	#   skip this OPC_NODE
	#
	# skipping warning issued in opc_inst.sh
	echo "${OPC_NODE} ${OPC_STATUS}" > $OPC_OUTPUT_FILE
    else
	#   produce real work_file(5) line for this node
	#

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

	if [ -z "${OPC_CL_NODE}" ]
	then
	    #	cluster server system is unknown
	    #
	    OPC_CL_PASSWD_REQ=""
	    OPC_CL_PASSWD=""
	else
	    if [ -z "${OPC_CL_PASSWD}" ]
	    then
		#	zero length password reset to default value
		#
		OPC_CL_PASSWD=OPC_UNKNOWN_PASSWORD
	    fi
	fi

	WORK_LINE="${OPC_NODE}"
	WORK_LINE="${WORK_LINE} ${OPC_STATUS}"
	WORK_LINE="${WORK_LINE} ${OPC_PASSWD_REQ}"
	WORK_LINE="${WORK_LINE} ${OPC_PASSWD}"
	WORK_LINE="${WORK_LINE} ${OPC_VERSION}"
	WORK_LINE="${WORK_LINE} ${OPC_TAPE_DIR}"
	WORK_LINE="${WORK_LINE} ${KERBEROS_BYPASS}"
	ACTIVATE_ONLY=0
	WORK_LINE="${WORK_LINE} ${ACTIVATE_ONLY}"
	WORK_LINE="${WORK_LINE} ${OPC_OVERRIDE}"
	#new one
	if [ -z "${OPC_RESOLVED_IP}" ]
	then
	    OPC_RESOLVED_IP="VOID"
	fi
	WORK_LINE="${WORK_LINE} ${OPC_RESOLVED_IP}"
	
	WORK_LINE="${WORK_LINE} ${OPC_NODE_KIND}"
	WORK_LINE="${WORK_LINE} ${OPC_CL_NODE}"
	WORK_LINE="${WORK_LINE} ${OPC_CL_IP_ADDRESS}"
	WORK_LINE="${WORK_LINE} ${OPC_CL_PASSWD_REQ}"
	WORK_LINE="${WORK_LINE} ${OPC_CL_PASSWD}"

	echo "${WORK_LINE}" > $OPC_OUTPUT_FILE
	unset WORK_LINE
    fi
    
    # remove OPC_TMP_DIR on Managed Node
    #
    if [ "${OPC_NODE_KIND}" = "CLIENT" ]
    then
	# remove OPC_TMP_DIR on Managed Node's cluster server
	#
	opcuxenvsave SAVE
    
    fi
}

#==============================================================================
#
#  DESCRIPTION
#    Create OPC_TMP_DIR on OPC_NODE node
#
#  ENVIRONMENT
#    OPC_TMP_DIR .. path-name of directory to be created
#    OPC_TMP_CREATED .. 1 when temporary directory was created
#
#  EXIT CODE
#      0 .. task successfully done
#      1 .. error
#

mk_tmp_dir()
{
    OPC_TMP_CREATED=0

#    FTP_CMD="dir ${OPC_TMP_DIR}/* ${VMS_FTP_OUTPUT}"
#    opcvmsftp "${FTP_CMD}"
    opcvmsftp_chk_access ${OPC_TMP_DIR}
    if [ $? -ne 1 ]
    then
        # temporary directory must be created
        #
        FTP_CMD="mkdir ${OPC_TMP_DIR}"
        opcvmsftp "${FTP_CMD}"
#        FTP_CMD="dir ${OPC_TMP_DIR}/* ${VMS_FTP_OUTPUT}"
#        opcvmsftp "${FTP_CMD}"
        opcvmsftp_chk_access ${OPC_TMP_DIR}
        if [ $? -ne 1 ]
        then
            #   Can't create directory
            #
            oi_error "opc" 061 ${OPC_TMP_DIR} ${OPC_NODE}
            return 1
        fi
 
        OPC_TMP_CREATED=1
    fi
    
    return 0
}

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

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

#   set global environment variables
#
REQ_FILE=${OPC_INST_DIR}/${OPC_PLATFORM}/require.dat
TMP_FILE=${OPC_TOPC}/opc-$$.tmp

#   initialize OPC_NODE environment variables
#
OPC_STATUS="NOTREADY"
OPC_OVERRIDE=0
OPC_PREV_VERS=""
export OPC_STATUS OPC_OVERRIDE OPC_PREV_VERS
OPC_TAPE_DIR=${OPC_TMP_DIR}

OPC_TMP_CREATED=0

#   initialize OPC_NODE's cluster server environment variables
#
OPC_CL_NODE=""
OPC_CL_IP_ADDRESS=""
OPC_CL_PASSWD_REQ=0
OPC_CL_PASSWD=""
export OPC_CL_NODE OPC_CL_IP_ADDRESS OPC_CL_PASSWD_REQ OPC_CL_PASSWD

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

if [ "${OPC_METHOD}" = "INSTALL" ]
then
    if [ ${OPC_DEPOT_NODE} = "VOID" -o ${OPC_DEPOT_NODE} = ${OPC_NODE} ]
    then
        #   1. create OPC_TMP_DIR directory on Managed Node
        #
        mk_tmp_dir
        if [ $? -eq 1 ]
        then
            produce_work 
            EXIT 1
        fi
    fi
fi

OPC_NODE_KIND="SERVER"
OPC_STATUS="READY"
produce_work

#   remove temporary file
#
rm -f ${TMP_FILE}

    # Get OS Version
    #
    opcvms_version
    if [ $? -eq 1 ]
    then
        echo "Cannot obtain OpenVMS version - continuing"
        EXIT 0
    else
        first_letter=`echo $VMS_VERSION | cut -c 1`
        if [ "$first_letter" != "V" ]
        then
            # Not a released version - ask if its okay
            echo "          The version of OpenVMS on ${OPC_NODE} is ${VMS_VERSION}."
            echo "          It cannot be determined if this version is greater than V7.3-1."
            printf "          Is this version greater than or equal to V7.3-1? (y/n) <y> " \c
            ask_yn=`line -t ${OPC_TIME_OUT}`
            if [ $? -ne 0 ]
            then
                #time out has occurred
                ask_yn="y"
            fi
            if [ -z "$ask_yn" ]
            then
                #default is wanted
                ask_yn="y"
            fi
            if [ "$ask_yn" = "y" ]
            then
                #it's okay, just exit
                EXIT 0
            else
                #not okay, exit with error
		echo "${OPC_NODE} NOTREADY" > $OPC_OUTPUT_FILE
                EXIT 1
            fi
        fi
        version_num=`echo $VMS_VERSION | cut -c 2-`
        major_version=`echo $version_num | cut -d . -f1`
        minor_version=`echo $version_num | cut -d . -f2`
	t_major=`echo $major_version | tr -d '[:digit:]'`
	t_minor=`echo $minor_version | tr -d '[:digit:]'`
	# Test for any non-numeric characters in the version number
	if [ "$t_major" -o "$t_minor" ]
	then
	    echo "          The version of OpenVMS on ${OPC_NODE} is ${VMS_VERSION}."
	    echo "          The minimum is V7.3-1."
	    echo "          It cannot be determined if the version is above the minimum."
	    printf "          Is this version greater than or equal to V7.3-1? (y/n) <y> " \c
	    ask_yn=`line -t ${OPC_TIME_OUT}`
	    if [ $? -ne 0 ]
	    then
		#time out has occurred
		ask_yn="y"
	    fi
	    if [ -z "$ask_yn" ]
	    then
		#default is wanted
		ask_yn="y"
	    fi
	    if [ "$ask_yn" = "y" ]
	    then
		#it's okay, just exit
		EXIT 0
	    else
		#not okay, exit with error
		echo "${OPC_NODE} NOTREADY" > $OPC_OUTPUT_FILE
		EXIT 1
	    fi
	fi
        if [ $major_version -gt 7 ]
        then
            #it's okay, just exit
            EXIT 0
        fi
        if [ $major_version -lt 7 -o $minor_version -lt 3 ]
        then
            echo "          The version of OpenVMS on ${OPC_NODE} is ${VMS_VERSION}."
            echo "          This is below the minimum of V7.3-1."
	    echo "${OPC_NODE} NOTREADY" > $OPC_OUTPUT_FILE
            EXIT 1
        fi
        if [ $minor_version -gt 3 ]
        then
            #it's okay, just exit
            EXIT 0
        fi
        echo "          The version of OpenVMS on ${OPC_NODE} is ${VMS_VERSION}."
        echo "          The minimum is V7.3-1."
        echo "          It cannot be determined if the version is above the minimum."
        printf "          Is this version greater than or equal to V7.3-1? (y/n) <y> " \c
        ask_yn=`line -t ${OPC_TIME_OUT}`
        if [ $? -ne 0 ]
        then
            #time out has occurred
            ask_yn="y"
        fi
        if [ -z "$ask_yn" ]
        then
            #default is wanted
            ask_yn="y"
        fi
        if [ "$ask_yn" = "y" ]
        then
            #it's okay, just exit
            EXIT 0
        else
            #not okay, exit with error
	    echo "${OPC_NODE} NOTREADY" > $OPC_OUTPUT_FILE
            EXIT 1
        fi
    fi

################################################################################
# end of opcvmsprlg
################################################################################
