#!/bin/ksh
#
#ident	"@(#)cl3se3.stf	1.11	04/12/15 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# This is the CL3SE3 NETWORK TEST MODULE
#
#########################################################################
# NOTES ON OPERATION AND ERROR HANDLING:
# This script is called by staf.eng, which is the main test harness.
# Standard output gets recorded in the test module log file by staf.eng.          
#
# Variable maxwarn is checked by staf.eng in the "chk_err()" monitoring process,               
# by grep'ing the log file of this test module.
# If maxwarn is exceeded, the staf.eng script will kill this test module.
# There are two formats for the error echos to the log files;
#
# 1.   echo "!ERROR: blah blah foo bar";
#      which will terminate the test.  If the test harness detects
#      a string match with "!ERROR", then all testing terminates.
#
# 2.   echo "!WARNING: blah blah foo bar";
#       The above format "!WARNING:" will make the log file entry,
#       but all testing will continue, no termination of tests,
#       UNLESS maxwarnings variable is exceeded.
#
# DANGER!!:  Even if you do not intend to use WARNING messages, variable
# maxwarn *MUST* be defined and the "maxwarnings=" line *MUST* be
# echo'ed to the logfile.  This syntax is mandatory.
#########################################################################

# variables
prgdir=../prgs                  # program directory
maxwarn=80			# max number of nonfatal warnings to tolerate.
clientmachine=$1		# Remote machine to start client on

port=4004
multiportfile=/tmp/multiportfile

# functions

clean_up()
{
	echo "Cl3se3 ending."
	kill -17 $se3id
}

### MAIN ###

# Trap signals
trap 'clean_up; exit' 17

# Check args
if [ -s $multiportfile ]
then
	if [ $# -ne 2 ]
	then
		echo "!ERROR: $0 called with bad arguments."
		exit 1
	else
		clientmachine=`head -$2 $multiportfile | tail -1 | cut -d';' -f2`
		serveraddress=`head -$2 $multiportfile | tail -1 | cut -d';' -f1`
	fi
	sleep $2 #Avoid conflict
else
	if [ $# -ne 1 ]
	then
		echo "!ERROR: $0 <client_host>"
		echo "!ERROR: $0 called with bad arguments."
		exit 1
	fi
fi

# SET LOGFILE FLAG FOR MAX WARNING COUNT
# This is checked by staf.eng, and if exceeded, staf.eng will kill this module.
echo "maxwarnings=${maxwarn}"

# Determine the server's IP address - not always what you
# expect if the machine has multiple interfaces

ping $clientmachine
if [ $? -ne 0 ] ; then
	echo "!ERROR:   No connectivity to $clientmachine."
	exit 1
fi

if [ ! -s $multiportfile ]
then
	interface=`/usr/sbin/arp -a | grep -w $clientmachine | awk '{print $1}' `
	if [ -z "$interface" ]
	then
		interface=`/usr/sbin/arp -an | grep $clientmachine | awk '{ print $1 }'`
	fi
	ifconfig_entry=`ifconfig $interface | grep inet`
	serveraddress=`expr "$ifconfig_entry" : '.*inet \([0-9\.]*\).*'`
fi

echo "serveraddress: $serveraddress"

if [ ! -z `ps -ef | grep 'cl3se3' | grep -v 'grep'` ]
then
    	instances=`ps -ef | grep 'cl3se3'|grep -v 'grep' | uniq |wc -l`
	port=`expr $port + $instances`
	cl3se3/bin/se3 -p $port $serveraddress &
	se3_6id=$!

	rsh $clientmachine "cd `pwd` ; env RUNTIME=$RUNTIME cl3se3/bin/cl3 -p $port $serveraddress"
else
    	cl3se3/bin/se3 $serveraddress &
	se3_6id=$!

	rsh $clientmachine "cd `pwd` ; env RUNTIME=$RUNTIME cl3se3/bin/cl3 $serveraddress"
fi

if [ -d /proc/$se3id ] ; then
	kill -9 $se3id
fi
