LOGDIR=/var/sadm/patch/115861-05
LOG=$LOGDIR/postbackout.log

# check for root
user=`/bin/id | /usr/bin/grep root | /usr/bin/wc -l`
if [ $user -eq 0 ]; then
	msg="You must be root to execute this script."
	echo "$msg"
        exit 1
fi

/bin/mkdir -p $LOGDIR

ESM_BASE=`/usr/bin/pkgparam SUNWstm ESM_BASE`
if [ $? -ne 0 ]; then
    msg="Could not determine ESM location."
    echo "$msg"
    echo "$msg" >> $LOG
    exit 1
fi

SM_BASE=/usr/share/webconsole
US_BASE=/usr/sbin
SM_ETC=/etc/opt/webconsole



# helper function to restart database (for making db changes)
start_db () {
    /usr/bin/su - $SSTR_DBOWNER -c ". $SSTR_DBBIN/postgres.env; $SSTR_DBSTART" >> $LOG 2>&1
    this_try=1
    until /usr/bin/su $SSTR_DBOWNER  -c ". $SSTR_DBBIN/postgres.env; $SSTR_PSQL -l" > /dev/null 2>&1
    do
        if [ $this_try -ge 12 ]; then
	    msg="Database still not up - giving up"
            echo "$msg"
	    echo "$msg" >> $LOG
            exit 1
        fi
        echo "Wait and try connecting to database again..." >> $LOG
        this_try=`/usr/bin/expr $this_try + 1`
        sleep 10
    done
}


# UI changes
# Copy the old esm.war file back to lockhart webconsole directory
/bin/cp $ESM_BASE/sssm/lib/esm.war $SM_BASE/esm >> $LOG 2>&1

# there ought to be a java install (lockhart needs it), so use that
# (something very wrong if not there)
JAVA_HOME=`/usr/bin/grep java.home $SM_ETC/webconsole | /bin/sed -e 's/java.home=//'`
JAR=$JAVA_HOME/bin/jar
if [ ! -x $JAR ] ; then
    if [ ! -x $JAR ] ; then
	msg="Could not find jar tool."
        echo "$msg"
	echo "$msg" >> $LOG
	exit 1
    fi
fi

# Expand the esm.war file in $SM_BASE/esm
cd $SM_BASE/esm
$JAR -xf esm.war >> $LOG 2>&1

# copy old component files over there too (see tr_mgmt/bin/postinstall)
/bin/cp $ESM_BASE/sssm/util/cre/components/esm-jade.car $SM_BASE/esm/WEB-INF/lib/esm-jade.jar >> $LOG 2>&1
/bin/cp $ESM_BASE/sssm/util/cre/components/idresolver.car $SM_BASE/esm/WEB-INF/lib/idresolver.jar >> $LOG 2>&1
/bin/cp $ESM_BASE/sssm/util/cre/components/services-api.jar $SM_BASE/esm/WEB-INF/lib >> $LOG 2>&1
/bin/cp $ESM_BASE/sssm/util/cre/components/serack.jar $SM_BASE/esm/WEB-INF/lib >> $LOG 2>&1
/bin/cp $ESM_BASE/sssm/util/cre/components/t3.jar $SM_BASE/esm/WEB-INF/lib >> $LOG 2>&1
/bin/cp $ESM_BASE/sssm/lib/esm-topology.jar $SM_BASE/esm/WEB-INF/lib >> $LOG 2>&1
/bin/cp $ESM_BASE/sssm/util/cre/components/switch.jar $SM_BASE/esm/WEB-INF/lib >> $LOG 2>&1
/bin/cp $ESM_BASE/sssm/util/cre/components/hds.jar $SM_BASE/esm/WEB-INF/lib >> $LOG 2>&1
/bin/cp $ESM_BASE/sssm/util/cre/components/hba.jar $SM_BASE/esm/WEB-INF/lib >> $LOG 2>&1
/bin/cp $ESM_BASE/sssm/util/cre/components/oob_util.jar $SM_BASE/esm/WEB-INF/lib >> $LOG 2>&1
/bin/cp $ESM_BASE/sssm/util/cre/components/esm-common.jar $SM_BASE/esm/WEB-INF/lib >> $LOG 2>&1
/bin/cp $ESM_BASE/sssm/util/cre/components/asset.jar $SM_BASE/esm/WEB-INF/lib >> $LOG 2>&1
/bin/cp $ESM_BASE/sssm/util/cre/components/discovery-impl.jar $SM_BASE/esm/WEB-INF/lib >> $LOG 2>&1

# Change file permissions
cd $SM_BASE/esm
/bin/chmod -R 755 . >> $LOG 2>&1

# re-register esm app (otherwise topo page won't display)
SMREG=$US_BASE/smreg
if [ -f "$SMREG" ]; then
    $SMREG add -a /usr/share/webconsole/esm >> $LOG 2>&1
fi


# These are changes made by the -01 rev postbackout.  If earlier versions
# of the patch were installed before this patch, then don't back these
# changes out (because they are still needed by the previous versions.  If no
# old versions were installed, go ahead and make these backout changes

oldpatch=0
/usr/bin/showrev -p | /usr/bin/grep "Patch: 115861" | /usr/bin/grep -v "115861-05" > /dev/null 2>&1
if [ $? -eq 0 ]; then
    oldpatch=1
fi

if [ $oldpatch -ne 1 ]; then
    # restore old server.xml file
    if [ -f $SM_ETC/.server.xml.bak ]; then
        /usr/bin/mv -f $SM_ETC/.server.xml.bak $SM_ETC/server.xml >> $LOG 2>&1
    fi

    # restore old db table content
    SSTR_HOME=$ESM_BASE/sssm
    SSTR_DBHOME=$SSTR_HOME/util/pgsql
    SSTR_DBBIN=$SSTR_DBHOME/nsm1/bin
    SSTR_DBSTART=$SSTR_DBBIN/dbstart.sh
    SSTR_DBOWNER=sstr001
    SSTR_PSQL=$SSTR_DBHOME/bin/psql
    start_db

    /usr/bin/su - $SSTR_DBOWNER -c ". $SSTR_DBBIN/postgres.env; $SSTR_PSQL -f $SSTR_DBHOME/nsm1/ddl/LocalizePropsPopulate.ddl" >> $LOG 2>&1
fi


# got a new transient.xml.default for 4970744, so need to regenerate the
# original transient.xml file
. $ESM_BASE/lib/configvars_lib.ksh
. $ESM_BASE/component/tr_mgmt/bin/component_lib.ksh
cl_update_rmi_registry_port >> $LOG 2>&1


# restart smicc and ESM if not adding patches with esmpatch
if [ "$ESM_CONTEXT" != "patch" ]; then
    SMSERVER=$US_BASE/smcwebserver
    if [ -f "$SMSERVER" ]; then
        $SMSERVER start >> $LOG 2>&1
    fi

    $ESM_BASE/bin/esmcontrol -m start >> $LOG 2>&1
fi
