#!/sbin/sh
#------------------------------------------------------------------#
#
# HPUX_ID: @(#) $Revision: 5.1 $ generated on Mon Mar  9 14:16:59 EST 1998
#
# (c)Copyright 1983-1996 Hewlett-Packard Co.,  All Rights Reserved.
#
#------------------------------------------------------------------#
# SD postremove script template for use with all patches.          #
#------------------------------------------------------------------#
# This file is optional for all patches.  The purpose of this      #
# script is to perform patch installation actions that cannot be   #
# accomplished by simple unconditional file extraction from the    #
# software source media.                                           #
#------------------------------------------------------------------#

_PATCHID=PHCO_23966

UTILS="/usr/lbin/sw/control_utils"

if [ ! -f $UTILS ]; then
    echo "ERROR:   Cannot find $UTILS"
    exit 1
fi

. $UTILS
exitval=$SUCCESS

#####
##### Insert any code you may require right here.
##### Pay attention to the exitval variable and set appropriately
##### in your code.  If you are unsure as to what types of operations
##### are legal in this file and what types aren't, consult the 
##### "Guidelines for SD Control Scripts" document.
#####
##### START_CUSTOMIZATION_HERE

########################################################################
# FUNCTIONS
########################################################################
# Swagentd_shutdown
#
# Shut down the swagentd daemon in an orderly manner.  It is not the 
# simple matter of shutting down other daemons, since swagentd is the
# parent process of this configure script.  Other processes might be
# using the daemon at this time, and they must be allowed to finish so
# the daemon can be killed.  Fortunately, the most likely processes are
# swacl, swlist, and swdepot, which finish quickly.
#
# Note that this should be safe at system boot time, since this script
# is not run if this fileset is already in the CONFIGURED state.
#
Swagentd_shutdown()
{
    kill_named_procs "swagentd"		# Deny new accesses.
    sleep 3				# Allow current agents to finish.
    kill_named_procs $pid "SIGKILL"	# Kill the daemon RIGHT NOW.
    retval=$?

    if [[ $retval -eq $SUCCESS ]]
    then
        retval=$FAILURE
        typeset -i count=100
        while [[ $count -gt 0 ]]
        do
           _pid=$(ps -e |
           while read _PID _TTY _TIME _CMD
           do
               [[ $_CMD = swagentd ]] && echo $_PID
           done)

           if [[ -z $_pid ]]
           then
               retval=$SUCCESS
               break
           fi

           sleep 3
           ((count=count-1))
        done
    fi


    if [[ $retval -ne $SUCCESS ]]
    then
	echo "WARNING: The currently running swagentd daemon could not be killed."
	echo "         The daemon must be killed manually, and the new one started"
	echo "         manually.  Until then, Software Distributor processes on"
	echo "         this host will use the currently running daemon, which might"
	echo "         cause errors."
    fi
    return $retval
} # End of Swagentd_shutdown()
#
########
# Swagentd_restart
#
# Start the swagentd daemon.  Give error message if failure.
#
Swagentd_restart()
{
    /usr/sbin/swagentd
    retval=$?
    if [[ $retval -ne $SUCCESS ]]
    then
	echo "ERROR:   The Software Distributor agent daemon could not be started."
	echo "         Further software configuration cannot be done without it."

    fi
    return $retval
} # End of Swagentd_restart()


########################################################################
## MAIN 
########################################################################
#
#  If SW-GETTOOLS version of these bits exist on the system then restore
#  the links to this version.  They were probably removed
#  by the preremove script for this fileset
#

if [[ -f /usr/lbin/swagent ]] && [[ -f /var/adm/sw/lbin/swagent ]]
then
	mv /usr/lbin/swagent /usr/lbin/swagent#
	ln -s /var/adm/sw/lbin/swagent /usr/lbin/swagent
fi

if [[ -f /usr/sbin/swagentd ]] && [[ -f /var/adm/sw/sbin/swagentd ]]
then
	mv /usr/sbin/swagentd /usr/sbin/swagentd#
	ln -s /var/adm/sw/sbin/swagentd /usr/sbin/swagentd
fi

#
#
# Make sure /var/adm/sw/ exists; otherwise, when we start up the daemon,
# it will fail because it can't open its log file /var/adm/sw/swagentd.log.
#
if [ ! -d /var/adm/sw ]; then
    mkdir -p /var/adm/sw
    chmod 555 /var/adm/sw
fi

#
# Kill the daemon and re-start it.
#
Swagentd_shutdown
retval=$?
if [[ $retval -eq $SUCCESS ]]; then
    Swagentd_restart
    retval=$?
    # If restarting failed, bail out NOW.
    [[ $retval -ne $SUCCESS ]] && exit $retval
fi

exit $exitval


