#!/bin/sh
# 
# This script installs a patch to HP Systems Insight Manager 5.0.
#

####################################
#
# 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_50="C.05.00.00.00"
VERSION_50_UPDATE_1="C.05.00.01.00"
PATCHED_VERSION_50="C.05.00.01.00"
PATCHED_BUILD_DATE="2005-12-08 12: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/')"
verString="$(echo $verString  | sed 's/^Systems Insight Manager \([^ ]*\).*$/\1/')"
#echo 2: verString=$verString


# if the version string indicates the CMS has all ready been patched
# then exit
if [ "$verString" = "$PATCHED_VERSION_50" ]
then
    echo "This HP Systems Insight Manager patch is already installed.  No changes being made."
    exit 400
fi

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

echo "HP Systems Insight Manager 5.0 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  ---------
# --------------------------------------------------
echo ""
echo "Copying updated files"
echo ""
cp -fr  $STAGING/bin            /opt/mx
cp -fr  $STAGING/config         /etc/opt/mx
cp -fr  $STAGING/doc            /opt/mx
cp -fr  $STAGING/hpwebadmin     /opt
cp -fr  $STAGING/jboss          /opt/mx
cp -fr  $STAGING/lbin           /opt/mx
cp -fr  $STAGING/lib            /opt/mx
cp -fr  $STAGING/mibs           /opt/mx
cp -fr  $STAGING/opensource     /var/opt/mx
cp -fr  $STAGING/share          /opt/mx
cp -fr  $STAGING/tools          /var/opt/mx

# --------------------------------------------------
# --------------------------------------------------
# Create link for new 'mxgetdbinfo' command
ln -f /opt/mx/lbin/.mxaux   /opt/mx/bin/mxgetdbinfo

# Update version before restarting
UpdateSIMVersion

# ------------------------------------
# ------ removeoldfiles   ---------
# ------------------------------------
/opt/mx/lbin/removeoldfiles

# ------------------------------------
# ------ mxinitconfig -a  ---------
# ------------------------------------
/opt/mx/bin/mxinitconfig -a
if [ $? -ne 0 ]
then
    echo "ERROR running 'mxinitconfig -a'.  See '/var/opt/mx/logs/initconfig.log'."
    exit 550
fi


# ----------------------------------------------------------
# ------ wait for HTTP server so CLIF commands can work ----
# ----------------------------------------------------------
while ( true )
do
    /opt/mx/bin/mxmib  > /dev/null 2>&1

    if [ $? -ne 255 ]        #  errorlevel = 255 (-1) if SOAP not responding yet
    then
        break                #  It responded, go on...
    fi
    echo "Waiting for Systems Insight Manager to complete initialization..."
    # --- delay 15 seconds to make sure HTTP server is started. ---
    $JAVA -server  -cp $MX_LIB_DIR/mx.jar  com.hp.mx.utilities.Delay  15
done

# --- delay 15 seconds to make really sure HTTP server is fully ready. ---
$JAVA -server  -cp $MX_LIB_DIR/mx.jar  com.hp.mx.utilities.Delay  15

# ------------------------------------
# ------ compile MIBs    ------
# ------------------------------------
for mib in cpqcr  cpqfca  cpqhlth  cpqida  cpqnic  cpqrpm  cpqscsi  cpqsm2  cpqstsys  svrclu
do
    echo ""
    echo "Compiling mib: $mib"
    /opt/mx/bin/mxmib -a  /opt/mx/mibs/$mib.cfg
    if [ $? -ne 0 ]
    then
        echo "Failed to compile mib: $mib"
        echo "Continuing installation."
    fi
done


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