#! /bin/csh -f  
# @(#)sysdiag.sh	1.17	11/21/88
#
#
# Sysdiag :
#
#   System Diagnostic control program
#	Please update revision history and revision string
#	if making a change. 
#
#   1-Aug-88
#
#   Revision history:
#
#   11/16/90	version 2.6 	its
#		added floppy disk test
#   12/7/90	version 2.7 	its
#		added window execution delays
#		disk test now will run on /usr/tmp
#   1/29/91	version 2.8 	its
#		fixed fbtype.c to run with sga20
#   2/28/91	version 2.9 	its
#		added fpui (fpu ieee compatibility) test 
#

set version = "3.0  Sep 15, 1992"   # version number of this source

#initializations
#
unset source_file   # source file for -f option
unset windows	    # run under window system?
unset dryrun	    # dryrun only?
unset force_dryrun  # if "-d" option, force dryrun
set extra	    # collects extra arguments for stress tests
set PRE_MENU	    # bring up menu before invoking tests
set POST_MENU	    # bring up menu after invoking tests
set POST_MORE	    # just do UNIX more(1) on logfiles after tests
set LOOP	    # loop thru tests
set RMONtime = 60   # default run-time for resource monitor


# delay WAIT_CNT before starting tests
set W0_WAIT_CNT = 0
set W1_WAIT_CNT = 0 
set W2_WAIT_CNT = 0
set W3_WAIT_CNT = 0

set PROCS = ()      
umask 0
set null
if ($?SYSD_LOGDIR) then
    set LOGDIR = $SYSD_LOGDIR
else
    set LOGDIR = /usr/adm/sysdiaglog
endif
set SYSDIAG_DIR = /usr/diag/sysdiag
set path = ($path $SYSDIAG_DIR)
#
# get args
#
unset next	# marks next argument in sequence
foreach i ($argv) #{ 
    if ($?next) then #{
	set $next = $i
	unset next 
    #}
    else if ($i:x == '-h') then #{
	echo "usage: sysdiag <options>:"
	echo "        -s................run under suntools"
	echo "        -f <file>.........load pre-saved sysdiag settings"
	echo "        -c<options>.......pass options to all stress tests"
	echo "        -d................dryrun, print but do not exec tests"
	echo "        -l <logdir>.......set logdir <default $LOGDIR>"
	echo "        -h................this message"
	exit 
    #}
    else  if ($i:x =~ -c*) then
	 set extra = `echo $i:x | sed 's/-c//'`
    else if ($i:x == '-s') then
	 set windows
    else if ($i:x == '-d') then
	set force_dryrun
    else if ($i:x == '-f') then
        set next = source_file
    else if ($i:x == '-l') then
        set next = LOGDIR
    else 
	echo unknown option: $i:x
    endif 
end #}

setenv SYSD_VERBOSE 7	# prefix output with originating module
setenv SYSD_ERR_RUN	# continue running on error

setenv SYSD_LOGDIR $LOGDIR

# stress test modules
set MNTR
set PMEM
set VMEM
set DISK
set TAPET     # tape test
set ENET
set FPUM
set FPUI
set STRESS
# these are off by default
unset FPUP
unset PORT    # serial port
unset MUX     # async mux card
@ NMUX = 0
unset KEYMS   # keyboard/mouse
unset CRT     # video test
unset VME     # VMEram test
unset FLOP    # Floppy disk test

# window labels
set W0l = '"memory/cpu tests"'
set W1l = '"peripheral tests"'
set W2l = '"enet/port tests"'
set W3l = '"disk read test"'
set CSl = '"System diagnostic console"'
set PFl = '"Performance monitor"'
set MNl = '"monitor"'

# window icon labels
set W0L = '"memory"'
set W1L = '"enet"'
set W2L = '"periph"'
set W3L = '"disk"'
set CSL = '"console"'
set PFL = '"perfmon"'
set MNL = '"monitor"'

# window {x,y} positions
set W0p = "000 000"
set W1p = "000 300"
set W2p = "000 600"
set W3p = "570 250"
set CSp = "570 000"
set PFp = "570 550"
set MNp = "570 250"
set MNP = "570 250"
set CLp = "1000 300"
set CLP = "1080 300"
set KMp = "900  300"
set KMP = "1000 300"

# window {x,y} sizes
set W0s = "570 300"
set W1s = "570 300"
set W2s = "570 300"
set W3s = "506 300"
set CSs = "580 250"
set PFs = "580 350"
set MNs = "300 100"
set CLs = "100 060"

set white = "252 252 252"
set black = "000 000 000"

# determine if /dev/fb is color or monochrome
if (`fbtype` == color) then #{
	set COLOR
	# enable color windows
	set red = "255 000 000"
	set green = "000 255 000"
	set blue = "000 000 255"
	set yellow = "255 255 000"
	set cyan = "000 255 255"
	set magenta = "255 000 255"
	set turquoise = "112 147 219"
	set grey = "220 220 220"

	# window forground colors
	set W0fc = "$red"
	set W1fc = "$green"
	set W2fc = "$blue"
	set W3fc = "$cyan"
	set CSfc = "$yellow"
	set PFfc = "$magenta"
	set MNfc = "$grey"
	set CLfc = "$turquoise"

	# window background colors
	set W0bc = "$white"
	set W1bc = "$black"
	set W2bc = "$white"
	set W3bc = "$black"
	set CSbc = "$black"
	set PFbc = "$black"
	set MNbc = "$black"
	set CLbc = "$white" 
#}

else  #{ force black and white
	unset COLOR
	# window forground colors
	set W0fc = "$black"
	set W1fc = "$black"
	set W2fc = "$black"
	set W3fc = "$black"
	set CSfc = "$black"
	set PFfc = "$black"
	set MNfc = "$black"
	set CLfc = "$black"

	# window background colors
	set W0bc = "$white"
	set W1bc = "$white"
	set W2bc = "$white"
	set W3bc = "$white"
	set CSbc = "$white"
	set PFbc = "$white"
	set MNbc = "$white"
	set CLbc = "$white" 
#}
endif

