#!/bin/sh
#
# "@(#)dptelog.rc.src 1.4 94/08/11
#
# This version of the dptelog.rc script can be used on the Solaris(TM) 
# operating system to start up the dptelog process.
#
# When placed in the /etc/rc2.d directory and named appropriately, such as 
# "S99dptelog", this script will automatically start the dptelog process
# after the Solaris(TM) system boots to its multi-user level.

mode=$1

/usr/sbin/modinfo | grep dpti2o >/dev/null 2>&1
if test $? != "0"
then
	exit 0
fi

usage_error() {
      echo "  $0 start             (start dptelog process)"
      echo "  $0 stop              (stop dptelog process)"
      echo " "
}

if [ ! -d /usr/bin ]
then			# /usr not mounted
	exit 1
fi

set `/usr/bin/id`
if [ $1 != "uid=0(root)" ]; then
        echo "$0: must be run as root"
        exit 1
fi

#
# Find dptelog server pid from the process tree
#
dptelog_server_pid()
{
  grep=/usr/bin/grep
  ps=/usr/bin/ps
  cut=/usr/bin/cut
  awk=/usr/bin/awk

# In following grep for "dptelog" processes, explictly exclude any matches 
# on this shell file named "dptelog.rc"

  $ps -u 0 -l | $grep -v dptelog. | $grep dptelog | $cut -c1-24 | $awk '{print $4 " " $5}' |
  while read pid ppid; do
    parent_dptelog_ps=`$ps -p $ppid | $grep dptelog`
    if [ -z "$parent_dptelog_ps" ]; then
      echo "$pid"
      break
    fi
  done
}

get_dptelog_server_pid()
{
    if [ -x /usr/bin/pgrep ] ; then
		/usr/bin/pgrep -u 0 -P 1 -x dptelog
	else
		dptelog_server_pid
	fi
}

kill_dptelog()
{
	if [ "$1" != "" ]
	then
		SIGNAL=-$1
	fi

        if [ -x /usr/bin/pkill ]
	then
		/usr/bin/pkill $SIGNAL -u 0 -P 1 -x dptelog
	else
#       	get dptelog pid

        	dptelog_pid=`dptelog_server_pid`

#       	kill dptelog process

        	if [ "$dptelog_pid" != "" ] ; then
             		/usr/bin/kill $SIGNAL $dptelog_pid
        	fi
	fi
}

case "$mode" in

'start')
	if [ ! -d /var/opt/SUNWhwrdg ]
	then				# in install
		mkdir -p /var/opt/SUNWhwrdg
	fi

	if [ -x /opt/SUNWhwrdg/dptelog ] ; then
		/opt/SUNWhwrdg/dptelog &
	fi
	;;


'stop') 	
	kill_dptelog
	;;

'reset') 	
	;;

'get_server_pid') 	
	get_dptelog_server_pid
	;;

*)
	usage_error
	exit 1
	;;
esac

exit 0
