#!/bin/sh
# 
# This script adds a language kit to HP Systems Insight Manager 5.1.
#

####################################
#
# file and directory locations
#
####################################
STAGING="SIM"
SIM_BIN="/opt/mx/bin"
MXVERSION="${SIM_BIN}/mxversion"
MX_CONFIG_DIR="/etc/opt/mx/config"
MX_LIB_DIR="/opt/mx/lib"
MX_TOOLS_DIR="/var/opt/mx/tools"
MX_SETUP_DIR="/var/opt/mx/setup"
MXVERSION_PROPS="${MX_CONFIG_DIR}/MxVersion.props"
SAVED_MXVERSION_PROPS="${MX_CONFIG_DIR}/MxVersion.sav"
MXTOOL="${SIM_BIN}/mxtool -l f -t BogusToolName"
MXSTOP="${SIM_BIN}/mxstop"
MXSTART="${SIM_BIN}/mxstart"
HPWEBADMIN="/opt/hpwebadmin"
JAVA="/opt/mx/j2re/bin/java"

####################################
#
# version strings for matching
#
####################################
VERSION_51="C.05.01.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 > /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
}

#############################################################
#
# Function: UpdateSIMVersion()
#
# Updates the SIM version file (/etc/opt/mx/config/MxVersion.props)
#
#############################################################

UpdateSIMVersion()
{
    cp -f  $STAGING/config/MxVersion.new         /etc/opt/mx/config/MxVersion.props
}


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

# 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 being made."
    exit 100
fi

# Check if the "mxversion" utility can be found
if [ ! -e "${MXVERSION}" ]
then
    echo "HP Systems Insight Manager not found.  No changes being 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 being made."
    exit 300
fi

# extract the version string from the "mxversion" output
#echo 1: simVersion=$simVersion
verString="$(echo $simVersion | sed 's/^.*- Linux \([^ ]*\).*$/\1/')"
# only 1st 13 characters of version (C.05.01.00.00)
verString="$(echo $verString  | sed 's/^.*- HP-UX \([^ ]\{13\}\).*$/\1/')"
verString="$(echo $verString  | sed 's/^Systems Insight Manager \([^ ]*\).*$/\1/')"
#echo 2: verString=$verString


# Check to see if we can recognize the version of SIM
if [ "$verString" != "$VERSION_51" ]
then 
    echo "HP Systems Insight Manager 5.1 not found.  No changes being made."
    exit 500
fi

echo "HP Systems Insight Manager 5.1 found."

# 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 "Stopping the HP Systems Insight Manager daemons."
    $MXSTOP  > /dev/null 2>&1

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

# Delay 30 seconds
$JAVA -server  -cp $MX_LIB_DIR/mx.jar  com.hp.mx.utilities.Delay  30


# --------------------------------------------------
# ------ Copy new files over installation  ---------
# --------------------------------------------------
cp -f  HOTFIX51_009.jar  /opt/mx/patch/HOTFIX51_009.jar
cp -f  MxHelpMapper.jsp  /opt/hpwebadmin/webapps/mxhelp/mxportal/MxHelpMapper.jsp
rm -f  /opt/mx/jboss/server/hpsim/work/jboss.web/localhost/mxhelp/org/apache/jsp/mxportal/MxHelpMapper_jsp.class

echo ""
echo "Running 'simtranslate'..." 
echo ""
chmod u+x simtranslate
./simtranslate stage  ko


# Restart HP SIM if needed
if [ "$wasSIMStarted" = "TRUE" ]
then
    echo "Restarting the HP Systems Insight Manager daemons."
    $MXSTART > /dev/null 2>&1
fi


echo "HP Systems Insight Manager successfully updated."
exit 0
