#!/bin/sh
#
#====================================================================
#
# File Name: ov_install
# 
# Description: This script performs integration with HP OpenView and
# IBM NetView/6000.
#
# Copyright 1994 Technology by Design.  All rights reserved.
#
#====================================================================
#
# set -x
#
# Shell Variables

APP_NAME="AT-View"
ARF_FILE=ati.arf
MIB_FILE=ati.mib
SYM_FILE=ati
OID2SYM=oid_to_sym
OID2VEN=oid_to_type
FIELD_FILE=ati.fields		# SNMP agent and vendor enumerations
ICON_PREFIX=ati			# icon file <sample>.<size>.<type>

#====================================================================
# OpenView/NetView Variables.  These do not need to be changed.  
#
OV_DIR=${OV_MAIN_PATH}
OV_MIB_DIR=${OV_SNMP_MIBS}
OV_APP_DIR=${OV_REGISTRATION}/C
OV_SYM_DIR=${OV_SYMBOLS}/C
OV_FIELD_DIR=${OV_FIELDS}/C
OV_ICONS_DIR=${OV_BITMAPS}/C
OV_OID2SYM_FILE=${OV_CONF}/C/oid_to_sym
OV_OID2VEN_FILE=${OV_CONF}/oid_to_type
OV_TRAPD_FILE=${OV_CONF}/C/trapd.conf
OV_XNMEVENTS=${OV_BIN}/xnmevents
NV6000_ADDTRAP=${OV_BIN}/addtrap

#====================================================================
# Set up system commands to support several architectures.
#
setup_sys_cmds() 
{
	ARCH=`/bin/uname`
	case "$ARCH" in
	SunOS)
		awk=/bin/awk
		cp=/bin/cp
		ed=/bin/ed
		grep=/bin/grep
		mv=/bin/mv
		ps="/bin/ps -axc"
		rm=/bin/rm
		sed=/bin/sed
		whoami=/usr/ucb/whoami
		;;
	AIX)
		awk=/bin/awk
		cp=/bin/cp
		ed=/bin/ed
		grep=/usr/ucb/grep
		mv=/bin/mv
		ps="/bin/ps -ae"
		rm=/bin/rm
		sed=/bin/sed
		whoami=/bin/whoami
		;;
	HP-UX)
		awk=/usr/bin/awk
		cp=/bin/cp
		ed=/bin/ed
		grep=/bin/grep
		mv=/bin/mv
		ps="/bin/ps -aed"
		rm=/bin/rm
		sed=/bin/sed
		whoami=/usr/bin/whoami
		;;
	*)
		awk=/bin/awk
		cp=/bin/cp
		ed=/bin/ed
		grep=/bin/grep
		mv=/bin/mv
		ps="/bin/ps -axc"
		rm=/bin/rm
		sed=/bin/sed
		whoami=/usr/ucb/whoami
		;;
	esac

    ${rm} -f /tmp/.echotmp
    (echo "hi there\c" ; echo " ") > /tmp/.echotmp
    if ${grep} c /tmp/.echotmp > /dev/null 2>&1 ; then
        n='-n'
        c=''
    else
        n=''
        c='\c'
    fi
    ${rm} -f /tmp/.echotmp
}

#====================================================================
# Check to make sure you are running as root.
#
test_for_root() 
{
	user=`$whoami`
	if [ "$user" != root ] ; then
		cat << EOM

You must be root to run $0

EOM
		exit 0
	fi
}

#====================================================================
# Check for OpenView/NetView installation.
#
test_for_ov()
{
	if [ ! -d $OV_APP_DIR ]; then
		cat << EOM

Neither HP OpenView nor IBM NetView/6000 appear to 
be installed on this system.

The directory ${OV_APP_DIR} is missing.

Please run this script on your HP OpenView or 
IBM NetView/6000 system.

EOM
		exit 0
	fi
}

