#!/bin/ksh
#
#ident	"@(#)flowctl.stf	1.7	04/12/15 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# This program is the control program of flowctl test case. It should call Iflowctl and Oflowctl in parallel. 
# Variables
prgdir=../prgs                  # program directory
maxwarn=80   
rhname=$1


#functions
cleanup()
{
	echo "flowctl ending!"
	kill -17 $flhid
	rsh $rhname "pkill -x Iflowctl"
}

# Main
if [ $# -lt 4 ]
then
	echo "!ERROR: $0 <rhname> <local_device_name> <remote_device_name> <baud_rate>"
	echo "!ERROR: $0 called with bad arguments."
	exit 1
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}"

l_device=$2
r_device=$3
b_rate=$4

RUNTIME=`expr $RUNTIME - 60`

RUNT=`expr $RUNTIME / 2`

ping $rhname
if [ $? -ne 0 ]
then
	echo "!ERROR: Cannot ping ppp_client $ppp_client"
	exit 1
fi

rsh $rhname "cd `pwd` ; flowctl/bin/Iflowctl -s -d $r_device -b $b_rate >/dev/null &" &
Iflsid=$!
sleep 4
env RUNTIME=$RUNT flowctl/bin/Oflowctl -s -d $l_device -b $b_rate &
Oflsid=$!
wait $flsid

rcp $rhname:/tmp/Iflowctl.tmp /tmp/Iflowctl.$rhname.tmp

cmp -s /tmp/Iflowctl.$rhname.tmp /tmp/Oflowctl.tmp

if [ $? -ne 0 ]
then
	echo "!ERROR: The test could not pass software flow control test. \n"
	rsh $rhname rm -f /tmp/Iflowctl.tmp 
	rm -f /tmp/Oflowctl.tmp  /tmp/Iflowctl.$rhname.tmp
        exit 1
else
	echo "!PASS: The test could pass software flow control test. \n"
fi
	
rsh -l root $rhname rm -f /tmp/Iflowctl.tmp 
rm -f /tmp/Oflowctl.tmp  /tmp/Iflowctl.$rhname.tmp

#sleep 60 
wait $Oflsid

rsh $rhname "cd `pwd` ; flowctl/bin/Iflowctl -n -d $r_device -b $b_rate >/dev/null &" &
flnid=$!
sleep 4
env RUNTIME=$RUNT flowctl/bin/Oflowctl -n -d $l_device -b $b_rate &
wait $flnid

rcp $rhname:/tmp/Iflowctl.tmp /tmp/Iflowctl.$rhname.tmp
if [ -z `cat /tmp/Iflowctl.$rhname.tmp` ]
then
	echo "!ERROR: The test could not pass no flow control test. \n"
else
	echo "!PASS: The test could pass no flow control test. \n"
fi

rsh -l root $rhname rm -f /tmp/Iflowctl.tmp 
rm -f /tmp/Iflowctl.$rhname.tmp
