#!/bin/sh
#
#	ident "@(#)S147osiftrd	1.20 08/09/02" SMI
#
#	Copyright 08/09/02 Sun Microsystems, Inc. All Rights Reserved
#
#	S147osiftrd: start/stop the FTAM Responder daemon 'osiftrd' on each subnetwork
#		     The daemon on CNLS is ALWAYS started (loopback facility offered)
#		     whilst the other two ones only if corresponding devices found and
#		     if they are configured into stack.
#		     The three daemons possibly started are connected on subnetwork
#			 cons -> (X25)    by sap no.  64
#			 clnp ->          by sap no. 164
#		     tcp  -> (RF1006) by sap no. 264
#

THIS1=`basename $0`

#
# Error Codes:
#
e_ok=0			# Everything's fine
e_usage=1		# Wrong Usage
e_noexe=2		# not found EXE_PATH /opt/SUNWconn/bin
e_noftrbin=3	# Can't find binary of FTAM Responder
e_mustberoot=4	# You must be root to run this script
e_noftrdbin=5	# Can't find binary of FTAM Responder DAEMON (ftrd)
e_alrun=6		# osiftrd already running on the sap
e_nostkrun=7	# OSI stack not running (DAEMON osinetd)
e_ftrdnotrun=8	# osiftrd's not running
e_noosinetd=9	# cannot find 'osinetd.cnf' file

#
# Error Function: $1 = Message ; $2 = exit code
#
err () {
        echo "\n## $THIS1 ERROR: $1\n" 1>&2
        if [ $2 -ne $e_ok ]; then
#	        echo "## exit value is $2"
            exit $2
        fi
}

#
#  Init
#

EXE_PATH=/opt/SUNWconn/bin					# Path of executables (is SUNWftam* pkg installed?)
FTR_BIN=osiftr								# Name of FTAM Responder executable
FTRD_BIN=osiftrd							# Name of FTAM Responder Daemon executable
OSINETD=/etc/SUNWconn/osinet/osinetd.conf	# Name of stack conf. file
RK6D=/etc/opt/SUNWconn/rk6/rk6d.conf		# Name of stack conf. file
X25_DEV=/dev/x25							# Device name for x.25
TCP_DEV=/dev/tcp							# same for tcp ip
SAP_CONS=64									# Sap no. for cons subnetwrk
SAP_CLNP=164								# Sap no. for clnp subnetwrk
SAP_TCP=264									# Sap no. for tcpip subnetwrk
SAPS=$SAP_CLNP								# set of saps for which daemons must be started

# Launch of daemons dependends on underlying stacks
# availability and configurations

PATH="/sbin:/bin:/usr/bin:/usr/ucb:/etc:$PATH"
 
export PATH

#
# Usage
#
 
USAGE=`cat <<%
Usage: $THIS1 start|stop
%
`

trap  'echo "Interrupted "; exit ' INT QUIT

