#!/bin/sh
# Copyright 10 Jun 1996 Sun Microsystems, Inc. All Rights Reserved.
#
# This script will start/stop the EM SNMP Trap daemon 'em_snmp-trap'

EM_HOME="${EM_HOME-/opt/SUNWconn/em}"

#
# Return pid of named process in variable "pid"
#
pidproc() {
	pid=`/usr/bin/ps -e |
		/usr/bin/grep -w $1 |
		/usr/bin/sed -e 's/^  *//' -e 's/ .*//'`
}

#
# Kill named process(es)
#
killproc() {
	pidproc $1
	[ "$pid" != "" ] && kill -9 $pid > /dev/null 2>&1
}

#
# SNM trap daemon is not compatable with em, if SUNWembc is installed
# it is removed from /etc/inetd.conf and inetd is sent a HUP signal.
#
check_oldtrapd () {

/usr/bin/pkginfo SUNWembc.* > /dev/null 2>&1
if test $? -eq 0
then
   if test ! -z "`/usr/bin/grep '^snmp-trap' /etc/inetd.conf`"
   then
# remove snmp-trapd from inetd.conf
      chmod 644 /etc/inetd.conf
      ex - /etc/inetd.conf <<ENDEDIT >/dev/null
1,\$s/^snmp-trap/# snmp-trap/g
w
q
ENDEDIT
      chmod 444 /etc/inetd.conf

# instruct inetd to re-read config file
      pidproc inetd
      [ "$pid" != "" ] && kill -HUP $pid

# kill off na.snmp-trap
      killproc na.snmp-
   fi
   pidproc inetd
   [ "$pid" != "" ] && kill -HUP $pid
# for the processes to release the port 162
   sleep 8
fi
}

case "$1" in

'start')
	if [ -x $EM_HOME/bin/em_snmp-trap ]; then
                killproc em_snmp-
		check_oldtrapd
#                echo "Starting em_snmp-trap  ...\n"
				if [ -z "$2" ]; then
                	$EM_HOME/bin/em_snmp-trap &
				else
                	$EM_HOME/bin/em_snmp-trap -p $2 &
				fi
				sleep 1
	fi

	;;
'stop')

	killproc em_snmp-
	;;

*)
	echo "Usage: $EM_HOME/bin/em_trapd { start [port] | stop }"
	;;
esac
exit 0
