#!/bin/sh
#
# postpatch script for CR_Management component
#
####

fatal () {
    echo "$*" >&2
    exit 1
}

# Check for root
#
user=`/bin/id | /usr/bin/grep root | /usr/bin/wc -l`
if [ $user -eq 0 ]; then
    fatal "You must be root to run this script."
fi


# Setup the ESM environment if not running under esmpatch
#
if [ "$ESM_CONTEXT" != "patch" ]; then

    ESM_BASE=`/usr/bin/pkgparam SUNWstm ESM_BASE`
    if [ $? -ne 0 ]; then
        fatal "Could not determine ESM location!"
    fi

    . $ESM_BASE/lib/esm_env.sh
fi

# Import the requisite standard ESM libraries
#
. $ESM_LIB/esm_lib.ksh        &&
. $ESM_LIB/configvars_lib.ksh ||
    fatal "Failure to import the standard ESM libraries!"


# --------------------------------------------------------------------
# Update the Data Tier
#
# Include the standard Data Tier subcomponent library and ensure the
# Oracle client environment is properly established.  It is expected
# that the essential test of the Oracle client environment has already
# been successfully tested by the prepatch phase.
#
if [ -n "$LD_LIBRARY_PATH" ] ; then
    LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:$ORACLE_HOME/lib"
else
    LD_LIBRARY_PATH=$ORACLE_HOME/lib
fi
export LD_LIBRARY_PATH

_dtlibpath=$ESM_BASE/component/platform/bin/dt_lib.ksh

. $_dtlibpath ||
    fatal "Unable to import the CR data tier library ($_dtlibpath)!"

dt_set_env ||
    fatal "Unable to setup the CR data tier environment!"

el_slog 1 "Updating the database..."

for script in UIAssetList AllAssetsQueryHVB UIFactoryQueryDefinitions UISubReportQueryDefinitions
do
    spath=$DT_SCRIPTS/factorydata/$script.sql

    el_slog 1 "  Executing the $spath script..."

    $SQLPLUS -S $DBU_CREDENTIALS @$spath > $RETVAL 2>&1 < /dev/null
    if [ $? -ne 0 ] ; then
        el_log 1 "`$CAT $RETVAL`"
        el_assert "Failure to execute the '$script' SQL script against the database!"
    fi
done


# --------------------------------------------------------------------
# Update the Service Tier Container (IH)
#
ESM_IH_HOME=$ESM_PLATFORM/ih
TOMCAT_HOME=$ESM_IH_HOME/jakarta-tomcat-4.0.6-LE-jdk14
IHAPP=$ESM_IH_HOME/esmih.war
OLDIHAPP=$IHAPP.old.tar

# Prepare for a patched Container (IH) by tar'ing up and compressing the
# existing Tomcat app directory tree (using a special well-known filename),
# then completely removing the app directory tree in preparation for
# laying down the new, patched version.
#
if cd $TOMCAT_HOME/webapps/esmih &&
   /usr/bin/tar cf $OLDIHAPP .   &&
   /usr/bin/compress -f $OLDIHAPP
then
    cd ..
    /usr/bin/rm -rf esmih

    # Create the Container "esmih" Tomcat app directory and explode
    # the application war file into that directory.
    #
    if /usr/bin/mkdir esmih    &&
       cd esmih                &&
       /usr/bin/jar xf $IHAPP
    then
	:
    else
	fatal "Failure to install the new Container web application"
    fi
else
    fatal "Failure to preserve the existing esmih web application"
fi

# Clear out certain Container working files and directories
#
CRCRE_ROOT=$ESM_PLATFORM/container/cre

/usr/bin/rm -rf       $CRCRE_ROOT/persistence
/usr/bin/mkdir -m 755 $CRCRE_ROOT/persistence

/usr/bin/rm -rf       $CRCRE_ROOT/export/plain
/usr/bin/mkdir -m 755 $CRCRE_ROOT/export/plain

/usr/bin/rm -rf $CRCRE_ROOT/population/*.transient

/usr/bin/rm -rf $CRCRE_ROOT/components/*.xml


# --------------------------------------------------------------------
# Update the UI Lockart web application
#
PT_APPNAME='com.sun.netstorage.mgmt.ui.framework_1.0'
PT_APPDIR=$ESM_PLATFORM/smicc/frameworkweb
PT_APPCONFIG=$PT_APPDIR/WEB-INF/web.xml
PT_APPWAR=$ESM_PLATFORM/smicc/frameworkweb.war
OLDUIAPP=$PT_APPWAR.old.tar

# Prepare for a patched UI by unregistering the app in Lockhart,
# tar'ing up and compressing the existing exploded app directory
# tree (using a special well-known filename), then completely
# removing the app directory tree in preparation for laying down
# a new, patched version.
#
if /usr/sbin/smreg remove -a "$PT_APPNAME" ; then

    if cd $PT_APPDIR                &&
       /usr/bin/tar cf $OLDUIAPP .  &&
       /usr/bin/compress -f $OLDUIAPP
    then
	cd ..
	/usr/bin/rm -rf $PT_APPDIR

	# Create the Lockhart web app directory tree, explode the war file,
	# create the web app config file, then register the app with Lockhart.
	#
	if /usr/bin/mkdir $PT_APPDIR &&
	   cd $PT_APPDIR             &&
	   /usr/bin/jar xf $PT_APPWAR > /dev/null
	then
	    /usr/bin/mv $PT_APPCONFIG ${PT_APPCONFIG}.orig

	    /usr/bin/sed -e "s|%%ESM_CONFIG%%|$ESM_CONFIG|g"  \
			 -e "s|%%ESM_DPF_ETC%%|$ESM_CONFIG|g" \
			 ${PT_APPCONFIG}.orig > $PT_APPCONFIG
	    [ $? -eq 0 ] ||
		fatal "Failure to update the $PT_APPCONFIG file"

	    /usr/sbin/smreg add -a $PT_APPDIR ||
		fatal "Failure to register the Capacity Reporter smcwebserver application!"
	else
	    fatal "Unable to create the Capacity Reporter smcwebserver application!"
	fi
    else
	fatal "Failure to preserve the existing Capacity Reporter smcwebserver application"
    fi
else
    fatal "Unable to unregister the Capacity Reporter smcwebserver application!"
fi


# --------------------------------------------------------------------
# Start ESM and Lockhart if not running as part of esmpatch
#
if [ "$ESM_CONTEXT" != "patch" ]; then

    SWCSERVER=/usr/sbin/smcwebserver
    if [ -f "$SWCSERVER" ]; then
        $SWCSERVER start
    else
        fatal "Unable to start $SWCSERVER"
    fi

    $ESM_BASE/bin/esmcontrol start
fi

exit 0
