#!/bin/ksh
#
#ident	"@(#)ser_rdwt.stf	1.4	04/12/15 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Variables
prgdir=../prgs                  # program directory
maxwarn=80   
rhname=$1


#functions
cleanup()
{
	echo "read/write processes ending!"
	kill -17 $ser_rd_id
	kill -17 $ser_wt_id
	rsh $rhname "pkill ser_rd"
	rsh $rhname "pkill ser_wt"
}

# 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

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

RUNTIME=`expr $RUNTIME - 240`

RUNT=`expr $RUNTIME / 2`


ser_rdwt/bin/ser_rd -d $l_device -b $b_rate > /tmp/rd_r.tmp &
ser_rd_id=$!
sleep 6
rsh -l root $rhname "cd `pwd` ; env RUNTIME=$RUNT ser_rdwt/bin/ser_wt -d $r_device -b $b_rate >/tmp/rd_w.tmp &" &
wait $ser_rd_id

rcp $rhname:/tmp/ser_wt.tmp /tmp/ser_rd.$rhname.tmp
cmp -s /tmp/ser_rd.tmp /tmp/ser_rd.$rhname.tmp

if [ $? -ne 0 ]
then
	echo "!ERROR: The test could not pass read test. \n"
        exit 1
else
	echo "!PASS: The test could pass read test. \n"
fi

rm -f /tmp/ser*tmp
rsh -l root $rhname rm -f /tmp/ser_wt.tmp

sleep 120


rsh -l root $rhname "cd `pwd` ;  ser_rdwt/bin/ser_rd -d $r_device -b $b_rate >/tmp/wt_r.tmp &"  &
ser_wt_id=$!
sleep 6
env RUNTIME=$RUNT ser_rdwt/bin/ser_wt -d $l_device -b $b_rate > /tmp/wt_w.tmp &
wait $ser_wt_id

rcp $rhname:/tmp/ser_rd.tmp /tmp/ser_wt.$rhname.tmp
cmp -s /tmp/ser_wt.tmp /tmp/ser_wt.$rhname.tmp

if [ $? -ne 0 ]
then
	echo "!ERROR: The test could not pass write test. \n"
        exit 1
else
	echo "!PASS: The test could pass write test. \n"
fi

rm -f /tmp/ser*tmp
rsh -l root $rhname rm -f /tmp/ser_rd.tmp

sleep 120
echo "!PASS: the test case of ser_rdwt pass \n"

