#!/bin/sh

################################################################################
#	Shell file to start, stop and query IPX Gateway 
#
#       Modified by Anil.Srivastava@eng.sun.com to add the following
#		1)  version option
#		2)  remove the ij.status (run time status) 
#		    file on stopping the server
#		3)  enhanced status section
################################################################################


usage='Usage: ipxgate [start|stop|status|cfg|version]'


#
#	Gloabal variables
#
PKGNAME=SUNWipxip
STARTTIME=/var/opt/$PKGNAME/stime
IJHOME=/opt/$PKGNAME/bin
IJSERVER=ijserverd
IJDNS=ijdnsd
IJPIDFILE=$IJHOME/ijserverd.pid
IJSTATUSFILE=$IJHOME/ij.status
HOSTINFO=`uname -a`

# The following section accompalishes two things -
#	o  finds out if IPX/SPX is configured and running by querying 
#	   the runtime IPX/SPX configuration information.  If yes, then
#	   IPXCONF is set to 1 else it is set to 0
#	o  sets the env variable IPXSTAT to point to the right version of
#	   tnistat or ipxstat

if [ -f /opt/SUNWipx/bin/tnistat ]; then
    # this means PPS 1.1 is installed
    IPXPROD="PC Protocol Services 1.1"
    ISIPXCONF=`/opt/SUNWipx/bin/tnistat -ch | grep -v internal|wc -l`
    IPXSTAT=/opt/SUNWipx/bin/tnistat
elif [ -f /opt/SUNWefps/sbin/ipxstat ]; then 
    # this means PPS 2.0 is installed
    IPXPROD="Solstice connect/NW 2.0"
    ISIPXCONF=`/opt/SUNWefps/sbin/ipxstat -ch | grep -v internal|wc -l`
    IPXSTAT=/opt/SUNWefps/sbin/ipxstat
elif [ -f /opt/SUNWefps/nw/tnistat ]; then 
    # this means PSS 1.1 is installed
    IPXPROD="SolarNet PC Server Services 1.1"
    ISIPXCONF=`/opt/SUNWefps/nw/tnistat -ch | grep -v internal|wc -l`
    IPXSTAT=/opt/SUNWipx/nw/tnistat
else
    # set default values
    ISIPXCONF=0
    IPXSTAT=/opt/SUNWefps/sipx/ipxstat
fi
#
# Check command line parameters, exit if more or less than one argument
#

if [ $# -ne 1 ] ; then
	echo $usage
	exit 1
fi

#


if [ "$1" = "start" ] ; then

	if [ $ISIPXCONF = 0 ] ; then
		echo IPX/SPX is not configured or enabled.  Please check
		echo you IPX/SPX configuration.
		exit 1
	fi

	#
	#	Check if IJPIDFILE exists.  
	#
	IPXGATEUP=`ps -ef|grep ijserverd|grep -vc grep`
	if [ $IPXGATEUP = 1 ] ; then
		echo	IPX Gateway already running >&2
		exit 0
	else
		/usr/bin/rm -f $IJPIDFILE >&2
		echo	Starting IPX Gateway >&2
	fi

	#
	#	Check if executables exist
	#

	if [ ! -f $IJHOME/$IJSERVER ] ; then
		echo	Cannot locate IPX Gateway executable
		exit 1
	elif [ ! -f $IJHOME/$IJDNS ] ; then
		echo	Cannot locate IPX Gateway DNS module executable
		exit 1
	fi

	#
	#	Start ijserver
	#

	cd $IJHOME
	$IJHOME/$IJSERVER > ij.log 2>&1 &


	#
	#	Log pid to IJPIDFILE
	#

	ijpid=$!
	echo $ijpid > $IJPIDFILE
	chmod 664 ${IJPIDFILE}
	date > $STARTTIME 
	echo	IPX Gateway started with process id $ijpid

	exit 0


elif [ "$1" = "stop" ] ; then

	#
	#	Check if pid file exists
	#

	if [ ! -f $IJPIDFILE ] ; then
		echo IPX Gateway is not running
		exit 1
	else
		ijpid=`cat $IJPIDFILE`
		echo Stopping IPX Gateway, with process id $ijpid
		kill -TERM $ijpid 
		rm -f $IJPIDFILE
		rm -f $IJSTATUSFILE
		rm -f $STARTTIME
		echo IPX Gateway stopped
	fi
	exit 0
	
elif [ "$1" = "status" ] ; then

	#
	#	Check if pid file exists
	#

	if [ ! -f $IJPIDFILE ] ; then
		echo IPX Gateway is not running
		exit 1
	else
		echo 
		ijpid=`cat $IJPIDFILE`
		STARTTIME=`cat $STARTTIME`
		kill -USR1 $ijpid
		sleep 2
		kill -USR1 $ijpid
		if [ $? -ne 0 ] ; then
			echo IJserver not running
			rm -f $IJPIDFILE
		else 
			echo Server Summary:
			echo ---------------
                        echo IPX Gateway is running  since $STARTTIME
			echo $HOSTINFO
			grep -s Active $IJSTATUSFILE
			if [ -f /opt/SUNWipxip/bin/saddr ] ; then
			    SERVADDR=`/opt/SUNWipxip/bin/saddr -t840|grep Address | grep -v Bound|/bin/sed -e "s/[:]//g"`
		   	    echo Server $SERVADDR
			else
			    SERVADDR=`grep -s "Server address" $IJSTATUSFILE`
			    echo $SERVADDR
			fi
			if [ -f $IPXSTAT ] ; then
			   echo
			   echo Gateway interfaces:
			   echo -------------------
			   $IPXSTAT -c
			   
			fi
			echo
			echo Client Summary:
			echo ---------------
			echo 'IPX Network\tMAC Address\tConnection ID.'
			grep -s "Client Addr" $IJSTATUSFILE |awk '{print $3 ":" $5}'|/bin/sed -e "s/Net#://g"|/bin/sed -e "s/:Node#:/\./g"|sed -e "s/[:]/\./g"|/bin/awk '{num = split($1,sec,".");for(i=1;i<=num;i++) if (length(sec[i])<2) sec[i] = "0" sec[i];printf("%s%s%s%s\t%s%s%s%s%s%s\t%8d\n", sec[1],sec[2],sec[3],sec[4],sec[5],sec[6],sec[7],sec[8],sec[9],sec[10],sec[11]);}'
			echo
			echo 'Total number of \c'
			grep -si "(s)" $IJSTATUSFILE
			echo
			echo IPX Packets in/out:
			echo -------------------
			grep -s "packet counts" $IJSTATUSFILE		 
			echo 
		fi
	fi
	#/bin/rm $IJSTATUSFILE
	exit 0

elif [ "$1" = "version" ] ; then

	#
	#	Report the version and build date of the server
	#
	OS=`uname -sr`
	ARCH=`uname -p`
	echo IPX Gateway installed on $OS /  $ARCH
	echo Server Version: 1.2.3
	echo Server Build Date: May 1996
	echo 
	exit 0

elif [ "$1" = "cfg" ]; then
	if [ -f $IPXSTAT ] ; then
	   echo
	   echo $IPXPROD is installed on $HOSTNAME
	   echo
   	   echo IPX/SPX configuration 
   	   echo ---------------------
	   $IPXSTAT -c
	   echo
	else
	   echo IPX/SPX protocol is not installed.  
	fi
	exit 0
else
	echo $usage
	exit 1
fi



