#!/bin/ksh 

# Copyright 2002-2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident  "@(#)postbackout 1.13     05/03/17 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-patch02
rm -f log_template_fr.post-patch02
rm -f log_template_ja.post-patch02
rm -f log_template_zh_CN.post-patch02

if [ -f log_template.pre-patch02 ]; then
    mv log_template.pre-patch02 log_template
fi

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

cd /opt/se6x20
rm -f data.post-patch02
if [ -f data.pre-patch02 ]; then
    mv data.pre-patch02 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

    mofout=`${wbembase}/wsi/bin/mofreg -u ${pkgtag} 2>&1`
    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}/wsi/server/jws/logr/unregDir/${pkgtag} ] 
        do
	    sleep 5
        done
	sleep 15
	wbempid=`/opt/se6x20/bin/getWbemPid 2>/dev/null`
    fi

    return 0
}

# 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

mof_unreg_helper() {
    pkg_tag=$1 
    if [ -d ${WBEMBASE}/wsi/server/jws/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}/wsi/server/jws/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
    fi
}


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

# 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}

#### undo the mofs for SUNWsedspp
PKG_NAME=SUNWsedspp
PKGPATCH_NAME=PATCH15

# sedspp_m (modify)
PKG_TAG=${PKG_NAME}_${PKGPATCH_NAME}_m
mof_unreg_helper ${PKG_TAG}

# sedspp_c (create)
PKG_TAG=${PKG_NAME}_${PKGPATCH_NAME}_c
mof_unreg_helper ${PKG_TAG}


#### undo the mofs for SUNWseput
PKG_NAME=SUNWseput

# seput_m (modify)
PKG_TAG=${PKG_NAME}_${PKGPATCH_NAME}_m
mof_unreg_helper ${PKG_TAG}

# seput_c (create)
PKG_TAG=${PKG_NAME}_${PKGPATCH_NAME}_c
mof_unreg_helper ${PKG_TAG}

#
# 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"

exit 0
