#!/bin/sh
#
# This script installs the HP BladeSystem Integrated Management Environment 1.1
# into HP Systems Insight Manager 4.2, SP1 or SP2.

PRODNAME="HP BladeSystem Integrated Management Environment 1.1"
HPSIMNAME="HP Systems Insight Manager 4.2"

####################################
#
# file and directory locations
#
####################################
SIM_BIN="/opt/mx/bin"
SIM_TOOLS="/var/opt/mx/tools"
MXVERSION="${SIM_BIN}/mxversion"
MXTOOL=${SIM_BIN}/mxtool
MXTOOLOPT="-l f -t BogusToolName"
MXSTOP="${SIM_BIN}/mxstop"
MXSTM="${SIM_BIN}/mxstm"
MXSTART="${SIM_BIN}/mxstart"
SLEEP=/bin/sleep
TAR="/bin/tar -xzf"
EXPR=expr
RM="/bin/rm"
CP="/bin/cp"
COMPILEDJSP="/opt/hpwebadmin/work/_/localhost/_/mxportal/bladeime"
CGESMXML="${SIM_BIN}/jamocha.xml"
BLADEIMEXML="${SIM_TOOLS}/bladeimeVersion.xml"
TARFILE=montero11.tgz

####################################
#
# version strings for matching
#
####################################
VERSION_42="C.04.02.00.00"
VERSION_42_REBUILD="C.04.02.01.00"
PATCH1_VERSION_42="C.04.02.00.01"
PATCH2_VERSION_42="C.04.02.00.02"
VERSION_42_ALL="C.04.02"

#############################################################
#
# Function: GetSIMStatus()
#
# Checks to see if HP SIM is running
#
# Returns 0 if HP SIM is running and 1 if it is not
#
#############################################################

GetSIMStatus()
{
    $MXTOOL $MXTOOLOPT > /dev/null 2>&1

    # if return value of 200, we know that HP SIM is not started
    if [ $? = 200 ]
    then
        return 1
    fi

    return 0
}

#############################################################
#
# Main: start of the main script execution
#
#############################################################

echo
echo
echo ====================================================
echo HP BladeSystem Integrated Management Environment 1.1
echo 
echo This product is an enhanced management plug-in for
echo ${HPSIMNAME}, Service Pack 1.
echo The installation will stop and restart
echo ${HPSIMNAME} if it is currently
echo running.
echo 
echo Enter "'y'" to continue this installation.
echo ====================================================

read x
if [ "$x" != 'y' -a "$x" != 'Y' ]; then
	echo "Installation was not chosen.  No changes made."
	exit 50
fi

echo 

# Check to see if the user is the root user
user=`whoami`
if [ "$user" != "root" ] 
then
    echo "This patch must be run by the root user.  No changes made."
    exit 100
fi

# Check if the "mxversion" utility can be found
if [ ! -e "${MXVERSION}" ]
then
    echo "${HPSIMNAME} not found.  No changes made."
    exit 200
fi

# execute "mxversion" and store the result
simVersion=`$MXVERSION`

# if "mxversion" failed to execute properly then exit
if [ $? -ne 0 ]
then
    echo "Failed to execute $MXVERSION .  No changes made."
    exit 300
fi

# extract the version string from the "mxversion" output
verString="$(echo $simVersion | sed 's/^.*- Linux \([^ ]*\).*$/\1/')"
verString="$(echo $verString | sed 's/^.*- HP-UX \([^ ]*\).*$/\1/')"
verString="$(echo $verString | sed 's/^Systems Insight Manager \([^ ]*\).*$/\1/')"
# this strips off the last 2 fields
# verString="$(echo $verString | sed 's/.[^.]*.[^.]*$//')"

# Check to see if we can recognize the version of SIM
if [ "$verString" != "$PATCH1_VERSION_42" -a "$verString" != "$PATCH2_VERSION_42" ]
then 
	echo "${HPSIMNAME}, SP1 or SP2 not found.  No changes made."
	echo ${HPSIMNAME}, Service Pack 1 or 2 must be installed
	echo before $PRODNAME is installed.
	exit 400
fi

echo
echo "${HPSIMNAME}, $verString found."

if [ ! -f $TARFILE ]; then
	echo Tar file, $TARGTILE missing. No changes made.
	exit 500
fi

mkdir temp
$TAR $TARFILE -C temp > /dev/null 2>&1
if [ $? -ne 0 ]; then
	echo Unable to extract tar file. No changes made.
        rm -rf temp
	exit 600
fi

echo
$MXSTM -r -f ./temp$CGESMXML > /dev/null 2>&1
$MXSTM -a -f ./temp$CGESMXML
if [ $? -ne 0 ]; then
	echo Unable to add CGESM rule.  No changes made.
        rm -rf temp
	exit 700
fi
$MXTOOL -r -f ./temp$BLADEIMEXML > /dev/null 2>&1
$MXTOOL -a -f ./temp$BLADEIMEXML
if [ $? -ne 0 ]; then
	echo Unable to add Blade IME rule.  No changes made.
        rm -rf temp
	exit 800
fi
rm -rf temp

# Check to see if HP SIM was started
wasSIMStarted="FALSE"
GetSIMStatus

# if HP SIM was running then stop it
if [ $? = 0 ]
then
	# Stop the HP SIM services
	echo
	echo "Stopping the ${HPSIMNAME} daemons..."
	$MXSTOP  #> /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo Unable to stop ${HPSIMNAME} daemons.  No changes made.
		exit 900
	fi

	# Remember that HP SIM was running so we can restart it at the end
	wasSIMStarted="TRUE"
fi

# Delay 30 seconds
x=0
while [ $x -lt 10 ]; do
	echo -n .
	$SLEEP 3
	x=`$EXPR $x + 1`
done
echo

# install new files
x=`pwd`
cd /
$TAR ${x}/$TARFILE > /dev/null 2>&1
if [ $? -ne 0 ]; then
        echo Unable to extract tar file. No changes made.
        rm -rf temp
        exit 1000 
fi

# remove the compiled JSP files so they are rebuilt
$RM -rf ${COMPILEDJSP}/*

# Restart HP SIM if needed
x=0
if [ "$wasSIMStarted" = "TRUE" ]
then
	echo
	echo "Restarting the ${HPSIMNAME} daemons..."
	$MXSTART #> /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo Unable to start ${HPSIMNAME} daemons.
		x=1100
	fi
fi

echo
echo $PRODNAME installation completed.
echo

exit $x