# miscellaneous settings
set TMP = /tmp
set TMPFILE_PREFIX = Sysdiag_Tmp_
set SHTL = 'shelltool	    '
set CMTL = 'cmdtool	    '
set CLTL = 'clock -d mda    '
set ICON = '-WP -1 -1'
set quote = '"'
set CMD = "csh -c $quote sysdiag -f"

# set up performance monitor 
if (-e /usr/old) then #{
    # sun4, where sun is trying to get rid of perfmon
    set PERF = /usr/old/perfmon
#}
else #{
    # sun3
    set PERF = perfmon 
endif #}

set PERFMON = "$PERF free idle user system disk interrupt input output"

# common settings for stress test arguments
#
set CMS = "-l-1 -z $extra"

# test-specific argument settings. 
#
#   each test may be selectively turned off by 
#   unsetting the appropriate variable from below
#   or, a class of tests (e.g. DISK rather than DISKd)
#   may be turned off by unsetting the (DISK) class variable
#
#set MNTRt = ""
unset MNTRt =
set PMEMt = ""
set VMEMp = "-p -s40000"
set VMEMf = "-f4 -b -c2000"
set FPUm  = "-s32"
set FPUp  = ""			# floating point pipeline test
set FPUi  = ""			# floating point IEEE test
set MUXt  = "-m1 -b1"			# MUX card test
set DISKd = "-d -s500000"
set DISKr = "-frl-1"
set ENETs = "-stream"
set ENETd = "-dgram"
set PORTt = "/dev/ttya /dev/ttyb"
set UNIXSCDt = "-d20"
set KEYMSt = ""
set VMEt = "-o100000 -s80000"
set FLOPt = "-e"

#
# If input source file specified, read it and redefine the
# previous (default) test settings 
#
if ($?source_file) then
    source $source_file
endif

#
# Change window positions and sizes if window.config file exists
#
if (-e window.config) then
    source window.config
endif

set L1 = "	   "
set L2 = "         "
set L3 = "         "

#
# loop thru tests & menus
#
start:
while (1) #{

# if command line "-d" option was entered, skip pre-menu and go directly 
# into "dryrun" of selected stress tests

if ($?force_dryrun) then #{
	set dryrun
	unset PRE_MENU 
endif #}

if ($?PRE_MENU) then #{

    while (1) #{
        set ans = ''
        clear
        echo "$L1 Solbourne System Diagnostics"
	echo "$L1 Version $version"
        echo "$L1 Copyright (c) 1990 Solbourne Computer, Inc."
        echo "$L1   "
        echo "$L1 t....set tests"
        echo "$L1 o....output configuration to file"
        echo "$L1 i....input configuration from file"
        echo "$L1 l....adjust log file settings"
        echo "$L1 z....adjust various other options"
        echo "$L1 r....run sysdiag tests"
        echo "$L1 q....quit this menu"
        echo -n "$L1 ==> "
    
        set k = $<
        set ans = ($k)
    
        if ("$ans" == "t") then #{
    
            while (1) #{
    	        unset wait
    	        set MNTRstate = "is not"
    	        set PMEMstate = "is not"
    	        set VMEMstate = "is not"
    	        set DISKstate = "is not"
    	        set TAPEstate = "is not"
    	        set ENETstate = "is not"
    	        set PORTstate = "is not"
    	        set MUXstate = "is not set"
    	        set FPUMstate  = "is not"
    	        set FPUPstate  = "is not"
    	        set FPUIstate  = "is not"
    	        set STRESSstate  = "is not"
    	        set KEYMSstate  = "is not"
    	        set VMEstate = "is not"
    	        set FLOPstate = "is not"
            
    	        if ($?MNTR) set MNTRstate = "is"
    	        if ($?PMEM) set PMEMstate = "is"
    	        if ($?VMEM) set VMEMstate = "is"
    	        if ($?DISK) set DISKstate = "is"
    	        if ($?TAPET) set TAPEstate = "is"
    	        if ($?ENET) set ENETstate = "is"
    	        if ($?PORT) set PORTstate = "is"
    	        if ($?MUX) set MUXstate = "is set <$NMUX controllers>"
    	        if ($?FPUM) set FPUMstate = "is"
    	        if ($?FPUP) set FPUPstate = "is"
    	        if ($?FPUI) set FPUIstate = "is"
    	        if ($?STRESS) set STRESSstate = "is"
    	        if ($?KEYMS) set KEYMSstate = "is"
    	        if ($?VME) set VMEstate = "is"
    	        if ($?FLOP) set FLOPstate = "is"
            
    	        clear

    	        echo "$L2 Sysdiag stress test menu selection"
                echo "$L2   "
#		echo "$L2 m..... monitor $MNTRstate set"
    	        echo "$L2 p..... pmem $PMEMstate set"
    	        echo "$L2 v..... vmem $VMEMstate set"
    	        echo "$L2 f..... FPU matrix $FPUMstate set"
