#!/bin/ksh

# Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident  "@(#)postbackout 1.8     03/09/22 SMI"


##############################################################
#
# STEP 1: Backout log template changes 
# This is equivalent to a preremove for SUNWseput
#
##############################################################

##############################################################
#
# STEP 2: Backout log template log_template changes
#
##############################################################

cd /opt/se6x20/resources

rm -f log_subscriptions.properties
rm -f log_template.post-patch04
rm -f log_template_fr.post-patch04
rm -f log_template_ja.post-patch04
rm -f log_template_zh_CN.post-patch04

mv log_template.pre-patch04 log_template

if [ -f log_template_fr.pre-patch04 ]; then
    mv log_template_fr.pre-patch04 log_template_fr
fi
if [ -f log_template_ja.pre-patch04 ]; then
    mv log_template_ja.pre-patch04 log_template_ja
fi
if [ -f log_template_zh_CN.pre-patch04 ]; then
    mv log_template_zh_CN.pre-patch04 log_template_zh_CN
fi

cd /opt/se6x20
rm -f data.post-patch04
if [ -f data.pre-patch04 ]; then
    mv data.pre-patch04 data
fi

##############################################################
#
# STEP 3: Backout WBEM changes
# This is the equivalent of the 'preremove' for the SUNWsedap 
# and SUNWsem12p packages
#
##############################################################

WBEM_PORT=5988

#  Function to perform the mof registration.
#
#  Input Arguments:	$1	TAG for registration of the MOF passed
#			$2	WBEM relocation basedir
#			$3	pid of WBEM services (null/empty if not running)
#
mof_unreg () {

    pkgtag=$1
    wbembase=$2
    wbempid=$3

    if [ "${wbembase}" = "/" ]
    then
      mofout=`/usr/sadm/bin/mofreg -u ${pkgtag} 2>&1`
    else
      mofout=`/opt/se6x20/bin/mofreg -u ${pkgtag} 2>&1`
    fi
    retstat=$?
    if [ $retstat -ne 0 ] ; then
	echo "$0: ERROR: mofreg -u ${pkgtag}; status: $retstat" >&2
	echo "$mofout" | sed -e 's/.*EXITVALUE/EXITVALUE/' >&2
	exit 1
    fi

# A restart will process the unregister request (in 5.9, not yet 
# sure about 5.8)

    if [ -z "$wbempid" ] ; then
	/opt/se6x20/bin/restart_se6000

        while [ -d ${wbembase}/var/sadm/wbem/logr/unregDir/${pkgtag} ] 
        do
	    sleep 5
        done
	sleep 15
	wbempid=`/opt/se6x20/bin/getWbemPid 2>/dev/null`
    fi

    return 0
}


#------------------------------------------------------------------------
#  PKG_NAME holds the name of the package for the current package
#------------------------------------------------------------------------

#
# Figure out which package we need to postprocess in the backout.
# It will either by SUNWsem12p or SUNWsedap, depending on what
# is in the tmp file directory after an install.
#

if pkginfo SUNWsedap 1>/dev/null 2>&1
then
    PKG_NAME=SUNWsedap
else
    PKG_NAME=SUNWsem12p
fi

# Get the base directory for Device Management - RICK COMMENT OUT
BASEDIR=`pkgparam $RELOCATE SUNWdm BASEDIR 2>/dev/null`
PARENTDIR=`pkgparam $RELOCATE SUNWdm PARENTDIR 2>/dev/null`
BASEDIR=${BASEDIR}/${PARENTDIR}
PKGPATCH_NAME=PATCH04
PKG_TAG=${PKG_NAME}_${PKGPATCH_NAME}

# Get the WBEM base installation directory...
WBEMBASE=`/opt/se6x20/bin/getWbemBase 2>/dev/null`
if [ $? -ne 0 ]
then
    echo "$0: ERROR: Cannot determine WBEM base directory." >&2
    exit 1
fi

REUSE=${REUSELOGR:-'false'}


SOLARIS=`uname -r`

    
if [ -d ${WBEMBASE}/var/sadm/wbem/logr/unregDir/${PKG_TAG} ]
then

    echo "\nUnRegistering the ${PKG_TAG}.mof file ..."
    mofout=`mof_unreg ${PKG_TAG} ${WBEMBASE} $pid`
    if [ $? -ne 0 ] ; then
        echo "$0: WARNING: Failed while unregistering the ${PKG_TAG}.mof file." >&2
        echo "$mofout" | sed -e 's/.*EXITVALUE/EXITVALUE/' >&2
    fi

    if [ -d ${WBEMBASE}/var/sadm/wbem/logr/failDir/${PKG_TAG} ] 
    then
        echo "$0: WARNING: Failed while unregistering the ${PKG_TAG}.mof file." >&2
        echo "File exists under wbem failDir directory." >&2
    fi


#
# Restart the daemon processes after patching
#

    echo "Patch : Restart the daemon processes after patching..."
    /opt/se6x20/bin/restart_se6000
    rc=$?
    echo "Patch : Restart completed. Exit code = $rc"
fi
exit 0
