#! /bin/csh -f
#
#       @(#)tapetop 1.6 10/19/87 Copyright 1985 Sun Micro
#
#	script to find 1/4 inch tape devices we want to test,
#	and to (optionally) ask operator to approve list.
#
set usedev
onintr finit
set path=($path $SYSDIAG_DIRECTORY)
@ pass = 1
@ errors = 0
@ error_limit = 5
set mode
set test_args
set test_interrupt_rc = 98
set shoebox
if ($?SHOEBOX_TEST) set shoebox = "shoebox "
if ("$RECON_TEST" != no) then
  set recon_test=yes
else
  set recon_test=no
endif

foreach arg ($argv)
  if ($arg == auto || $arg == select || $arg == verify) then
    set mode = $arg
  else if ($arg == sp) then
    setenv SINGLE_PASS yes
  else if ($arg == test) then 
    set testing 
  else if ($arg == re) then
    setenv RUN_ON_ERROR yes
  else if ($arg == cf) then
    set make_core_file
  else 
    set test_args = ($test_args $arg) 
  endif 
  if ($arg == d) set debug 
end 
  
if ($mode == '') then
  if ($?SYSDIAG_MODE) then
    set mode = $SYSDIAG_MODE
  endif 
endif
 
if (!($mode == verify || $?ROUTINE_TEST || ($TERM != sun && $mode == select))) then
  echo 'tapetop: '$shoebox''$mode' mode.'
else echo ' '
endif
echo "tapetop: Probing for 1/4 inch drives ..."

# set tape_blocks = 1765	# 14 big + 1 little blks
# set tape_blocks = 380		# 3 big + 2 little blks
set tape_blocks = 1000		# 7 big + 118 little blks

if ($?TAPE_TEST) then
  if ($TAPE_TEST == long) then
    if ($TAPE_HEADS == 9) then
      if ($EMULEX_CONTROLLER == yes) then
	 set tape_blocks = 88250   # 700 big + 50 little blks
      else 
	 set tape_blocks = 79550   # 631 big + 44 little blks
      endif
    else
      set tape_blocks = 35000      # 277 big + 98 little blks
    endif
  endif
endif

set devs= (none)

#				probe for tape test file
if (-e tape.test) then
	set devs=($devs "tape.test") 
endif

#                            probe for shoebox st (1/4 in. scsi tapes)
if ($?SHOEBOX_TEST) then
  foreach i (1 2 3 4 5 6 7)
    if (-e /dev/rst${i}) then
      set devs=($devs "st${i}")
    endif
  end
else
#  if ($INTERVENTION_TESTS == enabled) then
#                            probe for st (1/4 in. scsi tapes)
     if ($QIC_24 == yes) then
        if (-e /dev/rst8) then
	   if (-e /dev/rst0) then
              set devs=($devs "st8")
           else
	      echo tapetop: '"/dev/rst8"' found but no '"/dev/rst0"' found.
	      if ("$mode" != verify) then
		 if ($TERM == sun) then
		    exec /bin/csh
		 else exit(0)
	         endif
	      endif
	   endif
        else
  	   echo tapetop: '"QIC-24"' selected but no '"/dev/rst8"' found.
	   if ("$mode" != verify) then
	     if ($TERM == sun) then
	        exec /bin/csh
	     else exit(0)
	     endif
	   endif
        endif
#                            probe for st (1/4 in. scsi tapes)
     else if (-e /dev/rst0) then
            set devs=($devs "st0")
#				probe for ar (1/4 in. tape old controller)
     else if (-e /dev/rar0) then
	   set devs=($devs "ar0") 
        endif
     endif
#  endif
endif # if ($?SHOEBOX_TEST)

