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

####################################
#
# file and directory locations
#
####################################
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"

DCDATA_FILE="${MX_CONFIG_DIR}/datacol/dcdata.dat"
SAVED_DCDATA_FILE="${MX_CONFIG_DIR}/datacol/dcdata.sav"
NEW_DCDATA_FILE="dcdata.dat"

MXJAR_FILE="${MX_LIB_DIR}/mx.jar"
SAVED_MXJAR_FILE="${MX_LIB_DIR}/mx.sav"
NEW_MXJAR_FILE="mx.jar"

PGJAR_FILE="${MX_LIB_DIR}/pg73jdbc3.jar"
SAVED_PGJAR_FILE="${MX_LIB_DIR}/pg73jdbc3.sav"
NEW_PGJAR_FILE="pg74.215.jdbc3.jar"

TOOLS_FILE="${MX_TOOLS_DIR}/managementurl.xml"
TOOLS_FILE_2="${MX_SETUP_DIR}/managementurl.xml"
SAVED_TOOLS_FILE="${MX_TOOLS_DIR}/managementurl.sav"
NEW_TOOLS_FILE="managementurl.xml"

####################################
#
# version strings for matching
#
####################################
VERSION_42="C.04.02.00.00"
VERSION_42_REBUILD="C.04.02.01.00"
PATCHED_VERSION_42="C.04.02.00.01"


#############################################################
#
# 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: UpdateSIM42Version()
#
# Checks to see if HP SIM is running
#
# Returns 0 if HP SIM is running and 1 if it is not
#
#############################################################

UpdateSIM42Version()
{
    mv $MXVERSION_PROPS $SAVED_MXVERSION_PROPS
    simExternalName=`grep "SimExternalName" $SAVED_MXVERSION_PROPS`
    newSimExternalName="$(echo $simExternalName | sed 's/\(^.*4.2\)\(.*$\)/\1 Update 1 \2/')"

    simInternalRevision=`grep "SimInternalRevision" $SAVED_MXVERSION_PROPS`
    newSimInternalRevision="$(echo $simInternalRevision | sed "s/\(^.*=\).*$/\1$PATCHED_VERSION_42/")"

    simBuildDate=`grep "SimBuildDate" $SAVED_MXVERSION_PROPS`
    echo $newSimExternalName > $MXVERSION_PROPS
    echo $newSimInternalRevision >> $MXVERSION_PROPS
    echo $simBuildDate >> $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/^.*- HP-UX \([^ ]*\).*$/\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_42" ]
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_42" -a "$verString" != "$VERSION_42_REBUILD" ]
then 
    echo "HP Systems Insight Manager 4.2 not found.  No changes being made."
    exit 500
fi

echo "HP Systems Insight Manager 4.2 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


# ------------------------------------
# ------ mx.jar              ---------
# ------------------------------------
# Move the existing file to a saved file, and copy in new version.
if [ -e $MXJAR_FILE ] 
then
    mv -f $MXJAR_FILE $SAVED_MXJAR_FILE
fi
cp -f $NEW_MXJAR_FILE $MXJAR_FILE

# ------------------------------------
# ------ managementurl.xml   ---------
# ------------------------------------
# Move the existing file to a saved file, and copy in new version.
if [ -e $TOOLS_FILE ] 
then
    mv -f $TOOLS_FILE $SAVED_TOOLS_FILE
fi
cp -f $NEW_TOOLS_FILE $TOOLS_FILE
cp -f $NEW_TOOLS_FILE $TOOLS_FILE_2

# ------------------------------------
# ------ pg74.215.jdbc3.jar  ---------
# ------------------------------------
# Move the existing file to a saved file, and copy in new version.
if [ -e $PGJAR_FILE ] 
then
    mv -f $PGJAR_FILE $SAVED_PGJAR_FILE
fi
cp -f $NEW_PGJAR_FILE  ${MX_LIB_DIR}/$NEW_PGJAR_FILE

# ------------------------------------
# ------ dcdata.dat          ---------
# ------------------------------------
# Move the existing file to a saved file, and copy in new version.
if [ -e $DCDATA_FILE ] 
then
    mv -f $DCDATA_FILE $SAVED_DCDATA_FILE
fi
cp -f $NEW_DCDATA_FILE $DCDATA_FILE

# ------------------------------------
# ------ init from dcdata.dat   ------
# ------------------------------------
$JAVA -server -DMX_BASE_DIR="" -Djava.library.path=$MX_LIB_DIR  -cp $MX_LIB_DIR/mx.jar:$MX_LIB_DIR/jdom.jar:$MX_LIB_DIR/pg74.215.jdbc3.jar  com.hp.mx.devtool.dbinit.RegisterDataCollectionIds
if [ $? -ne 0 ]
then
    echo "Failed to initialize data collection changes."
    exit 550
fi
# Update version to mark complete
UpdateSIM42Version


# 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