#====================================================================
# Verify that integration should be performed.
#
ov_integrate_query()
{
	cat << EOM

${APP_NAME} integration for HP OpenView and IBM NetView/6000.

This script will perform the following steps to integrate 
${APP_NAME} with HP OpenView or IBM NetView/6000:

	1. Registers ATI device symbols
	2. Adds a sysObjectID to symbol mapping for ATI devices
	3. Adds a sysObjectID to vendor mapping for ATI devices
	4. Adds agent and vendor enumerations for ATI devices
	5. Copies symbol icons to the bitmap directory
	6. Registers the AT-View application
	7. Copies the ATI MIB to the snmp-mibs directory.  
	8. Register AT-View traps in the trapd.conf file. 

All OpenView or NetView files will be copied to <filename>.bak 
before the changes are integrated.

EOM
	echo $n "Do you wish to perform this integration? [y] $c"
	read key
	case $key in
		n* | N*)
			echo "Integration cancelled."
			exit 0
		;;
	esac
}

#====================================================================
# Register device symbols subclass.  
#
# Check to see if $SYM_FILE exists.  
register_symbols() 
{
	if test "$SYM_FILE" != "" ; then

		$cp ${SYM_FILE} ${OV_SYM_DIR}
	else
		cat << EOM1

No symbol file exists.  Registration of the device symbols
subclass has NOT been completed.
EOM1

	fi
}

#====================================================================
# Add sysObjectID to symbol mapping.
#
# 1. Check to see if $OID2SYM data exists.
# 2a. Yes - Copy the original file to a temporary file.
# 2b. No - Continue with the next phase.
# 3. Do the following for each line in $OID2SYM:
#   4. Check to see if it is already in the $OV_OID2SYM_FILE.
#   5a. Yes - No changes are required for this line.  Go to 4.
#   5b. No - Check to see if the OID is already in the
#       $OV_OID2SYM_FILE.
#   6a. Yes - Replace the duplicate with the new $OID2SYM data.
#   6b. No - Add the $OID2SYM data to the file.
# 7. Compare the temporary file with the original file.  If they are
#    different, move the temporary file to *.bak.
# 8. Invoke ovtopofix, if it exists (only for OV).
#
register_oid2sym()
{
	if test "$OID2SYM" != "" ; then
		cat << EOM1

The ${APP_NAME} oid_to_sym data will be added to ${OV_OID2SYM_FILE}. 

EOM1

		$cp ${OV_OID2SYM_FILE} ${OV_OID2SYM_FILE}.$$

		ORIG_IFS=$IFS
		IFS=';'

		OID_DATA=`cat ${OID2SYM} | tr '\12' ';'`
		set $OID_DATA

		for MAPPING in $OID_DATA ; do
			$grep "${MAPPING}" ${OV_OID2SYM_FILE}.$$ >/dev/null 2>&1  
			if [ "$?" = 0 ] ; then
				cat << EOM

"${MAPPING}" already exists in ${OV_OID2SYM_FILE}.
EOM
			else
				IFS=':'
				MAP_DATA=`echo ${MAPPING}`
				set $MAP_DATA
				OID=`echo ${MAP_DATA} | $awk '{print $1}'`
				IFS=';'
				$grep "${OID}" ${OV_OID2SYM_FILE}.$$ > /dev/null 2>&1
				if [ "$?" = 0 ] ; then
					$ed ${OV_OID2SYM_FILE}.$$  > /dev/null 2>&1 << EOFM1
/$OID
c
$MAPPING
.
w
q
EOFM1
					cat << EOM2
 
An older sysObjectID to symbol mapping was found for OID '${OID}'.
This has been replaced with a new mapping.
EOM2
				else
					$ed ${OV_OID2SYM_FILE}.$$  > /dev/null 2>&1 << EOFM2
$
a
$MAPPING
.
w
q
EOFM2
					cat << EOM3
 
The sysObjectID to symbol mapping was added for OID '${OID}'.
EOM3
				fi
#				if test -f ${OV_DIR}/bin/ovtopofix ; then
#					${OV_DIR}/bin/ovtopofix -u -o ${OID}
#				fi
			fi
		done

		diff ${OV_OID2SYM_FILE} ${OV_OID2SYM_FILE}.$$  > /dev/null 2>&1
		if [ "$?" = 0 ] ; then
			rm ${OV_OID2SYM_FILE}.$$
		else
			mv ${OV_OID2SYM_FILE} ${OV_OID2SYM_FILE}.bak
			mv ${OV_OID2SYM_FILE}.$$ ${OV_OID2SYM_FILE}
		fi
		IFS=$ORIG_IFS

#		cat << EOM4
# 
	else
		cat << EOM5

No sysObjectID to symbol file exists.  Mapping of the sysObjectID to
symbol for ATI devices has NOT been completed.

EOM5
	fi
}