if ($#devs != 1) echo tapetop: Devices found - $devs[2-$#devs]

if ("$recon_test" != no) then
  if (!($?SHOEBOX_TEST)) then	# run recon test only if (not SHOEBOX_TEST)
    recon sd v
    if ($status == 0) then
	echo tapetop: Needs both SCSI disk and SCSI tape to run SCSI reconnect test
	set recon_test=no
    else if ("$mode" != select) then
	echo tapetop: SCSI dis/reconnect test will be run
    endif
  else
    echo tapetop: SCSI dis/reconnect test is disabled in SHOEBOX_TEST mode
    set recon_test=no
  endif
endif    
    
if ($INTERVENTION_TESTS != enabled && (!($?SHOEBOX_TEST))) then
     echo tapetop: Intervention tests are not enabled, no tests to run.
     if ("$mode" != verify) then
       if ($TERM == sun && (!($?ROUTINE_TEST))) then
         exec /bin/csh
       endif
     endif
     exit
endif

if ("$mode" == verify) then
  if ("$recon_test" != no) recon v

  if ($#devs == 1 && "$recon_test" == no) then
    if ($INTERVENTION_TESTS == enabled || $?SHOEBOX_TEST) then
      echo tapetop: No devices found, no tests to run.
    else
      echo tapetop: No devices found, intervention tests are not enabled.
    endif
  endif
  exit
else

  set devs=($devs[2-$#devs])

  @ n=1
  set usedev="k"
  while($n <= $#devs)
	if ("$mode" == select) then
		set r
		while ("$r" != y && "$r" != n)
		  echo -n "tapetop: Test "$devs[$n]" ? "
		  set k = $<
		  set r = ($k)
		end
		if ($r == y) then
			set usedev=($usedev $devs[$n])
		endif
	else
			set usedev=($usedev $devs[$n])
	endif
	@ n++
  end

  if ("$mode" == select && "$recon_test" != no) then
    set r
    while ("$r" != y && "$r" != n)
      echo -n "tapetop: Test SCSI dis/reconnect ? "
      set k = $<
      set r = ($k)
    end
    if ($r == n) set recon_test=no
  endif

  if ($#usedev == 1 && "$recon_test" == no) then
        if ($INTERVENTION_TESTS == enabled || $?SHOEBOX_TEST) then 
          echo tapetop: No devices found, no tests to run.
	else 
	  echo tapetop: No devices found, intervention tests are not enabled.
	endif 
	if ($TERM == sun && (!($?ROUTINE_TEST))) then 
          exec /bin/csh
	else exit(0)
        endif
  else
	if ($recon_test == yes) setenv RUN_RECON yes

	if ("$mode" == select && $TERM != sun && !($?ROUTINE_TEST)) exit(1)
	set usedev=($usedev[2-$#usedev])
	foreach dev ($usedev)
	   if ($dev =~ *ar* || $dev =~ st*) then
		echo tapetop: Rewinding $dev ".. "
		set rew=`mt -f /dev/r$dev rew |& cat`
		while ($#rew != 0)
		  echo -n "tapetop: Correct $dev problem then press return: "
		  set rew=$<
		  set rew=`mt -f /dev/r$dev rew |& cat`
		end
		echo tapetop: $dev rewind complete.
	   endif
	end

        if ($?SHOEBOX_TEST || $?RUN_ON_ERROR) then
          set logname=`nextlog tapetop`
          set return = 0
          set start_logged
	  if ($#usedev != 0) then
            echo tapetop: Started on $usedev. `date` | tee $logname
	  endif
	  if ($recon_test == yes) then
            echo tapetop: Started testing SCSI dis/reconnect. `date` | tee $logname
	  endif
	  
	  @ log_interval = 60

	  if ($?LOG_PASS_MSG) then
  	    set dt = `date`
  	    set start_month = $dt[2]
  	    @ d = $dt[3]
  	    set hm = $dt[4]
  	    @ h = `expr substr $hm 1 2`
  	    @ m = `expr substr $hm 4 2`
  	    @ aa = $m + $log_interval
  	    while ($aa >= 60)
    	      @ h = $h + 1
    	      @ aa = $aa - 60
  	    end
  	    @ m = $aa
  	    if ($h >= 24) then
    	      @ d = $d + 1
    	      @ h = $h - 24
  	    endif
  	    @ d = $d * 10000
  	    @ h = $h * 100
  	    @ log_time = $d + $h
  	    @ log_time = $log_time + $m
  	    set start_minute = $m
  	    @ start_day = $d
	  endif      

        else if ($#usedev == 0) then		# log it
             set logname=`nextlog tapetop`
             echo tapetop: Started testing SCSI dis/reconnect. `date` | tee $logname
	     endif
        endif

	if ($#usedev == 0) sleep 50		# allow other tests to start

	while (1)
          
	  foreach dev ($usedev)
	    if ($dev =~ *test*) then
	        echo  tapetop: Testing $dev $test_args
	        if ($TERM == sun) then 
	          devtest sd $dev w=254 $test_args
	          set return = $status
	        else
	          devtest sd $dev w=254 $test_args
	          set return = $status
	        endif
	        if ($?debug) echo Return status = $return
	        if ($return == $test_interrupt_rc) goto finit
	        if ($return != 0) then
	          @ errors++
		  set log_error_msg
	          if (!($?RUN_ON_ERROR)) goto finit
	        endif
            else if ($dev =~ st*) then
	        if ($?SHOEBOX_TEST) then
	          echo tapetop: w=$tape_blocks blocks on $dev
	        endif
	        if ($TERM == sun) then 
	          devtest sd /dev/r$dev w=$tape_blocks $test_args
	          set return = $status
	        else
	          devtest sd /dev/r$dev w=$tape_blocks $test_args
	          set return = $status
	        endif
	        if ($?debug) echo Return status = $return
	        if ($return == $test_interrupt_rc) goto finit
	        if ($return != 0) then
	          @ errors++
		  set log_error_msg
	          if (!($?RUN_ON_ERROR)) goto finit
	        endif
            else if ($dev =~ *ar*) then
	        echo tapetop: Testing $dev
	        if ($TERM == sun) then 
	          devtest sd /dev/r$dev w=$tape_blocks $test_args
	          set return = $status
	        else
	          devtest sd /dev/r$dev w=$tape_blocks $test_args
	          set return = $status
	        endif
	        if ($?debug) echo Return status = $return
	        if ($return == $test_interrupt_rc) goto finit
	        if ($return != 0) then
	          @ errors++
		  set log_error_msg
	          if (!($?RUN_ON_ERROR)) goto finit
	        endif
            else
	        echo tapetop: Unknown device - $dev \? 
	        goto finit
            endif

            if ($?make_core_file) cp /dev/null core
            if (-e core) then
              set corename=`nextlog core`
              mv core $corename
	      if (!($?logname)) then
                set logname=`nextlog tapetop`
              endif
              echo tapetop: Corefile moved to "'$corename'". `date` \
                           | tee -a $logname
            endif

            if ($?log_error_msg) then
              echo ERROR: tapetop, pass $pass, errors $errors. `date` \
		   | tee -a $logname
              unset log_error_msg
	      if ($errors >= $error_limit) then
	        echo tapetop: Error limit exceeded. `date` | tee -a $logname
	        goto finit
	      endif 
	      set return = 0
            endif

          end  # foreach dev ($usedev)

	  if ($#usedev == 0) then
	    echo tapetop: Testing SCSI dis/reconnect
	    recon sd $test_args

	    set return=$status
	    if ($return == $test_interrupt_rc) goto finit
	    if ($return != 0) then
	          @ errors++
		  set log_error_msg
	          if (!($?RUN_ON_ERROR)) goto finit
	    endif

            if ($?make_core_file) cp /dev/null core
            if (-e core) then
              set corename=`nextlog core`
              mv core $corename
	      if (!($?logname)) then
                set logname=`nextlog tapetop`
              endif
              echo tapetop: Corefile moved to "'$corename'". `date` \
                           | tee -a $logname
            endif

            if ($?log_error_msg) then
              echo ERROR: tapetop, pass $pass, errors $errors. `date` \
		   | tee -a $logname
              unset log_error_msg
	      if ($errors >= $error_limit) then
	        echo tapetop: Error limit exceeded. `date` | tee -a $logname
	        goto finit
	      endif 
	      set return = 0
            endif

	  endif	#if ($?usedev == 0)

	  if ($?SHOEBOX_TEST || $?RUN_ON_ERROR) then
            if ($?LOG_PASS_MSG) then
              set dt = `date`
              set current_month = $dt[2]
              @ d = $dt[3]
              set hm = $dt[4]
              @ h = `expr substr $hm 1 2`
              @ m = `expr substr $hm 4 2`
              @ d = $d * 10000
              @ h = $h * 100
              @ current_time = $d + $h
              @ current_time = $current_time + $m
              if ($current_month != $start_month) then
                @ log_time = 10000
                set start_month = $current_month
              endif
              if ($current_time >= $log_time) then
                set log_pass_msg
                @ aa = $m + $log_interval
                while ($aa >= 60)
                  @ h = $h + 100
                  @ aa = $aa - 60
                end    
                if ($h >= 2400) then
                  @ d = $d + 10000
                  @ h = $h - 2400
                endif  
                @ start_minute = $aa
                @ start_day = $d
                @ log_time = $d + $h
                @ log_time = $log_time + $start_minute
              endif
            endif
          endif   #if ($?SHOEBOX_TEST || $?RUN_ON_ERROR)

          if ($?log_pass_msg) then
            echo tapetop: Pass $pass, errors $errors. `date` | tee -a $logname
            unset log_pass_msg
          else
            echo tapetop: Pass $pass, errors $errors. `date`
          endif

	  if ($#usedev == 0) then  # only testing SCSI dis/reconnect in tapetop
	    echo tapetop: Sleeping for 20 minutes
	    sleep 1200	
	  endif

	  if ($?SINGLE_PASS) goto finit
          @ pass++

        end   # while (1)
    
  endif
endif

finit: 

if ($?start_logged) then
  if (!($return == 0 || $return == $test_interrupt_rc)) then
    echo tapetop: Terminated abnormally on $dev. `date` | tee -a $logname
  else
    if (!($?RUN_ON_ERROR || $?SHOEBOX_TEST)) then
      echo tapetop: Stopped. `date` | tee -a $logname
    endif
  endif
  if ($?RUN_ON_ERROR || $?SHOEBOX_TEST) then
    echo tapetop: Stopped, pass $pass, errors $errors. `date` | tee -a $logname
  endif
else if ($#usedev == 0) then
       echo tapetop: Stopped, pass $pass, errors $errors. `date` | tee -a $logname
     endif
endif

foreach dev ($usedev)
      if ($dev =~ *ar* || $dev =~ st*) then
              echo tapetop: Rewinding $dev
              mt -f /dev/r$dev rew >> /dev/null
      endif
end

if($TERM == sun && (!($?ROUTINE_TEST))) exec /bin/csh
