#! /bin/sh
#
# @(#)stopMC	1.9	LPS_UNX_COM	2/19/95
#
# Copyright 1994   Digital Equipment Corporation, Maynard, MA
#
# stopMC
#
# Stop a Management Client daemon
#
# Parameters:
#    $1 - Name of the Management Client object.
#
# Global variables:
#    ECHON
#    OSTYPE
#    PSA
#
# Exit values:
#    0 - Success, daemon was either stopped or was not running.
#    1 - Failure of some kind, error messages to stderr.
###

MCNAME=$1

# Get the pid of the currently runnning daemon, and check
# to see if it is running.  If it is, then send it an INT
# signal to force it to gracefully terminate.

: ${LPSODB:=/etc/lpsodb}
: ${LPSODBLIST:=lpsodblist}
: ${MCSTOPSIG:=2}	# Must be "2" instead of "INT" for some systems
: ${MCAPP:=lpsad}

if PIDFILE="`$LPSODBLIST -f $LPSODB -e -v lpsroot`/${MCNAME}.pid"
then
    if PID="`cat $PIDFILE 2> /dev/null`"
    then
	if [ $OSTYPE = OSF1 ]  ||  kill -0 $PID
	then
	    $ECHON "Stopping Management Client \"$MCNAME\" (pid: $PID) ..."

	    kill -$MCSTOPSIG $PID
	    echo "done."
	    exit 0
	fi
    fi
fi

# If we get here, then something didn't work.  Could be something
# to do with the pid file (disk full, etc), so use the old fashioned
# technique of grepping for the process using the "ps" command.

STRING="${MCAPP}.*${MCNAME}"

case $OSTYPE in                     # To fetch the pid from the 'ps' line
    OSF1 | BSD ) AWKCMD='{print $1}' ;;
           SV3 ) AWKCMD='{print $2}' ;;
           SV4 ) AWKCMD='{print $2}' ;;
esac

PID=`$PSA | egrep "$STRING" | egrep -v egrep | head -1 | awk "$AWKCMD"`

if [ "$PID" ]
then
    $ECHON "Stopping Management Client \"$MCNAME\" (pid: $PID) ..."

    if kill -$MCSTOPSIG $PID
    then
	echo "done."
    else
	exit 1
    fi
else
    echo "    [Management Client \"$MCNAME\" is not currently running]"
    echo
fi

exit 0