if [ $# -ne 1 ]; then
	echo "$USAGE" 1>&2
	exit $e_usage
else mode=$1
fi

#
# Set PATH. executables are expected to be in the directory 
#	/opt/SUNWconn/bin
# Att! to start in a right way 'osiftr', its path must be in PATH
# when starting the daemon 'osiftrd'
 
if [ ! -d $EXE_PATH ]; then
	err "$EXE_PATH not mounted ?" $e_noexe
fi

PATH="$EXE_PATH:$PATH"
 
#
#  Must be root
#
if  [ "`id|sed 's/[()]/ /g'|awk '{print $2}'`x" != "root"x ]; then 
	err "You must be \"root\" to run <$THIS1>" $e_mustberoot
fi


case "$mode" in
'start')

	res=`ps -u root -f | grep "osiftamd" | grep -v grep`
	if [ "$res" = "" ]; then
		echo "\nosiftamd not launched waiting just a little bit ... \c"
		sleep 4
	fi

	echo "\nStarting osiftrd on all networks ...\n"
	#
	# Check what subnetworks are present on the machine to start/stop or not
	# their correponding daemons
	# First check devices and then look into stack conf. file
	#

	#
	# Check existence and status of osinetd.cnf file
	#

	if [ ! -f $OSINETD ]; then
		if [ ! -f $RK6D ]; then
			err "\n\nCan't find stack configuration file:\n\t<$OSINETD> or <$RK6D> not found" $e_noosinetd
		fi
		stack_prov=0
		SAPS="$SAP_TCP"

	else
		stack_prov=1
		if [ -c $X25_DEV ]; then
			STATUS=`cat $OSINETD | grep "$X25_DEV" `
			if [ "x$STATUS" != "x" ]; then
				SAPS="$SAPS $SAP_CONS"
			fi
		fi

		if [ -c $TCP_DEV ]; then
			STATUS=`cat $OSINETD | grep "$TCP_DEV" `
			if [ "x$STATUS" != "x" ]; then
				SAPS="$SAPS $SAP_TCP"
			fi
		fi
	fi
	#
	# Check existence of ftr binary: exepected to be again in /opt/SUNWconn/bin
	#

	if [ ! -x $EXE_PATH/$FTR_BIN ]; then
		err "\n\tCan't find FTAM Responder:\n\t\t<$FTR_BIN> not found in <$EXE_PATH>" $e_noftrbin
	fi

	#
	# Check existence of osiftrd binary: see above
	#

	if [ ! -x $EXE_PATH/$FTRD_BIN ]; then
		err "\n\tCan't find FTAM Listener daemon:\n\t\t<$FTRD_BIN> not found in <$EXE_PATH>" $e_noftrdbin
	fi

	#
	# Check the stack (daemon) is running
	#

	if [ $stack_prov -eq 1 ]; then
		STATUS=`ps -u root -f | grep osinetd | grep -v grep`;
		if [ "x$STATUS" = "x" ]; then
			err "\n\nOSI Core Stack is not running" $e_nostkrun;
		fi
	elif [ $stack_prov -eq 0 ]; then 
		STATUS=`ps -u root -f | grep rk6d | grep -v grep`;
		if [ "x$STATUS" = "x" ];	then
			err "\n\nRFC1006 is not running" $e_nostkrun;
		fi
	fi

	#
	# start all daemons over existing subnetworks
	#

	for SAP_NO in $SAPS
	 do

	 #
	 # Assign subnetwork name for output messages
	 #
	 case $SAP_NO in
	 $SAP_CONS)	NETWRK="cons" ;;
	 $SAP_CLNP)	NETWRK="clnp" ;;
	 $SAP_TCP)	NETWRK="tcp/ip"	;;
	 esac

	 echo "\t$NETWRK \c"
	 #
	 # Check if not already running
	 #

	 ps -u root -f | grep "osiftrd -s $SAP_NO" | grep -v grep |
	 while read UID PID PPID C STIME TTY TIME COMMAND
	   do
	    if [ x"$COMMAND" != "x" ]; then
			exit 17
	    fi
	 done

	 if [ $? -ne 0 ]; then
		 echo "\talready running!!!"
	 else
		 #
		 # At last, start it
		 #
		 /bin/nice -10 $EXE_PATH/$FTRD_BIN -s $SAP_NO &
		 pid=$!
		 sleep 1
		 kill -0 $pid 2>&1 >/dev/null
		 if [ $? != 0 ] ;then
		 	echo "\tGot a problem: not started"
		else
			echo "\tstarted"
		fi
#		 echo "## osiftrd id = $!"
	 fi
	done
	echo "\nAll done.\n"
	;;
'stop')
	echo "\nStopping osiftrd on all networks ...\n"

	#
	# Kill all osiftrd daemons it finds
	#

	SAPS="$SAPS $SAP_CONS $SAP_TCP"

	for SAP_NO in $SAPS
	 do

	#
	# Assign subnetwork name for output messages
	#
	 case $SAP_NO in
	 $SAP_CONS)	NETWRK="cons" ;;
	 $SAP_CLNP)	NETWRK="clnp" ;;
	 $SAP_TCP)	NETWRK="tcp/ip"	;;
	 esac

	#
	# Search the right 'osiftrd' and Kill it
	#

	ps -u root -f | grep "osiftrd -s $SAP_NO" | grep -v grep |

	while read UID PID PPID C STIME TTY TIME COMMAND
	  do
	   if [ x"$COMMAND" != "x" ]; then
		echo "\t$NETWRK\tstopped"
#		echo "## pid = "$PID
		kill -15 $PID
		exit 17
	   fi
	done

	if [ $? -eq 0 ]; then
		echo "\t$NETWRK\twas not running"
	fi
	done
	echo "\nAll done.\n"
	;;
*)
	echo "$USAGE" 1>&2
        exit $e_usage
	;;
esac

# Happy End
exit $e_ok 

