#!/bin/csh -f 
#
# $Id: testg,v 1.22 1997/09/24 01:07:16 naamato Exp $
#    Main menu script for testing gated
#
#    Revision History
#
#    11-11-94  JWJ   Created.
#    11-17-94  JWJ   Added more features.
#    11-22-94  JWJ   Added log file save feature.
#    11-29-94  JWJ   Put global variables in separate setup file.
#                    Added shell spawn feature.
#     1-10-95  JWJ   Added "sleep" feature when running tests.
#     1-11-95  JWJ   Save routing tables along with log file.
#     1-13-95  JWJ   Minor bug fix.
#     2-23-95  JWJ   Added prompt for test name, filled in command
#                    to compare actual results with expected.
#     3-1-95   JWJ   Minor bug fix.
#     3-3-95   JWJ   Reworked environment variables.
#     6-28-95  JWJ   Added log file viewing, minor bug fix.
#     6-30-95  JWJ   Added analysis of memory trace calls in log file.
#     8-3-95   JWJ   Removed sleep mode as separate function.
#                    Put in auto compare of route table vs. expected
#                    with sleep duration (renamed to wait duration).
#                    Added function to edit expected results file.
#     8-9-95   JWJ   Added menu print on/off function.
#                    Added ability to put secondary args on choice line.
#                    (Ex: 1 3 6 to set test sequence 3 6)
#     8-11-95  JWJ   Added command line option processing.
#     8-14-95  JWJ   Added "netstat -m" to analyze memory function per request.
#     8-16-95  JWJ   Added synchronization file processing.
#                    Added automatic mode (input from file) processing.
#     8-25-95  JWJ   Removed prompting for test name.  Added extra checks
#                    related to gated running or not.  Added aid to automatic
#                    testing by having program skip to next command if setting
#                    a test sequence and the specified config file does not exist.
#     8-28-95  JWJ   Added code to remove sync file at startup.
#    10-26-95  JWJ   Added -q option to "compare results" call.
#    10-30-95  JWJ   Added process existence check to "kill gated" command.
#    11-1-95   JWJ   Added check for "use/don't use QOS" when calling
#                    expected results script.
#    11-3-95   JWJ   Added "-z" option use for compare script.
#     2-1-96   JWJ   Added extra check when starting gated to avoid shell
#                    error when gated crashes on startup.
#                    Added QOS file referencing on/off to main menu.
#                    Modified flag commands to "choice, !choice".
#                    Added command to print status of all flags.
#     2-2-96   JWJ   Added command to set wait time before comparison
#                    (split off from auto compare command).
#     2-20-96  JWJ   Added check of $status when copying log files.
#                    Modified local log file name to include test name.
#                    Replaced "route flush" code with a shell routine call.
#                    Added "-M" option (do not print main menu).
#     3-1-96   JWJ   Added file name to "file does not exist" diagnostics.
#     3-7-96   JWJ   Changed messages in "set test name" command slightly.
#                    Added command to view parent configuration file generator
#                    (.cfg) file.
#     3-8-96   JWJ   Modified to wait after refresh in auto mode, even if not
#                    comparing against expected results.
#                    Added "normal exit" message for exit command in auto mode.
#     3-13-96  JWJ   Synchronization file (if any) now deleted on normal exit.
#     3-18-96  JWJ   Minor change in main menu entry wording (wait time).
#     4-23-96  JWJ   Changed default "use QOS" from ON to OFF.
#     4-26-96  JWJ   Added command to change test directory.  This allows
#                    testg to handle nested tests.
#     5-09-96  NAA/SWW   Implementation of editor selection at runtime. 
#     5-09-96  SWW   Added a more generic way of checking to see if gated is
#                    running.  Uses a new environment variable - ps_command.
#     5-09-96  SWW   Minor bug fix.  (gated_log_file_name renamed to
#                                     gated_logfile_name)
#     5-13-96  SWW   Check to see if gated is running if Option 8 (flush the
#                    routing table) is selected.  If gated is running, a
#                    warning message is printed and the routes are not flushed.
#     5-15-96  SWW   The PID of gated is now obtained from the gated.pid file.
#     5-20-96  SWW   view_cmd variable added. option 17 isn't working
#		     correctly with the runtime editor/viewer selection...
#     5-20-96  SWW   The check to see if gated is running will ignore matching
#		     processes if "rlogin" or "telnet" is in the name of the
#		     process.  (temporary hack- will be improved)
#     5-29-96  SWW   Added menu choice #25 to better accomodate Merit's
#		     directory organization.
#     6-05-96  SWW   Expanded menu choice #25 for 2 more directory levels,
#                    if needed.
#     6-14-96  SWW   Added some code to protect routes from being flushed
#                    on espresso.
#     6-26-96  SWW   Cleaned up some things, added a sleep before the gated.pid
#                    file is read to keep an old version from being read.
#     7-17-96  SWW   Added the machine name to names of the files saved in
#                    Option 14 (save gated log and routing tables)
#     8-07-96  SWW   An addition to the cheap hack on 5/20.  Ignores
#                    processes that contain "ssh" when looking for gated.
#   		     (** Fix Me **)
#     8-20-96 NAA/SWW Lockfiles are now mode 777.
#     3-03-97 CEH    Change 4 (kill gated) to allow signal to be specified
#                    default action is to SIGTERM not SIGKILL.
#
#    Environment variables used:
#
#    gated_work_dir  Directory where the gated.conf, expected.txt,
#                    and gated (or link to) are located.
#
#    gated_test_dir  Directory containing the individual test subdirectories.
#                    If undefined, sets to $gated_work_dir.
#
#    gated_script_dir      Directory containing scripts needed for running
#                          this program and the gated config file generator.
#
#    gated_config_file     Location and name of the config file used
#                          when running gated.
#
#    gated_results_file    Location and name of expected results file
#                          for the current config file.
#
#    gated_cmp_script      Script which compares routing table
#                          against expected results.
#
#    gated_qos_file        QOS file (used in comparing results)
#
#    gated_pid_file        PID file
#
#    gated_mainlog_dir     Location of gated log file
#    gated_locallog_dir    Where to put a copy of log file
#    gated_logfile_name   Log file name
#    gated_sync_dir        Directory for node synchronization
#
#    gated_perl_cmd        Perl executable (dir and file name)
#    ps_cmd                Syntax for the ps command.
#
#    EDITOR		   Editor to use.
#    view_cmd		   Command for viewing (edit a file in readonly mode).
#
#
#    Local variables
#
set proc_id = 0
set NULL="<none>"
set test_name=$NULL
set test_num=($NULL $NULL)
set auto_compare_mode="OFF"
set synchronize_mode="OFF"
set start_gated="OFF"
set use_qos="OFF"
set operating_mode="MANUAL"
set gated_sync_files=""