#		echo "$L2 W..... FPU pipe $FPUPstate set"
		echo "$L2 i..... FPU IEEE $FPUIstate set"
    	        echo "$L2 d..... disk-r/w/c $DISKstate set"
    	        echo "$L2 s..... disk-read $STRESSstate set"
    	        echo "$L2 t..... tape $TAPEstate set"
    	        echo "$L2 e..... enet $ENETstate set"
    	        echo "$L2 o..... port $PORTstate set"
    	        echo "$L2 a..... async MUX $MUXstate"
    	        echo "$L2 k..... key/ms $KEYMSstate set <suntools only>"
    	        echo "$L2 V..... VME RAM $VMEstate set"
    	        echo "$L2 F..... floppy disk $FLOPstate set"
        
                echo "$L2 q..... quit this menu"
                echo -n "$L2 ==> "
        
                set k = $<
                set ans = ($k)

                if ("$ans" == "m" && ans != m) then  #{
    	            if ($?MNTR) then
    		        unset MNTR
    	            else
    		        set MNTR
    	            endif 
        
                else if ("$ans" == "p") then 
    	            if ($?PMEM) then
    		        unset PMEM
    	            else
    		        set PMEM
    	            endif 
        
                else if ("$ans" == "v") then
    	            if ($?VMEM) then
    		        unset VMEM
    	            else
    		        set VMEM
    	            endif 
        
                else if ("$ans" == "f") then 
    	            if ($?FPUM) then
    		        unset FPUM
    	            else
    		        set FPUM
    	            endif 
		
                else if ("$ans" == "W") then 
    	            if ($?FPUP) then
    		        unset FPUP
    	            else
    		        set FPUP
    	            endif 
        
                else if ("$ans" == "i") then 
    	            if ($?FPUI) then
    		        unset FPUI
    	            else
    		        set FPUI
    	            endif 
        
                else if ("$ans" == "d") then 
    	            if ($?DISK) then
    		        unset DISK
    	            else
    		        set DISK
    	            endif 
		
        
                else if ("$ans" == "t") then 
    	            if ($?TAPET) then
    		        unset TAPET
    	            else
    		        set TAPET
    	            endif 
		
        
                else if ("$ans" == "e") then 
    	            if ($?ENET) then
    		        unset ENET
    	            else
    		        set ENET
    	            endif 
		
        
                else if ("$ans" == "o") then 
    	            if ($?PORT) then
    		        unset PORT
    	            else
    		        set PORT
    	            endif 
		
        
                else if ("$ans" == "a") then 
    	            if ($?MUX) then
    		        unset MUX
    	            else #{
			echo ""
			echo -n "How many MUX cards are installed? "
    		        set k = $<
			if ($k =~ [1-4]) then
			    set MUX
			    set NMUX = $k
			else
			    echo "  Only 1 to 4 MUX cards can be tested"
			    echo "  MUX test deselected"
			    unset MUX
			    set wait
			endif
		    endif #}
		
        
                else if ("$ans" == "s") then 
    	            if ($?STRESS) then
    		        unset STRESS
    	            else
    		        set STRESS
    	            endif 
		
        
                else if ("$ans" == "k") then 
    	            if ($?KEYMS) then
    		        unset KEYMS
    	            else
    		        set KEYMS
    	            endif 
		
        
    	        else if ($ans == V) then 
    	            if ($?VME) then
                        unset VME
                    else
                        set VME
    	            endif 
		
    	        else if ($ans == F) then 
    	            if ($?FLOP) then
                        unset FLOP
                    else
                        set FLOP
    	            endif 
        
                else if ("$ans" == "q") then
    	            break

    	        else
		    echo ""
    	            echo "unknown selection: $ans"
    	            set wait

    	        endif #}
        
    	        if ($?wait) then
    	            echo -n "press RETURN to continue ==> "
    	            set ans = $<
    	            unset wait  
    	        endif 
        
            end #} while loop for 't' sub menu
    
        #} 

        else if ("$ans" == "o") then #{

    	    echo -n "$L2 enter file name to save configuration ==> "
    	    set k = $<
    	    if (!($k == $null)) then #{
    		set file = ($k)
    		source save_config  
            endif #}
        #}

        else if ("$ans" == "i") then #{
    	    echo -n "$L2 enter file name to load configuration ==> "
    	    set k = $<
    	    if (!($k == $null)) then #{
    		set file = ($k)
    		if (!(-e $file)) then #{
    	    	    echo $file does not exist
    	    	    set wait
            	#}
    		else if (!(-f $file)) then #{
    	      	    echo $file is not a text file
    	    	    set wait
            	#}
    		else if (!(-r $file)) then #{
    		    echo "$file is not readable <permission denied>"
    		    set wait
    		#}
    		else
    	    	    source $file 
    		endif 
    	    endif #}
        #}
    
        else if ("$ans" == "z") then #{
    	    while (1) #{
    	        unset wait
    	        set STstate = "is not"
    	        set DRYRUNstate = "is not"
    	        set LOOPstate = "is not"
    	        set PRE_MENUstate = "is not"
    	        set POST_MENUstate = "is not"
    	        set POST_MOREstate = "is not"
    	        set SYSD_ERR_RUNstate = "is not"
    	        set VERBOSEstate = "is not"
            
    	        if ($?windows) set STstate = "is"
    	        if ($?dryrun) set DRYRUNstate = "is"
    	        if ($?LOOP) set LOOPstate = "is"
    	        if ($?PRE_MENU) set PRE_MENUstate = "is"
    	        if ($?POST_MENU) set POST_MENUstate = "is"
    	        if ($?POST_MORE) set POST_MOREstate = "is"
    	        if ($?SYSD_ERR_RUN) set SYSD_ERR_RUNstate = "is"
    	        if ($?VERBOSE) set VERBOSEstate = "is"
            
    	        clear
    	        echo "$L2 Sysdiag miscellaneous options"
                echo "$L2   "
    	        echo "$L2 s..... suntools $STstate set"
    	        echo "$L2 d..... dryrun (print, do not exec, stress tests) $DRYRUNstate set"
    	        echo "$L2 l..... sysdiag loop (to main menu) $LOOPstate set"
    	        echo "$L2 m..... pre-invocation (option-setting) menu $PRE_MENUstate set"
    	        echo "$L2 p..... post-invocation (logfile-searching) menu $POST_MENUstate set"
    	        echo "$L2 i..... post-invocation UNIX more(1) $POST_MOREstate set"
    	        echo "$L2 e..... 'run on error' $SYSD_ERR_RUNstate set"
    	        echo "$L2 v..... verbose $VERBOSEstate set"
    	        echo "$L2 w..... window execution delay count"
    	        echo "$L2 a..... performance monitoring"
            
                echo "$L2 q..... quit this menu"
                echo -n "$L2 ==> "
    
                set k = $<
                set ans = ($k)

                if ("$ans" == "s") then 
    	            if ($?windows) then
    		        unset windows
    	            else
    		        set windows
    	            endif
    
                else if ("$ans" == "d") then 
    	            if ($?dryrun) then
    		        unset dryrun
    	            else
    		        set dryrun
    	            endif
    
                else if ("$ans" == "l") then 
    	            if ($?LOOP) then
    		        unset LOOP
    	            else
    		        set LOOP
    	            endif 
    
                else if ("$ans" == "m") then
    	            if ($?PRE_MENU) then
    		        unset PRE_MENU
    	            else
    		        set PRE_MENU
    	            endif 
    
                else if ("$ans" == "p") then 
    	            if ($?POST_MENU) then
    		        unset POST_MENU
    	            else
    		        set POST_MENU
    	            endif 
    
                else if ("$ans" == "i") then
    	            if ($?POST_MORE) then
    		        unset POST_MORE
    	            else
    		        set POST_MORE
    	            endif 
    
                else if ("$ans" == "e") then
    	            if ($?SYSD_ERR_RUN) then
    		        unsetenv SYSD_ERR_RUN
    	            else
    		        setenv SYSD_ERR_RUN
    	            endif 
    
                else if ("$ans" == "v") then
    	            if ($?VERBOSE) then
    		        unset VERBOSE
    	            else
    		        set VERBOSE
    	            endif 
    
                else if ("$ans" == "a") then #{
    	            while (1) #{
    	                unset wait
    
    	                set UNIXSCDstate = "is not set"
    	                set RMONstate = "is not set"
    
    	                if ($?UNIXSCD) set UNIXSCDstate = "is set"
    	                if ($?RMON) set RMONstate = "time limit is set at <$RMONtime>"
            
    	                clear
    	                echo "$L3 Sysdiag Performance Monitoring"
    	                echo "$L3   "
    	                echo "$L3 s..... UNIX scheduler monitor $UNIXSCDstate"
    	                echo "$L3 r..... resource monitor $RMONstate"
    	                echo "$L3 q..... quit this menu"
    	                echo -n "$L2 ==> "
            
    	                set k = $<
    	                set ans = ($k)

    	                if ("$ans" == "s") then 
    		            if ($?UNIXSCD) then
    		                unset UNIXSCD
    		            else
    		                set UNIXSCD
    		            endif

    	                else if ("$ans" == "r") then
			    if ($?RMON) then
				unset RMON
			    else
				set RMON
    		                echo -n "$L3 enter desired run time ([[[dd:]hh:]mm:]ss) ==> "
    		                set k = $<
				if ($k != "") then
    		                    set RMONtime = ($k)
				else
				    unset RMON
				endif	
    		            endif

    	                else if ("$ans" == "q") then
    		            break

    	                else
		     	    echo ""
    		            echo "unknown selection: $ans"
    		            set wait
    	                endif 
        
    	                if ($?wait) then
    		            echo -n "press RETURN to continue ==> "
    		            set ans = $<
    		            unset wait
    	                endif
        
    	            end #} while loop for 'a' sub menu 
		#}

                else if ("$ans" == "w") then #{
    	            while (1) #{
    	                unset wait
    
    	                clear
    	                echo "$L3 Window Execution Delays"
    	                echo "$L3   "
    	                echo "$L3 0..... Window 0 delay = $W0_WAIT_CNT"
    	                echo "$L3 1..... Window 1 delay = $W1_WAIT_CNT"
    	                echo "$L3 2..... Window 2 delay = $W2_WAIT_CNT"
    	                echo "$L3 3..... Window 3 delay = $W3_WAIT_CNT"
    	                echo "$L3 q..... quit this menu"
    	                echo -n "$L2 ==> "
            
    	                set k = $<
    	                set ans = ($k)

    	                if ("$ans" == "q") then
				break
			else
				echo -n "enter new count -> "
    		        	set k = $<
    	                	if ("$ans" == "0") then 
					set W0_WAIT_CNT = $k
				else if ("$ans" == "1") then 
					set W1_WAIT_CNT = $k
				else if ("$ans" == "2") then 
					set W2_WAIT_CNT = $k
				else if ("$ans" == "3") then 
					set W3_WAIT_CNT = $k
    	                	else
		     	    		echo ""
    		            		echo "unknown selection: $ans"
    		            		set wait
				endif
    	                endif 

    	                if ($?wait) then
    		            echo -n "press RETURN to continue ==> "
    		            set ans = $<
    		            unset wait
    	                endif
        
    	            end #} while loop for 'w' sub menu 
                #} 
        
    	        unset wait

                else if ("$ans" == "q") then
    	            break

    	        else
		    echo ""
    	            echo "unknown selection: $ans"
    	            set wait
    	        endif
        
    	        if ($?wait) then
    	            echo -n "press RETURN to continue ==> "
    	            set ans = $<
    	            unset wait
    	        endif
        
    	    end #} while loop for 'z' sub menu 
        #}
    
        else if ("$ans" == "l") then #{
            while (1) #{
    	        unset wait
    	        clear
    	        echo "$L2 Sysdiag logfile options"
                echo "$L2   "
    	        echo "$L2 c..... change log directory (current is $LOGDIR)"
    	        echo "$L2 v..... view logfiles"
    	        echo "$L2 r..... reset (erase) logfiles"
                echo "$L2 q..... quit this menu"
                echo -n "$L2 ==> "
    
                set k = $<
                set ans = ($k)

                if ("$ans" == "c") then 
    	            echo -n "$L3 change log directory to ==> "
    	            set k = $<
    	            set file = ($k)
    	            set LOGDIR = $file

                else if ("$ans" == "v") then #{
    
    	            set nonomatch
    	            if (!(-e $LOGDIR)) then #{
    		        echo "$LOGDIR does not yet exist"
    		        set wait
                    #}

    	            else if (!(-d $LOGDIR)) then #{
    		        echo "$LOGDIR exists but is not a directory"
    		        set wait
                    #}

    	            else #{
    		        set x = $LOGDIR/*
    		        if ($#x == 1) then #{
    			    set noglob
    			    if ($x == $LOGDIR/*) then 
    			        set total = 0
    			    else
    				set total = 1
    			    endif
    			    unset noglob
                        #}
			else 
    			   set total = $#x
    		        endif

    		        if ($total == 0) then
    		            echo "$LOGDIR exists but is empty"
    		            set wait
    		        else
			    clear
    		            more $LOGDIR/*
     	            	    echo -n "Done. press RETURN to continue ==> "
    	            	    set foo = $<
    		        endif

    	            endif #}
                #}
    
    	        unset nonomatch

                else if ("$ans" == "r") then #{
    			if (-e $LOGDIR) then #{
    	            		set nonomatch
    	            		foreach i ($LOGDIR/*) #{
    	                		if (!(-d $i)) then
    		            		echo > $i
    		        		endif
                    		end #}
			endif #}
		#}
    
    	        unset nonomatch

    	        else if ("$ans" == "q") then
    	            break

    	        else
		    echo ""
    	            echo "unknown selection: $ans"
    	            set wait
    	        endif
    
    	        if ($?wait) then
    	            echo -n "press RETURN to continue ==> "
    	            set ans = $<
    	            unset wait
    	        endif
    
            end #} while loop for 'l' sub menu 
        #}
    
        else if ("$ans" == "r") then
	    break

        else if ("$ans" == "q") then
    	    exit

        else
	    echo ""
    	    echo "unknown selection: $ans"
    	    set wait
        endif 
    
        if ($?wait) then
    	    echo -n "press RETURN to continue ==> "
    	    set ans = $<
    	    unset wait
        endif
    
    end #} while loop for Main Menu

endif #} if ($?PRE_MENU)...

#
# make sure the log directory exists and is writable
#
unset wait

if (-e $LOGDIR) then #{
    if (-d $LOGDIR) then #{
        if (-w $LOGDIR) then #{
       	    #OK
    	else
    	    echo log directory $LOGDIR exists, but is not writable
    	    echo please fix...
    	    set wait
    	endif #}
    else
        echo file $LOGDIR exists, but is NOT a directory available for writing log files
        echo please fix...
        set wait
    endif #}
else
    mkdir $LOGDIR
    if (!(-e $LOGDIR)) then #{
        echo unable to create log directory
        echo please fix...
        set wait
    endif #}
    echo created log directory $LOGDIR
endif #}
    
if ($?wait) exit


set SUNTOOLS = $TMP/${TMPFILE_PREFIX}suntools
set W0FILE   = $TMP/${TMPFILE_PREFIX}W0
set W1FILE   = $TMP/${TMPFILE_PREFIX}W1
set W2FILE   = $TMP/${TMPFILE_PREFIX}W2
set W3FILE   = $TMP/${TMPFILE_PREFIX}W3
set TMPFILE  = $TMP/${TMPFILE_PREFIX}file


#
# record file positions and environment for use by
# logview program
#
set nonomatch
set x
set x = $LOGDIR/*
if (-e $LOGDIR && -d $LOGDIR && $#x > 1) then
      set logpos = `wc $LOGDIR/* | awk '{print $4 "-" $1}'`
else
    set logpos
endif

setenv LOGPOS "$logpos"
setenv TMP "$TMP"
setenv LOGDIR "$LOGDIR"
unset nonomatch


if ($?RMON) then

    # Old way of timing was to let each sub-process time itself
    # "-t" means elapsed time, "-T" means absolute time
    #  set CMS = "$CMS -t$RMONtime"
    # "-t" means elapsed time, "-T" means absolute time
    #   set CMS = "$CMS -T$RMONtime"

    if ($?windows) then
        set EXEC =
    else
        set EXEC = time 
    endif
else
    set EXEC =
endif

if ($?VERBOSE) then
    set CMS = "$CMS -v"
endif

#
# invoke the tests
#
if ($?windows) then #{

    # Use Suntools...
    #
    # create a .suntools file and run suntools, which will recursively call
    # this script (in each subwindow) with newly-generated files as input 
    # arguments. *note* '-C csh' for the console options, which fixes ~yet
    # another~ suntools bug-- namely that without the 'csh', spurious arguments
    # get placed there if this script is executed thru /etc/passwd on login
    #
    set W0 = "$CMTL -Wp $W0p -Ws $W0s -Wf $W0fc -Wb $W0bc $ICON -Wl $W0l -WL $W0L $CMD $W0FILE $quote"
    set W1 = "$CMTL -Wp $W1p -Ws $W1s -Wf $W1fc -Wb $W1bc $ICON -Wl $W1l -WL $W1L $CMD $W1FILE $quote"
    set W2 = "$CMTL -Wp $W2p -Ws $W2s -Wf $W2fc -Wb $W2bc $ICON -Wl $W2l -WL $W2L $CMD $W2FILE $quote"
    set W3 = "$CMTL -Wp $W3p -Ws $W3s -Wf $W3fc -Wb $W3bc $ICON -Wl $W3l -WL $W3L $CMD $W3FILE $quote"
    set CS = "$CMTL -Wp $CSp -Ws $CSs -Wf $CSfc -Wb $CSbc $ICON -Wl $CSl -WL $CSL -C csh"
    set PF = "$SHTL -Wp $PFp -Ws $PFs -Wf $PFfc -Wb $PFbc $ICON -Wl $PFl -WL $PFL csh  -fc $PERFMON"
    set MN = "$CMTL -Wp $MNp -Ws $MNs -Wf $MNfc -Wb $MNbc $ICON -Wl $MNl -WL $MNL -WP $MNP -Wi '"monitor"'"
    set CL = "$CLTL -Wp $CLp -Ws $CLs -Wf $CLfc -Wb $CLbc $ICON -WP $CLP -Wi -s"
    set KM = "input -Wp $KMp          $ICON -WP $KMP -Wi -s"

    #
    # Write the test selection arguments to each window file.
    # Each window file gets ALL the selected tests, then
    # tests are selectively unselected depending on which
    # window file is being created.  
    #
    set nonomatch
    \rm -f $W0FILE $W1FILE $W2FILE $W3FILE
    unset nonomatch
    set file = $W0FILE
    source save_config
    set file = $W1FILE
    source save_config
    set file = $W2FILE
    source save_config
    set file = $W3FILE
    source save_config

    set CMSQ = '$CMS'
    set EXTRA = "set CMS = $quote $CMSQ $extra $quote"

    # run VMEM, PMEM, FPUM, FPUP, FPUI, VMEram in window 0
    echo "set WAIT_CNT = $W0_WAIT_CNT" >> $W0FILE
    echo "unset PORT MUX ENET DISK STRESS TAPET FLOP UNIXSCD" >> $W0FILE

    # run DISK TAPET and FLOP in window 1
    echo "set WAIT_CNT = $W1_WAIT_CNT" >> $W1FILE
    echo "unset PORT MUX ENET PMEM VMEM FPUM FPUP FPUI STRESS VME UNIXSCD" >> $W1FILE

    # run ENET, PORT, MUX in window 2
    echo "set WAIT_CNT = $W2_WAIT_CNT" >> $W2FILE
    echo "unset PMEM VMEM DISK STRESS TAPET FPUM FPUP FPUI VME FLOP UNIXSCD" >> $W2FILE

    # run STRESS only in window 3
    echo "set WAIT_CNT = $W3_WAIT_CNT" >> $W3FILE
    echo "unset PORT MUX ENET PMEM VMEM DISK TAPET FPUM FPUP FPUI VME FLOP UNIXSCD" >> $W3FILE

    if ($?UNIXSCD) echo "set UNIXSCD"	>> $W0FILE

    #
    # add other necessary arguments to setup files
    #
    set nonomatch
    \rm -f $TMPFILE	    # just to be safe
    unset nonomatch
    echo "$EXTRA"  >> $TMPFILE
    echo "unset PRE_MENU POST_MENU POST_MORE LOOP windows" >> $TMPFILE
    echo "set am_slave" >> $TMPFILE

    if ($?dryrun) then
	echo "set dryrun"  >> $TMPFILE
    endif

    if ($?VERBOSE) then
	echo "set VERBOSE" >> $TMPFILE
    endif

    echo "set LOGDIR = $LOGDIR" >> $TMPFILE

    cat $TMPFILE >> $W0FILE
    cat $TMPFILE >> $W1FILE
    cat $TMPFILE >> $W2FILE
    cat $TMPFILE >> $W3FILE
    set nonomatch
    \rm -f $TMPFILE	    # just to be safe
    unset nonomatch

    echo $CS > $SUNTOOLS
    if ($?MNTR && $?MNTRt) echo $MNTR >> $SUNTOOLS
    echo $W0 >> $SUNTOOLS
    echo $W1 >> $SUNTOOLS
    echo $W2 >> $SUNTOOLS
    echo $W3 >> $SUNTOOLS
    echo $CL >> $SUNTOOLS
    if ($?KEYMS && $?KEYMSt) echo $KM >> $SUNTOOLS
    echo $PF >> $SUNTOOLS

    #
    # If the environment variable "EXTENDED_TEST" exists, then
    # append the specified suntools script
    #
    if ($?EXTENDED_TEST) then
        if (-e $EXTENDED_TEST) then
	    cat $EXTENDED_TEST >> $SUNTOOLS
	endif
    endif


    #
    # we may have to avoid using perfmon in the future...
    #    foreach i ($PERF)
    #	echo $SHTL "perfmeter $quote -v $i:x $quote" >> $SUNTOOLS
    #    end

    set suntools

    # 
    # make sure suntools not already running
    #
    set pid = `ps -aux |grep suntools |grep -v grep |awk '{print $2}'`
    if ($#pid != 0) then
        echo "ERROR: Suntools aready running - $pid" 
	exit
    endif

    if ($?RMON) then #{
        set temp = `echo $RMONtime:x | sed 's/:/ /g'`
        if ($#temp == 1) then	# only [ss] entered
	    @ seconds = $temp[1]  
        else if ($#temp == 2) then	# [mm:ss] entered
	    @ seconds = ($temp[1] * 60) + $temp[2]
        else if ($#temp == 3) then	# [hh:mm:ss] entered
	    @ seconds = ($temp[1] * 60 * 60) + ($temp[2] * 60) + $temp[3]
        else if ($#temp == 4) then	# [dd:hh:mm:ss] entered
	    @ seconds = ($temp[1] 24 * 60 * 60) + ($temp[2] 60 * 60) + ($temp[3] * 60) + $temp[4]
        endif

        echo ""
	echo "Time limit = $seconds seconds"
	(timer suntools $seconds &)

    endif #}

    echo ""
    echo -n "Starting Suntools... "
    if ($?COLOR) then 
       	suntools -pattern grey -s $SUNTOOLS 
    else
       	suntools -s $SUNTOOLS 
    endif

    clear_colormap		# ... in case display is color
    clear

    if ($?RMON) then
        echo "Time limit ($seconds seconds) expired..."
        echo ""
    endif

#}
else #{ non-suntools
    #
    # try to control jobs and temp files (see note at cleanup label)
    #
    onintr cleanup

    #
    # run the specified tests
    #
    setenv SYSD_VERBOSE 7	# prefix output with originating module

    clear

    #
    # if wait set, sleep wait_cnt seconds before starting tests
    #
    if (($?WAIT_CNT) && ($?am_slave)) then
	if ($WAIT_CNT != "0") then
            echo "delaying $WAIT_CNT seconds... "
            sleep $WAIT_CNT
	endif
    endif

    if ($?UNIXSCD && $?UNIXSCDt) then
	setenv sched "sched $CMS -a$LOGDIR/unixscd.p.log $UNIXSCDt"
	if ($?dryrun) then
	    echo $sched
	else
	    echo -n "starting UNIX scheduler monitor "
	    $EXEC $sched &
	endif 
    endif

    if ($?MNTR && $?MNTRt) then
	setenv mntr "monitor $CMS -a$LOGDIR/mntr.t.log $MNTRt"
	if ($?dryrun) then
	    echo $mntr
	else
	    set PROCS = "monitor"
	    echo -n "starting sysdiag monitor "
	    $EXEC $mntr &
	endif 
	setenv SYSD_MONITOR
    endif

    if ($?STRESS && $?DISKr) then #{
	if (-e diskconfig.stresstest && -r diskconfig.stresstest) then
	    #
	    # disconfig.stresstest file must be formatted as follows:
	    # /dev/sd0 /dev/sd1 /dev/sd3 ... etc
	    #
            set DISKc = `cat diskconfig.stresstest`
	else
            set DISKc = `/etc/mount | awk -f stress.awk`
	endif

	set PROCS = "$PROCS stress"

	foreach i ($DISKc) #{
	    #
	    # only perform test on "c" partition of specified disks
 	    #
	    if (!(-e ${i}c)) then #{
		echo "Warning: ${i}c does not exist (no entry in /dev directory)"
	    else if (!(-r ${i}c)) then
		echo "Warning: Cannot read ${i}c in /dev directory (permission denied)"
	    #
	    # vdev probes the disk to verify that it is online
	    #
	    else if (`vdev -r ${i}c` == "") then
		echo "Warning: ${i} does not respond (is disk installed?)"
	    else
		# 
		# get the disk size
		#
                set m_tmp =  `echo $i | awk '{print (substr($0,7,length($0)))}'`
		set tmp_size = `dkinfo $m_tmp |& grep c: | grep -v grep | awk '{print $2 * 512}'`
		#
		# if the size is greater than 2G-slop, truncate to 2G-slop
		#
		set max = 2147360768
		set size = `echo $tmp_size $max | awk '{ if ($1 > $2) {print $2} else {print $1}}'`
		#
		# if size is valid, then start the test
		#
	    	if ($size > 0) then #{
	            set var = "stress $CMS -a$LOGDIR/disk.r.log $DISKr -s${size}s61440 ${i}c"
			if ($?dryrun) then
	                    echo $var
			else
		    	    echo -n "starting disk read test on $i "
		    	    $EXEC $var &
			endif

		endif #}

	    endif #}

	end #}

    endif #}

    if ($?DISK && $?DISKd) then #{
	set nonomatch
	#
	# disconfig.disktest file must be formatted as follows:
	# /dev/sd0a /dev/sd1a /dev/sd3a ... etc
	#
	if (-e diskconfig.disktest && -r diskconfig.disktest) then
	    set DISKs = `cat diskconfig.disktest`
	else
	    #
	    # locate mounted disks "a" (/) partition
	    #
	    set DISK_tmp = `mount | egrep "[sx]d[0-9]a"\|"[sx]d[1-9][0-9]a" | egrep -v grep | awk '{print $1}'`
	    #
	    # if no root partition located, locate "g" (/usr) partitions
	    #
	    if ($#DISK_tmp == 0) then
		set DISK_tmp = `mount | egrep "[sx]d[0-9]g"\|"[sx]d[1-9][0-9]g" | egrep -v grep | awk '{print $1}'`
	    endif
	    set DISKs = "$DISK_tmp"
	endif

	set PROCS = "$PROCS disk"

	#
	# run the test for each "/tmp" and "/usr/tmp" directory located 
	#
	foreach i ($DISKs) #{
	    set MOUNTED = `/etc/mount | grep $i | grep -v grep | awk '{if (index($1,"/dev")) print $3;}'`	
	    if ("$MOUNTED" == '/') then 
	        set MOUNTED = /tmp
	    else if ("$MOUNTED" == "/usr") then
		set MOUNTED = /usr/tmp
	    endif

	    set var = "disk $CMS -a$LOGDIR/disk.d.log $DISKd -c$MOUNTED"
	    if ($?dryrun) then
	        echo $var
	    else
	        echo -n "starting disk read/compare test on $i "
	        $EXEC $var &
	    endif
	end #}

    endif #}

    if ($?PMEM && $?PMEMt) then #{
	setenv pmem "pmem $CMS -a$LOGDIR/pmem.t.log $PMEMt"
        if ($?dryrun) then
	    echo $pmem
	else
	    set PROCS = "$PROCS pmem"
	    echo -n "starting physical memory test "
	    $EXEC $pmem &
	endif
    endif #}

    if ($?VMEM && $?VMEMp) then #{
	setenv vmemp "vmem $CMS -a$LOGDIR/vmem.p.log $VMEMp"
        if ($?dryrun) then
	    echo $vmemp
	else
	    echo -n "starting virtual memory pattern test "
	    $EXEC $vmemp &
	endif
    endif #}

    if ($?VMEM && $?VMEMf) then #{
	setenv vmemf "vmem $CMS -a$LOGDIR/vmem.f.log $VMEMf"
        if ($?dryrun) then
	    echo $vmemf
	else
	    echo -n "starting virtual memory fork test "
	    $EXEC $vmemf &
	endif
    endif #}

    if ($?VMEM) then
	    set PROCS = "$PROCS vmem"
    endif

    if ($?FPUM && $?FPUm) then #{
	setenv fpum "fpum $CMS -a$LOGDIR/fpu.m.log $FPUm"
        if ($?dryrun) then
	    echo $fpum
	else
	    set PROCS = "$PROCS fpum"
	    echo -n "starting floating point matrix/data conversion tests "
	    $EXEC $fpum &
	endif
    endif #}

    if ($?FPUP && $?FPUp) then #{
	setenv fpup "fpipe $CMS -a$LOGDIR/fpu.p.log $FPUp"
        if ($?dryrun) then
	    echo $fpup
	else
	    set PROCS = "$PROCS fpup"
	    echo -n "starting floating point pipe test "
	    $EXEC $fpup &
	endif
    endif #}

    if ($?FPUI && $?FPUi) then #{
	setenv fpui "fpui $CMS -a$LOGDIR/fpu.i.log $FPUi"
        if ($?dryrun) then
	    echo $fpui
	else
	    set PROCS = "$PROCS fpui"
	    echo -n "starting floating point IEEE compatability test "
	    $EXEC $fpui &
	endif
    endif #}

    if ($?MUX && $?MUXt) then #{
	setenv mux "mux $CMS -n$NMUX -a$LOGDIR/mux.t.log $MUXt"
        if ($?dryrun) then
	    echo $mux
	else
	    set PROCS = "$PROCS mux"
	    echo -n "starting asynchronous MUX test "
	    $EXEC $mux &
	endif
    endif #}

    if ($?VME && $?VMEt) then #{
        setenv vmeram "vmeram $CMS -a$LOGDIR/vmeram.t.log $VMEt"
        if ($?dryrun) then
            echo $vmeram
        else
	    set PROCS = "$PROCS vmeram"
            echo -n "starting VMEbus RAM test "
            $EXEC $vmeram &
        endif
    endif #}

    if ($?FLOP && $?FLOPt) then #{
        setenv floppy "floppy $CMS -a$LOGDIR/floppy.t.log $FLOPt"
        if ($?dryrun) then
            echo $floppy
        else
	    set PROCS = "$PROCS floppy"
            echo -n "starting floppy disk test"
            $EXEC $floppy &
        endif
    endif #}

    if ($?TAPET) then #{
	set nonomatch
	if (-e tapeconfig && -r tapeconfig) then
	    set TMP_TAPEs = `cat tapeconfig`
	else
            set TMP_TAPES = `ls /dev | awk '{if(index($1,"rst")&&\!(index($1,"nrst")))printf("/dev/%s ",$1);}'` 
	    set TMP_TAPEs = `filter $TMP_TAPES`
	endif
	set PROCS = "$PROCS tape"
	set TAPEs = `tapes_online $TMP_TAPEs`
	foreach i ($TAPEs) #{
	    setenv tape "tape $CMS -a$LOGDIR/ttest.t.log $i"
	    if ($?dryrun) then
	        echo $tape
	    else
	        echo -n "starting tape test on $i "
	        $EXEC $tape &
	    endif
	end  #}
     endif #}

    if ($?ENET && $?ENETs) then #{
	setenv enets "enet $CMS -a$LOGDIR/enet.s.log $ENETs"
        if ($?dryrun) then
	    echo $enets
	else
	    echo -n "starting enet stream test "
	    $EXEC $enets &
	endif
    endif #}

    if ($?ENET && $?ENETd) then #{
	setenv enetd "enet $CMS -a$LOGDIR/enet.d.log $ENETd"
        if ($?dryrun) then
	    echo $enetd
	else
	    echo -n "starting enet datagram test "
	    $EXEC $enetd &
	endif
    endif #}

    if ($?ENET) then
	    set PROCS = "$PROCS enet"
    endif

    if ($?PORT && $?PORTt) then #{
	setenv port "port $CMS -a$LOGDIR/port.t.log $PORTt"
        if ($?dryrun) then
	    echo $port
	else
	    set PROCS = "$PROCS port"
	    echo -n "starting serial port test "
	    $EXEC $port &
	endif
    endif #}

    if (!($?dryrun)) then #{

        if (($?RMON) && !($?am_slave)) then #{

            set temp = `echo $RMONtime:x | sed 's/:/ /g'`
            if ($#temp == 1) then	# only [ss] entered
	        @ seconds = $temp[1]  
            else if ($#temp == 2) then	# [mm:ss] entered
	        @ seconds = ($temp[1] * 60) + $temp[2]
            else if ($#temp == 3) then	# [hh:mm:ss] entered
	        @ seconds = ($temp[1] * 60 * 60) + ($temp[2] * 60) + $temp[3]
            else if ($#temp == 4) then	# [dd:hh:mm:ss] entered
	        @ seconds = ($temp[1] 24 * 60 * 60) + ($temp[2] 60 * 60) + ($temp[3] * 60) + $temp[4]
            endif
		
            @ start_time = `gettime`		# mark current seconds
	    @ end_time = $start_time + $seconds	# ending time 

	    echo "Starting timer ($seconds seconds)" >/dev/console
            while (`gettime` < $end_time)
                sleep 4
            end
	    clear
	    echo ""
	    echo "Time limit ($seconds seconds) expired..."
	    echo "Please wait while processes are killed..."

	    set pid = ()
            foreach process ($PROCS)
                set pid = (`ps -aux | grep $process | grep -v grep | awk '{print $2}'`)
		if ($#pid != 0) then
		    echo ""
		    echo -n "killing "$process"... "
		    foreach p ($pid)
		        echo -n "$p "
	                kill -9 $p
		    end
		    echo "done"
		endif
	    end

	    echo ""

	#}
        else #{
            #wait for user interrupt 
            while (1)
                sleep 2
            end
        endif #}

    endif #}

endif #} #end non_suntools else

unset suntools

if (!($?dryrun) && !($?am_slave)) then
    if ($?POST_MENU) then
        logview
    else if ($?POST_MORE) then
        more $LOGDIR/*
    endif
endif

set PRE_MENU

# if loop to main menu not set && not dryrun && not sunview slave, exit
if (!($?LOOP) && !($?am_slave && $?dryrun)) break

if ($?am_slave && $?dryrun) then
    echo "Dryrun finished...waiting until sunview exits"
    while (1)
        end
endif

# if this was a forced dryrun ("-d" entered on command line), then  
# unset the forced dryrun so that the main menu will be re-displayed
if (!($?am_slave) && $?dryrun) then
    echo ""
    echo -n "Dryrun finished... press return to continue"
    unset force_dryrun
    unset dryrun
    set PRE_MENU
    set k = $<
    echo "Note: Dryrun option is no longer set..."
    sleep 3
endif


# This is the interrupt handler process. 
# We never want to enter this unless we receive an interrupt, 
# hence the never true if statement -> if(0)

if (0) then #{

cleanup:

    onintr -  #disable interrupts

    echo ""
    echo "Handling received interrupt..."

    set interrupted

    if ($?dryrun) then
        echo "Note: Dryrun option is no longer set..."
    endif
    unset force_dryrun
    unset dryrun

    sleep 5		# give sub-processes time to exit

    if (!($?dryrun)) then #{	# make sure all processes get killed
        foreach process ($PROCS)
            set pid = (`ps -aux | grep $process | grep -v grep | awk '{print $2}'`)
	    if ($#pid != 0) then
		echo ""
		echo -n "killing "$process"... "
		foreach p ($pid)
		    echo -n "$p "
	            kill -9 $p
		end
		echo "done"
	    endif
	end
	set nonomatch
        \rm -f Delete_Me_*
	unset nonomatch
    endif #}

    set PRE_MENU	# go back to top menu
    set PROCS = ""

    if ($?am_slave) then #{
        # if were a sunview slave and we receive an interrupt then
        # we want to create a csh so that our sunview window process
        # will not exit. This allows the user to be able to look 
        # through the cmdtool window for any useful information after 
        # the test is killed
	/bin/csh
	exit
    endif #}

#    if (!($?CONT)) then
#	set exit_date = `date`
#	echo sysdiag exited $exit_date
#	exit
#    endif
#    unset CONT

endif #} if (0)

end #} while (loop thru tests)

set exit_date = `date`

echo sysdiag exited $exit_date

exit 

