#!/bin/sh
#
# This script file updates Japanese help files of 
# HP Systems Insight Manager 5.0.

PRODNAME="HP Systems Insight Manager 5.0 Japanese help files"
HPSIMNAME="HP Systems Insight Manager 5.0"

####################################
#
# file and directory locations
#
####################################
SIM_BIN="/opt/mx/bin"
MXVERSION="${SIM_BIN}/mxversion"
MXTOOL=${SIM_BIN}/mxtool
MXTOOLOPT="-l f -t BogusToolName"
MXSTOP="${SIM_BIN}/mxstop"
MXSTART="${SIM_BIN}/mxstart"
SLEEP=/bin/sleep
TAR="/bin/tar -xzf"
EXPR=expr
RM="/bin/rm"
CP="/bin/cp"
TARFILE=hpsim50_ja_help.tgz

####################################
#
# version strings for matching
#
####################################
VERSION_50="C.05.00.00.00"

#############################################################
#
# 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 Systems Insight Manager 5.0
echo 
echo This script file updates Japanese help files.
echo The installation will stop and restart
echo ${HPSIMNAME} if it is currently 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" != "$VERSION_50" ]
then 
	echo "${HPSIMNAME}, not found.  No changes made."
	exit 400
fi

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

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

# 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

# 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 Japanese help file installation completed.
echo

exit $x