set verbose_level=1
set infoecho=echo
set infoechonl="echo -n"
set verbecho=$gated_script_dir/noecho
set logging="ON"
set timestamp=0

onintr USERBREAK

set os=`uname -s`


#
# Default waiting time before comparing expected results vs. routing table
#
if ( $?testg_def_wait_time ) then
	set wait_duration=$testg_def_wait_time
else
	set wait_duration=30
endif

#
#    Read variables on support/log file names and locations
#
# source $gated_script_dir/testg_setup.csh

#   Get the root hostname of the machine we are on
#   ('hostname -s' not standard on all machines)
set host=`hostname | sed -e 's/\([^.]\)\..*/\1/'`

#############################################################################
#      BAND-AID -- BAND-AID -- FIX ME PLEASE
#############################################################################
#if ( $host == "rivendell" ) then
	#set host=rv
#endif
#if ( $host == "excelsior" ) then
	#set host=excel
#endif
if ( $host == "Fox" ) then
	set host=fox
endif
if ( $host == "Espresso" ) then
	set host=espresso
endif

#   Set synchronization file name
set sync_file_name=$gated_sync_dir/$host.lock

#
#    Process command line options
#
set print_usage="OFF"
set print_menu="ON"
set argc=$#argv
set i=1
while ( $i <= $argc )
	switch ( $argv[$i] )
	case "-t":
		# Make sure another argument follows
		if ( $i == $argc ) then
			echo "error: no test name specified"
			set print_usage="ON"
		else
			@ i = $i + 1
			set test_name=$argv[$i]
		endif
		breaksw

	case "-s":
		# Make sure two more arguments follow.
		if ( $i + 2 > $argc ) then
			echo "error: must supply both sequence and test number"
			set print_usage="ON"
		else
			@ i = $i + 1
			set test_num[1]=$argv[$i]
			@ i = $i + 1
			set test_num[2]=$argv[$i]
		endif

		# Warn user if no test name specified.
		if ( "$test_name" == "$NULL" ) then
			echo "warning: test name not specified"
		endif
		breaksw

	case "-a":
		set operating_mode="AUTOMATIC"
		breaksw

	case "-g":
		set start_gated="ON"
		breaksw

	case "-M":
		set print_menu="OFF"
		breaksw

	case "-Q":
		set use_qos="OFF"
		breaksw

	case "-q":
		set verbose_level=0
		set verbecho=$gated_script_dir/noecho
		set infoecho=$gated_script_dir/noecho
		set infoechonl=$gated_script_dir/noecho
		breaksw

	case "-v":
		set verbose_level=2
		set verbecho=echo
		set infoecho=echo
		set infoechonl="echo -n"
		$verbecho "info: verbose mode on"
		breaksw

	case "-h":
		 echo "Usage: `basename $0` [-a] [-g] [-h] [-M] [-Q] [-v]"
		 echo "             [-t <test set name>]"
		 echo "             [-s <sequence> <test #>]"
		 echo ""
		 echo "Gated test driver options:"
		 echo "   -a      Indicates AUTO mode - input from file."
		 echo "   -g      Start gated when starting driver"
		 echo "   -h      Print this help list"
		 echo "   -M      Do not print main menu"
		 echo "   -Q      Do not use QOS table when comparing results"
		 echo "   -q      Set quiet mode only problems reported"
		 echo "   -s      Set sequence and test number on startup"
		 echo "   -t      Set test name on startup"
		 echo "   -v      Set verbose mode"
		 exit 0
		 breaksw
	case "-l":
		set logging="ON"
		$infoecho "info: logging events."
		breaksw
	default:
		echo "error: unknown option $argv[$i]"
		set print_usage="ON"
		breaksw
	endsw
	@ i = $i + 1
end

if ( $print_usage == "ON" ) then
	echo "Usage: `basename $0` [-a] [-g] [-h] [-Q] [-v] [-t <test set name>] [-s <sequence> <test #>]"
	echo "       The "-h" option will explain all the options."
	exit 0
endif
#

#
#    Print header, if desired
#
if ( $operating_mode != "AUTOMATIC" ) then
	echo ""
	echo "                      GATED Test Driver"
	echo "                Version 2.41 - August 20, 1996"
	echo "          Copyright 1994, 1995, 1996 Merit Network, Inc."
	echo "" 
endif

if ( ! $?gated_work_dir ) then
	set gated_work_dir="."
	echo 'warning: $gated_work_dir not set, using current directory'