#====================================================================
# Add sysObjectID to vendor mapping.
#
# 1. Check to see if $OID2VEN data exists.
# 2a. Yes - Copy the original file to a temporary file.
# 2b. No - Continue with the next phase.
# 3. Do the following for each line in $OID2VEN:
#   4. Check to see if it is already in the $OV_OID2VEN_FILE.
#   5a. Yes - No changes are required for this line.  Go to 4.
#   5b. No - Check to see if the OID is already in the
#       $OV_OID2VEN_FILE.
#   6a. Yes - Replace the duplicate with the new $OID2VEN data.
#   6b. No - Add the $OID2VEN data to the file.
# 7. Compare the temporary file with the original file.  If they are
#    different, move the temporary file to *.bak.
#
register_oid2vendor()
{
    if test "$OID2VEN" != "" ; then
        cat << EOM1
 
The ${APP_NAME} oid_to_type data will be added to ${OV_OID2VEN_FILE}.

EOM1

        $cp ${OV_OID2VEN_FILE} ${OV_OID2VEN_FILE}.$$

        ORIG_IFS=$IFS
        IFS=';'

		OID_DATA=`cat ${OID2VEN} | tr '\12' ';'`
        set $OID_DATA

        for MAPPING in $OID_DATA ; do
			$grep "${MAPPING}" ${OV_OID2VEN_FILE}.$$ >/dev/null 2>&1
			if [ "$?" = 0 ] ; then
				cat << EOM

"${MAPPING}" already exists in ${OV_OID2VEN_FILE}.
EOM
			else
				IFS=':'
				MAP_DATA=`echo ${MAPPING}`
				set $MAP_DATA
				OID=`echo ${MAP_DATA} | $awk '{print $1}'`
				IFS=';'
				$grep "${OID}" ${OV_OID2VEN_FILE}.$$ > /dev/null 2>&1
				if [ "$?" = 0 ] ; then
					$ed ${OV_OID2VEN_FILE}.$$  > /dev/null 2>&1 << EOFM1
/$OID
c
$MAPPING
.
w
q
EOFM1
					cat << EOM2

An older sysObjectID to type mapping was found for OID '${OID}'.
This has been replaced with a new mapping.
EOM2
				else
					$ed ${OV_OID2VEN_FILE}.$$  > /dev/null 2>&1 << EOFM2
$
a
$MAPPING
.
w
q
EOFM2
					cat << EOM3

The sysObjectID to type mapping was added for OID '${OID}'.
EOM3
				fi
			fi
		done
		diff ${OV_OID2VEN_FILE} ${OV_OID2VEN_FILE}.$$  > /dev/null 2>&1
		if [ "$?" = 0 ] ; then
			rm ${OV_OID2VEN_FILE}.$$
		else
			mv ${OV_OID2VEN_FILE} ${OV_OID2VEN_FILE}.bak
			mv ${OV_OID2VEN_FILE}.$$ ${OV_OID2VEN_FILE}
		fi
		IFS=$ORIG_IFS
	else
		cat << EOM4

No sysObjectID to type file exists.  Mapping of the sysObjectID to
type for ATI devices has NOT been completed.

EOM4
	fi
}

