#!/bin/sh
#
# Copyright 1994 Sun Microsystems, Inc. All Rights Reserved
#
# Arguments: start or stop.
#

killtrace() {
	PID=`ps -ef |grep $RK6TRACE |grep -v grep|awk '{ print $2 }'`
	if [ -n "$PID" ]; then
		kill -KILL  $PID 2>&1 >/dev/null
	fi
}

killstat() {
	PID=`ps -ef |grep $RK6STAT |grep -v grep|awk '{ print $2 }'`
	if [ -n "$PID" ]; then
		kill -KILL  $PID 2>&1 >/dev/null
	fi
}

killnetd() {
	PID=`ps -ef |grep $RK6D |grep -v grep|awk '{ print $2 }'`
	if [ -n "$PID" ]; then
		kill -TERM $PID  >/dev/null
		#
		# wait until the daemon is really dead
		#
		while [ -n "$PID" ]; do
			PID=`ps -ef |grep $RK6D |grep -v grep|awk '{ print $2 }'`
		done
	fi
}

moduload() {
	MODID=`modinfo | grep $RK6 | awk '{ print $1 }'`
	if [ -n "$MODID" ]; then
		/usr/sbin/modunload -i $MODID > /dev/null
	fi
	/bin/echo "*** Unloading otk6 module."
}

RK6BIN="/opt/SUNWconn/rk6/bin"

RK6=RFC1006
RK6D=rk6d
NETD="$RK6BIN/$RK6D"
RK6TRACE=rk6trace
RK6STAT=rk6stat

#
# Set path to allow autopush.
#
PATH="/etc:/usr/etc:/bin:/sbin:/usr/bin:/usr/sbin" ; export PATH

mode=$1

case "$mode" in
'start')
	/bin/echo "starting rk6d (RFC1006) daemon\c"

	ERR=0
	#
	#
	if [ ! -x $NETD ]; then
		/bin/echo "*** Cannot execute $NETD \n"
		ERR=1
	fi

	if [ -f /etc/rc2.d/S90osinet ]
	  then
	    /bin/echo "*** Detected incompatible product with RK6 : OSI \n"
	    /bin/echo "    Please clean your installation before starting RK6\n"
	    ERR=1
	fi
	if [ $ERR -eq 1 ]; then
		exit 1
	fi
	#
	# starting the daemon
	#
	$NETD
	ERR=$?
	if [ ! $ERR -eq 0 ]; then
		/bin/echo "*** Unable to start rfc1006 daemon. \n"
		exit $ERR
	fi
	/bin/echo '.'
	
	;;
'stop')
	/bin/echo "*** Stopping rk6d daemon."

	#
	# Initialize the file for the returncode of the
	# daemon.
	#

	killtrace
	killstat
	killnetd
	moduload

	;;

*)
	/bin/echo "Usage: $0 start | stop \n"
	exit 1
	;;
esac