endif

if ( ! $?gated_script_dir ) then
	echo 'warning: $gated_script_dir not set, using current directory'
	set gated_script_dir ="."
endif

if ( ! $?gated_test_dir ) then
	set gated_test_dir=$gated_work_dir
endif

if ( $logging == "ON" ) then
	if( ! $?testg_logfile_name ) then
		echo "warning: logging option enabled without $testg_logfile_name defined, using default"
	else
		rm -f $testg_logfile_name
	endif
endif

if ("$test_name" == "$NULL" ) then
	set working_test_dir = "."
else
	set working_test_dir = $gated_test_dir/$test_name
endif


$verbecho
$verbecho '     --- gated working directory set to "'"$gated_work_dir"'"'
$verbecho '         gated script directory set to  "'"$gated_script_dir"'"'
$verbecho '         gated test directory set to    "'"$gated_test_dir"'"'
$verbecho

#
#    Print test name, sequence, and test number
#
if ( $operating_mode != "AUTOMATIC" ) then
	$infoecho "info: name: $test_name seq: $test_num[1]  test: $test_num[2]"
	if ( $logging == "ON" ) then
		set timestamp = `date '+%m/%d/%y:(%H:%M:%S):'`
		echo "$timestamp Beginning manual testing: $test_name seq: $test_num[1] test: $test_num[2]" >> $testg_logfile_name
	endif				
endif

#
#  If test sequence and number are set, copy over the config file.
#
if ( ("$test_name" != "$NULL") && ("$test_num[1]" != "$NULL") ) then
	#   Copy the configuration file of user's choice into working area.
	set cfg_filename="gated.conf.$test_name.$host.$test_num[1].$test_num[2]"
	set res_filename="expected.$test_name.$host.$test_num[1].$test_num[2]"

	$verbecho "info: cfg file is: $working_test_dir/$cfg_filename"
	$verbecho "info: res file is: $res_filename"

	if ( ! -e $working_test_dir/$cfg_filename ) then
		echo "error: $cfg_filename does not exist, ignoring request"
		set test_num[1]=$NULL
		set test_num[2]=$NULL
	else
		cp $working_test_dir/$cfg_filename $gated_config_file
	endif
	if ( ! -e $working_test_dir/$res_filename ) then
		echo "warning: results file $res_filename does not exist"
	else
		cp $working_test_dir/$res_filename $gated_results_file
	endif
endif

#
#    Clear this node's entry in the synchronization directory
#
$gated_perl_cmd $gated_script_dir/remove_sync_files $sync_file_name
$verbecho ""
$verbecho "info: synchronization file cleared"

#
#    Test to see if gated is running
#
if ( `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` ) then
	sleep 2
	set proc_id = `cat $gated_pid_file`
	$infoecho "info: gated running procid: $proc_id"
else
	if ( $start_gated == "ON" ) then
		$gated_work_dir/gated -f $gated_config_file
		if ( `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` ) then
			sleep 2
			set proc_id = `cat $gated_pid_file`
			$infoecho "info: gated started procid: $proc_id"
			if ( $logging	 == "ON" ) then
				set timestamp = `date '+%m/%d/%y:(%H:%M:%S):'`
				echo "$timestamp Gated successfully started." >> $testg_logfile_name
			endif
		else
			echo "error: gated started but is NOT running."
			echo "error: Run gated outside this program and check"
			echo "error: for errors and/or core file."
			set proc_id = 0
			if ( $logging == "ON" ) then
				set timestamp = `date '+%m/%d/%y:(%H:%M:%S):'`
				echo "$timestamp Gated started but is NOT running.:" >> $testg_logfile_name 
			endif
		endif
	else
		$infoecho "info: gated is not running."
	endif
endif

#
#    Main menu
#
set choice=999

if ( $logging == "ON" & $operating_mode == "AUTOMATIC" ) then
		set timestamp = `date '+%m/%d/%y:(%H:%M:%S):'`
		echo "$timestamp Beginning testing in automatic mode." >> $testg_logfile_name
	endif


while ( "$choice" != "0" )
	#  If running in automatic mode, skip the menu entirely
	if ( $operating_mode == "AUTOMATIC" ) then
		goto ENTER_CHOICE
	endif

	if ( $print_menu == "ON" ) then
		echo ""
		echo "                   Main Menu"
		echo "                   ---------"
		echo "1)  Select a test sequence (1t = display current test)"
		echo "2)  Check gated status"
		echo "3)  Start gated (3c = test current config file only)"
		echo "4)  Kill gated"
		echo "5)  Review the net status (5m = pipe to more)"
		echo "6)  Compare expected test results to routing table"
		echo "7)  Send refresh signal (config file re-read) to gated"
		echo "8)  Flush the routing table"
		echo "9)  Spawn a shell (csh)"
		echo ""
		echo "10) Set the test name (10d = set the test directory)"
		echo "11) Edit the configuration file"
		echo "12) Edit the expected results file"
		echo "13) View the gated log file"
		echo "14) Save the gated log and routing tables to local log files"
		echo "15) Analyze memory trace calls in the log file"
		echo "16) Set wait time for results comparison or between auto tests"
		echo "17) View the parent CFG file"
		echo ""
		echo "20) Status of flags"
		echo "21, \!21) Turn on/off auto comparison - expected results/routing table"
		echo "22, \!22) Turn on/off verbose mode (print more informational messages)"
		echo "23, \!23) Turn on/off synchronization with other nodes"
		echo "24, \!24) Turn on/off use of QOS file when comparing routes"
		echo "25) Set the working test dir (/testdir/machinetype/protocol/s1/s2/testname)"
		echo "26) Sync with other nodes for current test"
		echo "27) Kill gated and wait for it exit (optional arg to kill)"
		echo "28) Launch, compare, quit (test sequence [ delay machine1 [ machine2 ... ]]"
		echo "\!m) Turn off printing of Main Menu"
		echo "m)  Turn on printing of Main Menu"
		echo ""
		echo "0)  Exit this program"
		echo ""
		echo -n "Enter your choice: "
	else
		echo ""
		echo -n "Enter your choice ("m" to print Main Menu): "
	endif