#====================================================================
# Add SNMP agent and vendor enumerations.
#
# Check to see if $FIELD_FILE exists.
register_enums()
{
	if test "$FIELD_FILE" != "" ; then
		cat << EOM

The ${APP_NAME} agent and vendor enumerations file, ${FIELD_FILE} 
will now be copied to ${OV_FIELD_DIR}.

EOM

		$cp ${FIELD_FILE} ${OV_FIELD_DIR}
	else
		cat << EOM2

No agent and vendor enumerations file exists.  Addition of 
the agent and vendor enumerations has NOT been completed.

EOM2
 
	fi
}
 
#==================================================================== 
# Copy symbol icons.  
# 
copy_icons() 
{
	cat << EOM

The ${APP_NAME} icon files will now be copied to ${OV_ICONS_DIR}.

EOM

	$cp ${ICON_PREFIX}*.m ${ICON_PREFIX}*.p ${OV_ICONS_DIR}
}

#====================================================================
# Copy the application registration file.  
#
copy_arf() 
{
	cat << EOM

The ${APP_NAME} application registration file will
now be copied to ${OV_APP_DIR}. 

EOM

	$cp $ARF_FILE $OV_APP_DIR
	if [ ! -f $OV_APP_DIR/$ARF_FILE ] ; then
    cat << EOM1

Warning!!  An error has occurred.  The application registration
file could not be copied to the registration file directory.

Integration terminated.

EOM1
	exit 0
	fi
}

#====================================================================
# Copy the MIB file.
#
copy_mib()
{
	if test "$MIB_FILE" != "" ; then
		cat << EOM

The ${APP_NAME} MIB file will now be copied to ${OV_MIB_DIR}. 

EOM

		$cp $MIB_FILE $OV_MIB_DIR
		if [ ! -f $OV_MIB_DIR/$MIB_FILE ] ; then
			cat << EOM1

Warning!!  An error has occurred.  The ${APP_NAME} MIB file, 
${MIB_FILE} could not be copied to the MIB directory.

Integration terminated.

EOM1
			exit 0
		fi
	else
		cat << EOM2

No MIB file exists.  A device MIB has NOT been registered.

EOM2
	fi
}
#==================================================================== 
# Register Traps
# 
register_traps() 
{
TIMESTAMP=`date "+%y%m%d%H%M%S"`
case "$ARCH" in

	AIX)
		cat << EOM

Registering Traps with NV6000.

EOM
		cp $OV_TRAPD_FILE $OV_TRAPD_FILE.$TIMESTAMP
		$NV6000_ADDTRAP  -n alliedTelesyn -l AT-ViewThresholdExceded  -i .1.3.6.1.4.1.207 -g 6 -s 6 -S 2 -o V -t 0 -c "Threshold Events" -F '$1' -D "Threshold Event from AT-View"
		;;
	*)
		cat << EOM

Registering Traps with HPOV.

EOM
		cp $OV_TRAPD_FILE $OV_TRAPD_FILE.$TIMESTAMP
                $OV_XNMEVENTS -load ati.trapd
		$OV_XNMEVENTS -event
		;;
esac
}


#====================================================================
# Handle signals.  Remove temp files.
#
sig_handler()
{
	$rm -f ${OV_OID2SYM_FILE}.$$
	$rm -f ${OV_OID2VEN_FILE}.$$
	echo "Integration terminated."
	exit 1
}

#====================================================================
# Beginning of script.  
#

trap 'sig_handler'	1 2 3 15

setup_sys_cmds
test_for_root
test_for_ov
ov_integrate_query

echo "Shutting down OpenView ..."
${OV_DIR}/bin/ovstop

register_symbols
register_oid2sym
register_oid2vendor
register_enums
copy_icons
copy_arf
copy_mib
register_traps 

echo "Starting up OpenView/Netview/6000 ..."
${OV_DIR}/bin/ovstart ovwdb
${OV_DIR}/bin/ovstart ovtopmd
${OV_DIR}/bin/ovw -fields
${OV_DIR}/bin/ovtopofix -a
${OV_DIR}/bin/xnmsnmpconf -clearC
${OV_DIR}/bin/ovstart 


# All done... 

cat << EOM1

The ${APP_NAME} HP OpenView / IBM NetView/6000 integration files 
have been installed.


EOM1
