#!/bin/sh
#
#ident "%W% %G% SMI"
#
# Copyright %G% Sun Microsystems, Inc. All Rights Reserved
#
#	prepremove script for package: SUNWmhs1

# Exit codes for installation scripts 
e_ok=0      
e_fatal=1      # stop installation on this exit
e_warning=2    # Installation will go on. 
e_int=3        # Interrupted. Stop installation
e_reboot=10    # User must reboot after installation of all selected packages
e_rebootnow=20 # User must reboot right after installation of current package
               # To be added to one of the single-digit exit code above
# Trap interrupt
trap `exit $e_int` 15

AWK=/usr/bin/awk
ECHO=/usr/bin/echo
GREP=/usr/bin/grep
PS=/usr/bin/ps
WC=/usr/bin/wc
CRONTAB=/usr/bin/crontab

#
#	Stop running process(es)
#
pid=`$PS -fe | $GREP lib/dswebd | $GREP -v grep | $AWK '{print $2}'`
if [ -n "$pid" ]
then     
  echo "ERROR: \"/etc/init.d/dsweb stop\" must be called before installing the patch"
  exit $e_int
fi  

pid=`$PS -fe | $GREP lib/dsservd | $GREP -v grep | $AWK '{print $2}'`
if [ -n "$pid" ]
then
  echo "ERROR: \"/etc/init.d/dsserv stop\" must be called before installing the patch"
  exit $e_int
fi  

if [ -f /etc/init.d/init.dsnmpserv ]
then
	/etc/init.d/init.dsnmpserv stop
fi

if [ -f /etc/init.d/dsadm ]
then
	/etc/init.d/dsadm stop
fi

if [ -f $BASEDIR/SUNWconn/ldap/sbin/init.dsnmprad ]
then
        $BASEDIR/SUNWconn/ldap/sbin/init.dsnmprad stop
fi

if [ -f $BASEDIR/SUNWconn/ldap/sbin/dsradius ]
then
        $BASEDIR/SUNWconn/ldap/sbin/dsradius stop
fi

#
# kill the dspulld process
#
pid=`$PS -fe | $GREP lib/dspulld | $GREP -v grep | $AWK '{print $2}'`
if [ -n "$pid" ]
then
  kill -TERM $pid
fi

pid=`$PS -fe | $GREP lib/dspushd | $GREP -v grep | $AWK '{print $2}'`
if [ -n "$pid" ]
then
  kill -TERM $pid
fi

#
# Kill the dsyppush command if needed
pid=`$PS -fe | $GREP dsyppush | $GREP -v grep | $AWK '{print $2}'`
if [ -n "$pid" ]
then
  kill -TERM $pid
fi

#
# kill snmp daemons
#
pid=`$PS -fe | $GREP lib/dsnmp | $GREP -v grep | $AWK '{print $2}'`
if [ -n "$pid" ]
then
  kill -TERM $pid
fi

#
# kill java apps
#
echo "\nTerminating java apps..."
pid=`$PS -fe | $GREP deja | $GREP -v grep | $AWK '{print $2}'`
if [ -n "$pid" ]
then
	pid2=`$PS -fe | $GREP "$pid" | $GREP jre | $GREP -v grep | $AWK '{print $2}'`
	if [ -n "$pid2" ]
	then
		kill -TERM $pid2
	else
		pid3=`$PS -fe | $GREP "$pid" | $GREP java | $GREP -v grep | $AWK '{print $2}'`
		if [ -n "$pid3" ]
		then
			kill -TERM $pid3
		fi 
	fi
fi

pid=`$PS -fe | $GREP dsadmintool | $GREP -v grep | $AWK '{print $2}'`
if [ -n "$pid" ]
then
        pid2=`$PS -fe | $GREP "$pid" | $GREP jre | $GREP -v grep | $AWK '{print $2}'`
        if [ -n "$pid2" ]
        then
                kill -TERM $pid2
        else
                pid3=`$PS -fe | $GREP "$pid" | $GREP java | $GREP -v grep | $AWK '{print $2}'`
                if [ -n "$pid3" ]
                then
                        kill -TERM $pid3
                fi 
        fi
fi

#
# remove the $BASEDIR/SUNWconn/html/lib/sunds_port.html file
# dynamically created by the adm server
#
rm -f $BASEDIR/SUNWconn/html/lib/sunds_port.html
#
#
exit $e_ok 