ENTER_CHOICE:
	if ( $os == "Linux" ) then
		set resp_list=$<:q
	else
		set resp_list=$<
	endif
	set resp_list=($resp_list)
	if ( $#resp_list == 0 ) then
		continue
	else
		set choice=$resp_list[1]
	endif

	switch ( "$choice" )
	case "1":
		# If user supplied two args with choice "1", (e.g. 1 3 6)
		# use those two args, else prompt user
		if ( $#resp_list >= 3 ) then
			set test_num=($resp_list[2] $resp_list[3])
		else
			if ( $#resp_list != 1 ) then
				echo "error: bad input"
				breaksw
			endif

			# Prompt user to enter test numbers
			set valid = 0
			while ( ! $valid )
				echo ""
				echo -n "Enter sequence <space> test: "
				if ( $os == "Linux" ) then
					set resp=$<:q
				else
					set resp=$<
				endif
				if ( "$resp" == "" ) then
					break
				endif

				set resp=($resp)
				if ( $#resp != 2 ) then
					echo "error bad input, hit <CR> to quit"
				else
					set test_num=($resp)
					set valid = 1
				endif
			end
		endif

		if ( "$test_num[1]" != "$NULL" ) then
			echo "info: name: $test_name seq: $test_num[1]  test: $test_num[2]"
			if ( $logging == "ON" ) then
				set timestamp = `date '+%m/%d/%y:(%H:%M:%S):'`
				echo "$timestamp Testing: $test_name, sequence: $test_num[1], test: $test_num[2]" >> $testg_logfile_name
			endif
			# Copy the gated configuration file of user's choice into working area.
			set cfg_filename="gated.conf.$test_name.$host.$test_num[1].$test_num[2]"
			set res_filename="expected.$test_name.$host.$test_num[1].$test_num[2]"
			$verbecho "info: cfg file: $working_test_dir/$cfg_filename"
			$verbecho "info: results file: $res_filename"
		endif     

		#   If the configuration file does not exist, ignore and go on.
		if ( ! -e $working_test_dir/$cfg_filename ) then
			echo "warning: $cfg_filename not found, request ignored"
			breaksw
		else
			cp $working_test_dir/$cfg_filename $gated_config_file
		endif
		if ( ! -e $working_test_dir/$res_filename ) then
			echo "warning: $res_filename not found"
		else
			cp $working_test_dir/$res_filename $gated_results_file
		endif

            #
            #   Synchronize and send hangup signal if gated is running.
            #
            if ( $proc_id != 0 ) then
               #   Synchronize with peers, if turned on
               if ( $synchronize_mode == "ON" ) then
		  # syncronize with other nodes
		  $gated_perl_cmd $gated_script_dir/sync_tests $sync_file_name $test_name $test_num[1] $test_num[2] $gated_sync_files
               endif 

               #   If gated has crashed, and we are in automatic mode, exit the program.
                if ( ! `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` ) then
                  if ( $operating_mode == "AUTOMATIC" ) then
                     echo "error: gated has quit, exiting."
                     exit 2
                  else
                     echo "warning:  ** GATED HAS STOPPED RUNNING **"
                     set proc_id = 0
                     breaksw
                  endif
               endif

#
#  TEMPORARY
#
#  If this is px, put in an additional 10 second delay before hangup signal.
#
#if ( "$host" == "px" ) then
#   echo -n "Sleeping additional 10 seconds..."
#   sleep 10
#   echo "done"
#endif

               #   Send a "hangup" signal to gated.  This will cause it to
               #   re-read the configuration file.
		$infoecho "info: hup'ing gated procid: $proc_id"
		kill -HUP $proc_id

               #   If in automatic mode, wait the specified duration before
               #   moving on to the next test.  Do this even if result file
               #   comparison is off (tables need time to resolve changes).
               if ( ($operating_mode == "AUTOMATIC") || ($auto_compare_mode == "ON") ) then
                  if ( $wait_duration > 0 ) then
                     $infoecho "info: Waiting $wait_duration seconds ... "
                     sleep $wait_duration
                     $infoecho "done"
                  endif
               endif

               #   Compare routing table against expected results.
               if ( $auto_compare_mode == "ON" ) then
                  $verbecho "info: comparing route table with expected results."
                  if ( ("$use_qos" == "ON") & ( $logging == "ON" ) ) then
                       	$gated_cmp_script -z -q -l $gated_qos_file $gated_results_file
		  else if ( ("$use_qos" == "ON") & ( $logging == "OFF" ) ) then
		        $gated_cmp_script -z -q $gated_qos_file $gated_results_file
                  else if ( ("$use_qos" == "OFF") & ( $logging == "ON" ) ) then
                        	$gated_cmp_script -z -l $gated_results_file
	          else if ( ("$use_qos" == "OFF") & ( $logging == "OFF" ) ) then
				$gated_cmp_script -z $gated_results_file
                  endif
               	  endif

               #   Reset for synchronization
               if ( $synchronize_mode == "ON" ) then
		  $gated_perl_cmd $gated_script_dir/remove_sync_files $sync_file_name
               endif
            endif   # if gated is running
         endif     # if test numbers are specified
         breaksw

      case "1t":
	echo "info: name: $test_name seq: $test_num[1]  test: $test_num[2]"
	breaksw

      case "2":
		if ( `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` ) then
			sleep 2
			set proc_id = `cat $gated_pid_file`
			echo "info: gated running procid: $proc_id"
		else
			set proc_id = 0
			echo "info: gated is not running."
		endif
		breaksw

      case "3":
         if ( `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` ) then
		sleep 2
		set proc_id = `cat $gated_pid_file`
		echo "warning: gated already running procid: $proc_id"
         else
		$gated_work_dir/gated -f $gated_config_file

		# Allow time for the process to register
		sleep 2
		if ( `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` ) then
			sleep 2
			set proc_id = `cat $gated_pid_file`
			$infoecho "info: gated started procid: $proc_id"
		else

			echo "error: gated started but is NOT running."
			echo "error: Run gated outside this program and check"
			echo "error: for errors and/or core file."
			set proc_id = 0
		endif
	endif
	breaksw

      case "3c":
         $gated_work_dir/gated -N -c -f $gated_config_file
         breaksw

      case "4":
         if ( $#resp_list >= 2 ) then
            set use_signal=$resp_list[2]
	 else
	    set use_signal="TERM"
	 endif
         if ( `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` ) then
	    sleep 2
            set proc_id = `cat $gated_pid_file`
            kill -$use_signal $proc_id
            $infoecho "info: gated process $proc_id killed."
	     if ( $logging == "ON" ) then
		set timestamp = `date '+%m/%d/%y:(%H:%M:%S):'`
		echo "$timestamp gated process $proc_id killed" >> $testg_logfile_name
	     endif	
           set proc_id = 0
         else
            echo "warning: no gated process to kill."
         endif
         breaksw

      case "5":
         netstat -rn
         breaksw

      case "5m":
         netstat -rn | more
         breaksw

      case "6":
         if ( ("$use_qos" == "ON") & ( $logging == "ON" ) ) then
              	$gated_cmp_script -z -q -l $gated_qos_file $gated_results_file
	  else if ( ("$use_qos" == "ON") & ( $logging == "OFF" ) ) then
	        $gated_cmp_script -z -q $gated_qos_file $gated_results_file
          else if ( ("$use_qos" == "OFF") & ( $logging == "ON" ) ) then
               	$gated_cmp_script -z -l $gated_results_file
          else if ( ("$use_qos" == "OFF") & ( $logging == "OFF" ) ) then
		$gated_cmp_script -z $gated_results_file
          endif
         breaksw

      case "7":
         if ( `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` ) then
	    sleep 2
            set proc_id = `cat $gated_pid_file`
            kill -HUP $proc_id
            $infoecho "info: HUP signal sent to gated  procid $proc_id"
         else
            echo "error: gated is not running."
         set proc_id = 0
         endif
         breaksw

      case "8":
	 if ( $host == "espresso" ) then
            echo "warning: This is Espresso-  routes not flushed."
         else if ( `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` ) then
	    sleep 2
            set proc_id = `cat $gated_pid_file`
            echo "error: gated is running, routes not flushed."
         else

            $infoecho "info: flushing routing table..."
            $gated_script_dir/route_flush.csh
         endif
         breaksw

      case "9":
         /bin/csh -f
         breaksw

      case "10":
         if ( $#resp_list >= 2 ) then
            set resp=$resp_list[2]
         else
            echo "info: current test name is $test_name"
            echo -n "Enter new test name (<CR> to leave alone): "
            set resp=$<
	    if ( $os == "Linux" ) then
	 	set resp=$<:q
	    else
		set resp=$<
	    endif
         endif
         if ( "$resp" != "" ) then
            set test_name=$resp
            set working_test_dir = $gated_test_dir/$test_name
#           echo "info: new working test dir is $working_test_dir"
            set test_num=($NULL $NULL)
		$infoecho "info: name: $test_name seq: $test_num[1]  test: $test_num[2]"
		    if ( $logging == "ON" ) then
			set timestamp = `date '+%m/%d/%y:(%H:%M:%S):'`
			echo "$timestamp name: $test_name seq: $test_num[1] test: $test_num[2]" >> $testg_logfile_name
		    endif	
         endif
         breaksw

      case "10d":
         if ( $#resp_list >= 2 ) then
            set resp=$resp_list[2]
         else
            echo "info: current test directory is $gated_test_dir"
            echo ""
            echo -n "Enter new test directory (<CR> to leave alone): "
	    if ( $os == "Linux" ) then
	 	set resp=$<:q
	    else
		set resp=$<
	    endif
         endif
         if ( "$resp" != "" ) then
            set gated_test_dir = $resp
            $infoecho "info: new gated test dir is $gated_test_dir"
            if ( "$test_name" != "$NULL" ) then
               set working_test_dir = $gated_test_dir/$test_name
            else
               set working_test_dir = $gated_test_dir
            endif

            $infoecho "info: new working test dir is $working_test_dir"
         endif
         breaksw

      case "11":
         $EDITOR $gated_config_file
         breaksw

      case "12":
         $EDITOR $gated_results_file
         breaksw

      case "13":
         $view_cmd $gated_mainlog_dir/$gated_logfile_name
         breaksw
      case "14":
         if ( "$test_num[1]" == "$NULL" ) then
            echo "error: test sequence currently defined"
         else
            set datenum = `date +"%m%d"`
            set seq = $test_num[1]
            set num = $test_num[2]
            set from_file = $gated_mainlog_dir/$gated_logfile_name
            set to_file   = $gated_locallog_dir/$gated_logfile_name.$host.$test_name.$seq.$num.$datenum
            #
            #  NOTE: have to use "$status" here because BSDI, contrary to the
            #        man page, does not return proper exit code from cp 
            #        (returns null in any situation).
            cp $from_file $to_file
            if ( $status == 0 ) then
               $verbecho "info: $from_file copied to $to_file"
            else
               echo "error: copying log file"
            endif
            #
            # Now save an image of the routing tables.
            set to_file = $gated_locallog_dir/netstat.$host.$seq.$num.$datenum
            netstat -rn >$to_file
            if ( $status == 0 ) then
               $verbecho "info: outing tables copied to $to_file"
            else
               echo "error: copying routing table to file"
            endif
         endif
         breaksw

      case "15":
         netstat -m
         $verbecho "info: searching log file for MEM_TRACE calls"
         $gated_script_dir/analyze_memory.csh
         breaksw

      case "16":
         if ( $#resp_list >= 2 ) then
            set resp=$resp_list[2]
         else
            echo -n "Enter wait duration (<CR>=$wait_duration): "
	    if ( $os == "Linux" ) then
	 	set resp=$<:q
	    else
		set resp=$<
	    endif
         endif
         if ( "$resp" != "" ) then
            set wait_duration=$resp
         endif
         $infoecho "info: Wait duration now: $wait_duration seconds"
         breaksw

      case "20":
         $infoecho "info: Automatic comparison mode is $auto_compare_mode"
         if ( $auto_compare_mode == "ON" ) then
		 $infoecho "info: Wait duration now: $wait_duration seconds"
         endif
         $infoecho "info: verbose level is $verbose_level"
         $infoecho "info: sync mode is $synchronize_mode"
         if ( $synchronize_mode == "ON" ) then
            $verbecho "info: sync directory: $gated_sync_dir"
            $infoecho "info: nodes are: $gated_sync_nodes"

         endif
         $infoecho "info: QOS file referencing is $use_qos"
         breaksw

      case "17":
         if ( "$test_name" == "$NULL" ) then
           echo "error: No test name currently defined"
         else
           $view_cmd $working_test_dir/$test_name.cfg
         endif
         breaksw

      case "21":
         set auto_compare_mode="ON"
         $infoecho "info: automatic comparison mode turned ON"
	 $infoecho "info: wait duration now: $wait_duration seconds"
         breaksw

      case "!21":
         set auto_compare_mode="OFF"
         $infoecho "info: automatic comparison mode turned OFF"
         breaksw

      case "22":
         set verbose_level=2
	 set verbecho=echo
         $infoecho "info: verbose mode turned ON"
         breaksw

      case "!22":
         set verbose_level=1
	 set verbecho=$gated_script_dir/noecho
         $infoecho "info: verbose mode turned OFF"
         breaksw

      case "23":
         set synchronize_mode="ON"
         if ( $#resp_list >= 2 ) then
            set resp=( $resp_list[2-] )
         else
            echo -n "Enter node names to synchronize with: "
	    if ( $os == "Linux" ) then
	 	set resp=$<:q
	    else
		set resp=$<
	    endif
         endif
         set gated_sync_nodes=( $resp )
         $infoecho "info: sync on"
         $verbecho "info: sync directory: $gated_sync_dir"
         $infoecho "info: nodes: $gated_sync_nodes"

	 set gated_sync_files=""
	 foreach node ($gated_sync_nodes)
		set gated_sync_files = ($gated_sync_files $gated_sync_dir/$node.lock)
	 end
         breaksw

      case "!23":
         set synchronize_mode="OFF"
	 $gated_perl_cmd $gated_script_dir/remove_sync_files $sync_file_name
         $infoecho "info: synchronization mode turned OFF"
         breaksw

      case "24":
         set use_qos="ON"
         $infoecho "info: QOS file referencing turned ON"
         breaksw

      case "!24":
         set use_qos="OFF"
         $infoecho "info: QOS file referencing turned OFF"
         breaksw
      case "25":
         # This is basically a combination of choices 10 and 10d.  This better
         # suits Merit's directory organization of
         #       $gated_test_dir/<machine_type>/<protocol>/<level1>/<level2>/$test_name
	 # (level1 and level2 are not always present)

         # If user supplied at least three args with choice "25",
         # (e.g. 25 suns idrp 2suns or 25 suns bgp dpa dpa_syntax export)
         # use those args, else prompt user
         if ( $#resp_list >= 4 ) then
            # make the last item in the list the test_name
            set test_name=$resp_list[$#resp_list]
            set machine_type=$resp_list[2]
            # convert sun --> suns and pc --> pcs
            if ( $machine_type == "sun") then
               set machine_type = "suns"
            endif
            if ( $machine_type == "pc") then
               set machine_type = "pcs"
            endif
            set protocol=$resp_list[3]

            if ( $#resp_list == 4 ) then
               set working_test_dir = $gated_test_dir/$machine_type/$protocol/$test_name
               $infoecho "info: new working test dir is $working_test_dir"
            endif

            if ( $#resp_list == 5 ) then
               set sub1 = $resp_list[4]
               set working_test_dir = $gated_test_dir/$machine_type/$protocol/$sub1/$test_name
               $infoecho "info: new working test dir is $working_test_dir"
            endif

            if ( $#resp_list == 6 ) then
               set sub1 = $resp_list[4]
               set sub2 = $resp_list[5]
               set working_test_dir = $gated_test_dir/$machine_type/$protocol/$sub1/$sub2/$test_name
               $infoecho "info: new working test dir is $working_test_dir"
            endif
            
         else
            if (( $#resp_list != 1 ) || ( $#resp_list > 6 ))  then
               echo "error: bad input"
               breaksw
            endif

            # Prompt user to enter the info
            set valid = 0
            while ( ! $valid )
	       if ("$test_name" != "$NULL") then
	         $infoecho "info: current test directory is $working_test_dir"
	       else
		 $infoecho "info: current test directory is $gated_test_dir"
	       endif
	       echo "<CR> to leave alone"
               echo "<sub1> and/or <sub2> are optional"
               echo -n "Enter <machine type> <protocol> <sub1> <sub2> <test name>: "
		if ( $os == "Linux" ) then
			set resp=$<:q
		else
			set resp=$<
		endif
               if ( "$resp" == "" ) then
                  break
               endif

               set resp=($resp)
               if (( $#resp < 3 ) || ( $#resp > 5 )) then
                  echo "error: bad input, hit <CR> to quit"
               else
                  # make the last item in the list the test_name
                  set test_name=$resp[$#resp]
		  set machine_type=$resp[1]
                  # pc --> pcs , sun --> suns
                  if ( $machine_type == "sun" ) then
                     set machine_type = "suns"
                  endif
                  if ( $machine_type == "pc" ) then
                     set machine_type = "pcs"
                  endif
		  set protocol=$resp[2]
               
                  if ( $#resp == 3 ) then
                     set working_test_dir = $gated_test_dir/$machine_type/$protocol/$test_name
                     $infoecho "info: new working test dir is $working_test_dir"
                     set valid = 1 
                  endif

                  if ( $#resp == 4 ) then
                     set sub1 = $resp[3]
                     set working_test_dir = $gated_test_dir/$machine_type/$protocol/$sub1/$test_name
                     $infoecho "info: new working test dir is $working_test_dir"
                     set valid = 1
                  endif

                  if ( $#resp == 5 ) then
                     set sub1 = $resp[3]
                     set sub2 = $resp[4]
                     set working_test_dir = $gated_test_dir/$machine_type/$protocol/$sub1/$sub2/$test_name
                     $infoecho "info: new working test dir is $working_test_dir"
                     set valid = 1
                  endif

               endif
            end
         endif
         breaksw

      case "26":
	 # sync up with other testers for this test
	 if ( $synchronize_mode != "ON" ) then
		echo "error: must have syncronization turned on"
	 else
		if ( $#resp_list >= 2 ) then
			set use_test_name=$resp_list[2]
		else
			set use_test_name=$test_name
		endif
		$gated_perl_cmd $gated_script_dir/sync_tests $sync_file_name $use_test_name "no_test_number" "no_sequence" $gated_sync_files
	        if ( $logging == "ON" ) then
			set timestamp = `date '+%m/%d/%y:(%H:%M:%S):'`
			echo "$timestamp nodes are: $gated_sync_nodes" >> $testg_logfile_name
		        endif
	 endif
	 breaksw

	case "27":
		if ( $#resp_list >= 2 ) then
			set use_signal=$resp_list[2]
		else
			set use_signal="TERM"
		endif
                if ( ! `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` ) then
			$infoechonl "info: killing $proc_id "
		endif
		while ( `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` )
			sleep 2
			if ( -e $gated_pid_file ) then
				set proc_id = `cat $gated_pid_file`
				$infoechonl "."
				kill -TERM $proc_id
			endif
			set proc_id = 0
		end
		$infoecho "killed."
		breaksw
	##
	## much copied from "1" 
	case "28":
		# require user input with commands
		if ( $#resp_list < 3 ) then
			echo "error: bad input, test and seq required"
			breaksw
		endif
		# seconds
		if ( $#resp_list > 4 ) then
			set machine_launch_delay=$resp_list[4];
			set machines=($resp_list[5-])
		endif

		set test_num=($resp_list[2] $resp_list[3])

		if ( "$test_num[1]" != "$NULL" ) then
			echo "info: name: $test_name seq: $test_num[1] test: $test_num[2]"

			# Copy the gated configuration file of user's
			# choice into working area.
			set cfg_filename="gated.conf.$test_name.$host.$test_num[1].$test_num[2]"
			set res_filename="expected.$test_name.$host.$test_num[1].$test_num[2]"
			$verbecho "info: cfg file: $working_test_dir/$cfg_filename"
			$verbecho "info: results file: $res_filename"
		endif     

		#   If the configuration file does not exist
		if ( ! -e $working_test_dir/$cfg_filename ) then
			echo "error: $cfg_filename not found, request ignored"
			breaksw
		else
			cp $working_test_dir/$cfg_filename $gated_config_file
		endif
		if ( ! -e $working_test_dir/$res_filename ) then
			echo "error: $res_filename not found"
			breaksw
		else
			cp $working_test_dir/$res_filename $gated_results_file
		endif

		# Synchronize 
		#
		if ( $synchronize_mode != "ON" ) then
			echo "error: option 28 for syncronize mode only"
		endif

		# syncronize with other nodes
		$gated_perl_cmd $gated_script_dir/sync_tests $sync_file_name $test_name $test_num[1] $test_num[2] $gated_sync_files

		if ($?machines) then
			# launch gated in $machines[*] order pausing $machine_delay after each launch
			#
			foreach launch_machine ($machines[*]) 
				if ($host == $launch_machine) then
					echo "launching for $launch_machine"
					if ( `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` ) then
						sleep 2
						set proc_id = `cat $gated_pid_file`
						echo "warning: gated already running procid: $proc_id"
						breaksw
					else
						$gated_work_dir/gated -f $gated_config_file

						# wait for the process to register a pid file
						while ( ! -e $gated_pid_file )
							if ( ! `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet`) then
								break
							endif
							sleep 1
						end
						if ( `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` ) then
							set proc_id = `cat $gated_pid_file`
							$infoecho "info: gated started procid: $proc_id"
						else
							echo "error: gated started but is NOT running."
							echo "error: Run gated outside this program and"
							echo "error: check for errors and/or core file."
							set proc_id = 0
						endif
					endif
					$infoecho "info: sleeping $machine_launch_delay"
					sleep $machine_launch_delay
					$gated_perl_cmd $gated_script_dir/sync_tests $sync_file_name "${test_name}wait_for_${launch_machine}" $test_num[1] $test_num[2] $gated_sync_files
				else
					$gated_perl_cmd $gated_script_dir/sync_tests $sync_file_name "${test_name}wait_for_${launch_machine}" $test_num[1] $test_num[2] $gated_sync_files
				endif
			end
		else
			# launch gated
			#
			if ( `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` ) then
				sleep 2
				set proc_id = `cat $gated_pid_file`
				echo "warning: gated already running procid: $proc_id"
				breaksw
			else
				$gated_work_dir/gated -f $gated_config_file

				# wait for the process to register a pid file
				while ( ! -e $gated_pid_file )
					if ( ! `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` ) then
						break
					endif
					sleep 1
				end
				if ( `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` ) then
					set proc_id = `cat $gated_pid_file`
					$infoecho "info: gated started procid: $proc_id"
				else
					echo "error: gated started but is NOT running."
					echo "error: Run gated outside this program and"
					echo "error: check for errors and/or core file."
					set proc_id = 0
				endif
			endif

			# syncronize wait with other nodes
			$gated_perl_cmd $gated_script_dir/sync_tests $sync_file_name "${test_name}wait_for_propagation" $test_num[1] $test_num[2] $gated_sync_files
		endif

		unset machines

		# comment about px (bsdi 2.0 machine) requiring
		# 10 extra seconds before hup removed

		# If in automatic mode, wait the specified duration before
		# moving on to the next test.  Do this even if result file
		# comparison is off (tables need time to resolve changes).
		if (($operating_mode != "AUTOMATIC") || ($auto_compare_mode != "ON") ) then
			echo "error: 28 used in auto with auto compare only"
		endif
		if ( $wait_duration <= 0 ) then
			echo "error: wait delay must be larger than 0"
		endif
		echo "info: Waiting $wait_duration seconds ... "
		sleep $wait_duration
		$infoecho "done"

		# Compare routing table against expected results.
		$verbecho "info: comparing route table with expected results."
		if ( "$use_qos" == "ON" ) then
			$gated_cmp_script -z -q $gated_qos_file $gated_results_file
		else
			$gated_cmp_script -z $gated_results_file
		endif

		# syncronize end with other nodes
		$gated_perl_cmd $gated_script_dir/sync_tests $sync_file_name "${test_name}end_of_test" $test_num[1] $test_num[2] $gated_sync_files

		#   Reset for synchronization
		$gated_perl_cmd $gated_script_dir/remove_sync_files $sync_file_name
		endif

		# kill gated
		#
		while ( `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` )
			sleep 2
			if ( -e $gated_pid_file ) then
				set proc_id = `cat $gated_pid_file`
				kill -TERM $proc_id
			endif
			set proc_id = 0
		end

		# flush kernel forwarding table.
		#
		if ( $host == "espresso" ) then
			echo "warning: This is Espresso-  routes not flushed."
		else
			$infoecho "info: flushing routing table..."
			$gated_script_dir/route_flush.csh >/dev/null
			$infoecho "done"
		endif
		breaksw
      case "!m":
         set print_menu="OFF"
         breaksw

# added this 6/11/96 - i'm always leaving my finger on the shift key...
      case "!M":
         set print_menu="OFF"
         breaksw

      case "m":
         set print_menu="ON"
         breaksw

      case "#":
         # comment (probably in an auto test script) - ignore
         breaksw

      case "0":
         if ( $operating_mode == "AUTOMATIC" ) then
            $infoecho "info: exiting testg ..."
            if ( `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` ) then
	       sleep 2
               set proc_id = `cat $gated_pid_file`
               $infoecho "info: gated is still running,  procid = $proc_id"
            endif
         endif

         # Clear this node's synchronization file
	 $gated_perl_cmd $gated_script_dir/remove_sync_files $sync_file_name
         breaksw

      default:
         echo "error: bad input, please try again"
         breaksw
   endsw
end
#

exit 0

USERBREAK:

if ( $logging == "ON" ) then
	set timestamp = `date '+%m/%d/%y:(%H:%M:%S):'`
	echo "$timestamp Caught interrupt signal.  Exiting." >> $testg_logfile_name
endif				



#
# kill gated
# 
echo "killing gated if running..."
while ( `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` )
	sleep 2
	if ( -e $gated_pid_file ) then
		set proc_id = `cat $gated_pid_file`
		kill -TERM $proc_id
		$infoecho "info: gated process $proc_id killed."
	endif
	set proc_id = 0
end
#
# flush routes 
#
if ( $host == "espresso" ) then
	echo "warning: This is Espresso-  routes not flushed."
else if ( `$ps_cmd | grep 'gated ' | grep -v grep | grep -v rlogin | grep -v ssh | grep -c -v telnet` ) then
	sleep 2
	set proc_id = `cat $gated_pid_file`
	echo "error: gated is running, routes not flushed."
else
	$infoecho "info: flushing routing table..."
	$gated_script_dir/route_flush.csh
endif

exit 1
