#!/bin/sh
set -x

#-----------------------------------------------------------------------------
# padtest.sh -
#
# Copyrighted as an unpublished work.
# (c) Copyright 1991 SBE, Inc. and by other vendors under license agreements.
# All Rights Reserved.
#-----------------------------------------------------------------------------
# RCS info:
# RCS revision: $Revision: 1.1 $
# Last changed on $Date: 91/09/13 17:51:55 $
# Changed by $Author: root $
#-----------------------------------------------------------------------------
# $Log:	padtest.sh,v $
# Revision 1.1  91/09/13  17:51:55  root
# Compile data generation program 'pdg'.
# 
# Revision 1.0  91/09/13  17:41:29  root
# Initial revision
# 
#-----------------------------------------------------------------------------
#

PASS=0
FAIL=1
TRUE=1
FALSE=0
MAXBOARD=3

############################################################################
# Parse command line

if [ $# = 0 ]	# no parameters = display required command syntax
then
	echo "usage: $0 boardno ch1 ch2 [ repeat tstno circuits ]"
	echo "       if boardno>$MAXBOARD, then enter interactive mode"
	exit $FAIL
fi

BRDNO=${1:=100}        # board to test, default to interactive mode

if [ $BRDNO -gt $MAXBOARD ]  # enter interactive mode
then
	INTERACTIVE=$TRUE
	BRDNO=0     # default to board 0
else
	INTERACTIVE=$FALSE
fi

CH1=${2:='A'}   # first channel to test, default channel A
CH2=${3:='B'}   # second channel to test, default channel B
LOOPCOUNT=${4:=5}   # test repetition count, default = 5
TESTNO=${5:="3"}    # tests to run, default = 3
NUM_VCS=${6:=15}    # number of virtual circuits, default = 15.  Note that
           			# there must be a getty executing for each circuit

############################################################################
# Set up program references

PGM_XCF="../xcf/xcf"
PGM_RESET="../vcomdl/vcom_rst"
PGM_DOWNLOAD="../vcomdl/vcom_dl"
PGM_STAT="../vcomdl/vcom_stat"
PGM_XLD="../vcomdl/vcom_xld"
PGM_CONFIG="../vcomdl/vcom_cfg"

PADTEST="padtest"
PGM_PADTEST="./$PADTEST"

DATAGEN="pdg"
PGM_DATA="./$DATAGEN"

############################################################################
# compile the test programs
if [ ! -e $PGM_PADTEST ]
then
	cc -O -o $PADTEST -I../../include $PADTEST.c
fi

if [ -e $PGM_DATAGEN ]
then
	cc -O -o $DATAGEN -I../../include $DATAGEN.c
fi

############################################################################
# RESET and RELOAD macro

re_st_start()
{
	$PGM_RESET -f $BRDNO
	$PGM_DOWNLOAD $BRDNO
	$PGM_STAT $BRDNO
	$PGM_CONFIG $BRDNO
	if [ x"$1" = x"xxx" ]
	then
		$PGM_XLD $BRDNO
	fi
}

############################################################################
# Set up the test Environment for pad_tests
# set up data for input to xcf - channel 1

echo "TYPE = X25
L3PLPMODE = DCE
LAPB_MODE = DCE
LAPB_phy_if = DCE
LAPB_MAXFRAME = 263
LAPB_BAUD = 56000
X25_VSN = 84
HTC = 255
LTC = 1 " > /tmp/x25port.$CH1
    
# set up data for input to xcf - channel 2

echo "TYPE = X25
L3PLPMODE = DTE
LAPB_MODE = DTE
LAPB_phy_if = DTE
LAPB_MAXFRAME = 263
X25_VSN = 84
HTC = 255
LTC = 1 " > /tmp/x25port.$CH2

############################################################################
# create initial configuration data

$PGM_XCF -b $BRDNO -c $CH1 /tmp/x25port.$CH1   # setup config info
rm -f /tmp/x25port.$CH1
$PGM_CONFIG -b $BRDNO -c $CH1                  # reprogram the channel

$PGM_XCF -b $BRDNO -c $CH2 /tmp/x25port.$CH2   # setup config info
rm -f /tmp/x25port.$CH2
$PGM_CONFIG -b $BRDNO -c $CH2                  # reprogram the channel

############################################################################
#
#re_st_start xxx                    # this should not be needed

############################################################################
#

if [ $INTERACTIVE = $TRUE ]
then
	echo "\t\t MAIN MENU :

\t\t 1> Run the Data Transfer test on multiple VCs
\t\t 2> Run the Discconect -Connect test on multiple VCs
\t\t 3> Run test 1 and 2 in batch mode

\t\t Please select your option or 'q' to quit :\c"
	read TESTNO
fi

case $TESTNO in

1) 				
		Test="1"
		;;
2)	
		Test="2"
		;;
				
3)
		Test="1 2"
		;;
q)
		exit $PASS
		;;
*)		echo "$0 ABORT: bad test option = $TESTNO."
		exit $FAIL
		;;
esac

############################################################################
#

if [ $INTERACTIVE = $TRUE ]
then
	while :
	do
		echo "Enter number of virtual connections (<256): \c"
		read NUM_VCS

		if [ $NUM_VCS -lt 0 -o $NUM_VCS -gt 255 ]
		then
			echo "Invalid Number."
		else
			break
		fi
	done
fi

############################################################################
#

if [ $INTERACTIVE = $TRUE ]
then
	while :
	do
		echo "Enter number of repetitions for the test (<26): \c"
		read LOOPCOUNT

		if [ $LOOPCOUNT -lt 0 -o $LOOPCOUNT -gt 25 ]
		then
			echo "Invalid Number."
		else
			break
		fi
	done
fi

for i in $Test
do
	$PGM_PADTEST -n$LOOPCOUNT -c$NUM_VCS -t$i
	RESULT=$?
	if [ $RESULT != $PASS ]
	then
		exit $RESULT
	fi
done

exit $PASS
