#!/bin/sh
#
# prepatch 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

# Stop ESM if not running as part of 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/bin/esmcontrol stop 

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

# Ensure ORACLE_HOME is set (and valid) for use during the postpatch phase,
# and that the two critical Oracle client utilities execute properly.
#
[ -n "$ORACLE_HOME" ] ||
    fatal "The environment variable ORACLE_HOME is not set."

_ck_cmd=$ORACLE_HOME/bin/tnsping

[ -x "$_ck_cmd" ] || fatal "Program $_ck_cmd is not executable.\n"\
                           "Perhaps ORACLE_HOME is not set properly?"

# ORACLE_HOME appears to be set properly, so now ensure
# that LD_LIBRARY_PATH is set correctly by running one
# of the critical Oracle utilities to see if it loads.
#
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

_ck_cmd=$ORACLE_HOME/bin/sqlplus

$_ck_cmd >/dev/null 2>&1 </dev/null ||
    fatal "Program $_ck_cmd does not execute properly.\n"\
    "Perhaps LD_LIBRARY_PATH is not set properly for Oracle?"

exit 0
