#!/bin/sh
#############################################################################
# Copyright (C)2005, Sun Microsystems Inc. All rights reserved.
# Use is subject to license terms.
#
# Do any cleanup required prior to removing the SCNBase user components.
#
# 1. check PKG_INSTALL_ROOT. If it is set, the package being removed has
#    no components in use on the system: exit immediately.
# 2. otherwise, unregister the cleanup-scn-base script with the flash archive
#    and sysidconfig subsystems.
# 3. and, if the Cacao container is running, undeploy the SCNBase Module.
#
# pragma ident $Id: preremove,v 1.4 2005/10/14 16:22:01 rc5273 Exp $
#############################################################################

if [ ! -z "$PKG_INSTALL_ROOT" ] ; then 
    exit 0
fi

###
## Remove the sym-link to the client_cleanup script in 
## /etc/flash/postdeployment.
###
flashdir="/etc/flash/postdeployment"
if [ -f "${flashdir}/cleanup-scn-base" ]
then
    /usr/bin/rm "${flashdir}/cleanup-scn-base"
fi

###
## Run sysidconfig with the '-r' option to get the client_cleanup script 
## removed from the list of defined applications.
###
/usr/sbin/sysidconfig -r /usr/lib/scn/bin/cleanup-scn-base > /dev/null 2>&1

###
## now do Cacao related cleanup
###
MODULENAME=com.sun.scn.base.SCNBase
MODULEFILE=${MODULENAME}.xml

# Get the base directory for Cacao packages
CACAOBASE=`pkgparam SUNWcacaort BASEDIR`
CACAOBIN=${CACAOBASE}usr/lib/cacao/bin

# see if cacao is currently running
PIDS=`/usr/bin/pgrep -u 0 -f -- "${CACAOBASE}usr/lib/cacao/lib/tools/launch" `
if [ $? -ne 0 ]; then
    # not running, so there's nothing to do
    exit 0
fi 

# search for the SCNBase module in cacao's list of deployed modules
MODTEST=`$CACAOBIN/cacaoadm list-modules | /usr/bin/grep $MODULENAME`

# if it was found, undeploy it.
if [ "$MODTEST" != "" ] ; then
    echo ""
    echo "Undeploying $MODULENAME"
    ${CACAOBIN}/cacaoadm undeploy $MODULENAME
    if [ $? -ne 0 ] ; then
	echo ""
	echo "WARNING !!!: Cacao could not undeploy the $MODULENAME module"
	echo ""
	echo "Undeploy the module as follows:"
	echo "	$CACAOBIN/cacaoadm undeploy $MODULENAME"
    fi
fi

exit 0
