#! /bin/csh -f
#
#       @(#)sysdiag 1.6 10/19/87 Copyright 1985 Sun Micro
#
#

setenv SYSDIAG_VERSION "2.60"
set option_level = 02
set test_list = (pmem vmem enet color sptest ipctest sunlink printer recon)

#*** specify the log file directory here(default to /usr/adm/sysdiaglog) ***
set log_dir1 = /usr
#set log_dir1 = /usr2
set log_dir2 = adm 
#set log_dir2 = `hostname`
set log_dir3 = sysdiaglog
#**********************************************************************

stty -tostop
set sd_dir = `pwd`
set ttys_dir = /etc
set path=($sd_dir $path /etc /usr/etc /usr/bin /usr/suntool /usr/local)
setenv SHELL /bin/csh  
setenv SYSDIAG_DIRECTORY $sd_dir
setenv SUN_MANUFACTURING no 
setenv VMEM_WAIT_TIME 0
setenv GRAPHICS_PROCESSOR no
setenv GRAPHICS_BUFFER no
setenv INTERVENTION_TESTS disabled 
setenv TAPE_TEST short 
setenv TAPE_HEADS 4
setenv EMULEX_CONTROLLER no
setenv QIC_24 no
setenv RECON_TEST no
setenv SP_BOARD no

foreach i (0 1 2 3 4 5 6 7)
  setenv SP_FIXTURE_$i 0
  set sp_board_$i
  set sp_dev_$i
  set sp_$i
end

@ ipc = 0
if ($TERM == sun) then
  foreach i (0 1 2 3)
    if (-e /dev/pc${i}) then
      @ ipc++
    endif
  end
endif
setenv IPC $ipc

set cgfour
color sd v
set return = $status
if ($return > 0) then
  setenv COLOR_MONITOR yes
  if ($return == 1 || $return == 11) set cgfour = -8bit_color_only
  if ($return > 10) setenv MOMOCROME_MONITOR yes
endif

set return = 0
set gp
set manuf = 'disabled'
set manuf_windows
set vmem_wait
set sp_devs
set sp_man_devs
set sp_selected = none 
set sp_list
set dcp_selected
set dcp_installed = no
set dcp_loaded = (no no no no)
set dcp_a_ports_attach = (no no no no)
set dcp_b_ports_attach = (no no no no)
set dcp_c_ports_attach = (no no no no)
set dcp_d_ports_attach = (no no no no)
set mcp_selected
set slp_selected = none
set slp_list
set prt_selected = none
set prt_list
set ipc_ports_selected = none 
set ipc_ports_list
set ipc_disks_selected = none 
set ipc_disks_list
set tape_option
set tape_controller
set tape_retension 
set tape_sleep
set clean_heads 
set f_file
if (-e /usr/sunlink/dcp/dcpload) set dcp_installed = yes
set log_pass
set single_pass
set shoebox
set run_on_error
set load_test

set cin
set sc1
set sc2
set sc3
set sc4
setenv TERM_IS $TERM 
if ($?WINDOW_PARENT) setenv TERM vt100
set k
set op
set testing
set cur_cmd

alias input_msg 'echo " "; echo -n '"  "'"$log_dir2"'" "'"$cur_cmd"'""'"$testing"'":"'" "'" "'; set kk = $<; set k = ($kk); continue'

alias line  'echo " ---------------------------------------------------------------------"'
alias sel_msg 'echo " "; echo " "; echo -n "'  Enter selection and press return: '"'
alias cont_msg 'echo -n "'  Press return to continue. '"'
alias cont_msg1 'cont_msg; set kin = $<'
alias cont_msg2 'cont_msg1; continue'
alias tty_end_msg 'echo " "; echo "**********************************************************"; echo "*       sysdiag: When  ready  to  end  testing           *"; echo "*                enter '"'endt'"' and press return.          *"; echo "**********************************************************"; echo " "'
alias f_msg 'echo " "; echo -n '"  "'"$log_dir2"'" "'"$f_command"'":"'" "'" "'; set kk = $<; continue'
alias bad_fn_msg 'echo " "; echo The selected option name is invalid, select another.'
alias usr_reply 'set kk = $<; set r = ($kk)'
alias bad_term_msg 'echo " "; echo The terminal entry is invalid.'
alias verify_msg 'echo " "; echo -n " Verify devices found, enter y if correct, n if not, press return. "'
alias correct_msg 'echo -n " Correct problems, restart System Diagnostics.(press return to continue) "; set r = $<; continue'
alias time_msg 'echo " "; echo The current date and time is: `date`'
alias reset_log 'echo -n " Reset log files? Enter y for yes, n for no, press return. "; usr_reply'
alias remove_core 'echo -n " Remove core files? Enter y for yes, n for no, press return. "; usr_reply'
alias list_core 'echo " "; echo sysdiag: Core files found:; echo " "; file core*; echo " "'
alias via ' set save_term = $TERM; setenv TERM $TERM_IS; vi \!*; setenv TERM $save_term'
alias auto_x 'echo -n "Enter a selection from above for automatic execution on login: "; usr_reply'
alias reset_sl_var ' set slp_list; set dcp_selected; set mcp_selected; set slp_selected = none'
alias select_port_msg 'echo " "; echo -n "Select the \!* ports to test, press return: "'
alias wrong_device ' echo " "; echo Incorrect \!* specified.; echo " "; cont_msg1'
alias install_fixtures ' echo " "; echo "**************************************************"; echo "*   Install the appropriate test fixture(s) on   *"; echo "*   the selected port(s) before starting tests.  *"; echo "**************************************************"; echo " "'
alias auto_start_msg ' echo " "; echo " NOTE: WHEN AUTOMATICALLY STARTING TESTS,  NO INTERVENTION WARNINGS"; echo "       ARE DISPLAYED,  THE USER MUST  ASSURE THAT SCRATCH TAPES ARE"; echo "       INSTALLED  IN  ALL TAPE  DRIVES AND THAT ALL DEVICE LOOPBACK"; echo "       TEST PLUGS AND OTHER TEST FACILITIES ARE CORRECTLY INSTALLED."'

setenv SYSDIAG_HOST `hostname` 
set options_dir = options
set log_dir = $log_dir1/$log_dir2/$log_dir3
setenv SD_LOG_DIRECTORY $log_dir
setenv LPT1 "locker $log_dir/ipc-sd"

alias start_msg 'echo \!* version started. `date` >> '$log_dir'/logtimes; if ($?EXTRA) options sd >> {$log_dir}/logtimes'
alias stop_msg 'echo \!* version stopped. `date` >> '$log_dir'/logtimes'

if (!(-e $log_dir)) then
  if (!(-e $log_dir1)) then
      mkdir $log_dir1
      if (!(-e $log_dir1)) then
        echo -n 'sysdiag: Super user must make '$log_dir1' directory. Enter return to continue. '
	set kk = $<
        exit
      endif
      chmod 777 $log_dir1
  endif
  cd $log_dir1
  if (!(-e $log_dir2)) then
    mkdir $log_dir2 
    chmod 777 $log_dir2
  endif
  cd $log_dir2
  if (!(-e $log_dir3)) then
    mkdir $log_dir3 
    chmod 777 $log_dir3
  endif
endif

cd $log_dir

if (!(-e $options_dir)) then
  mkdir $options_dir
  chmod 777 $options_dir
endif

if (-e ipc_lock_color) rm -f ipc_lock_color
if (-e color_lock_ipc) rm -f $SD_LOG_DIRECTORY/color_lock_ipc

if (-e sd_single_routine_options) then 
  rm -f sd_single_routine_options 
endif 

set af = auto_start
if ($argv != "") set af = $argv

if (-e $options_dir/$af) then
  set k = (o $af)
  if (-e $log_dir/core) then
    set corename = `nextlog core`
    mv $log_dir/core $corename
    echo ' '
    echo sysdiag: Saved previous core file to "'$corename'". `date` | tee -a $log_dir/logtimes
  endif
else
  if (-e $log_dir/core || -e $log_dir/core.1) then
    list_core
    set r
    while ("$r" != r && "$r" != s)
      echo  -n 'sysdiag: Previous core files are present, enter r to remove, s to save, press return. '
      usr_reply
    end
    if ("$r" == s && -e $log_dir/core) then
      set corename = `nextlog core`
      echo sysdiag: Saved previous core file to "'$corename'". `date` | tee -a $log_dir/logtimes
      mv $log_dir/core $corename
      sleep 4
    else if ("$r" == r) then
      rm -f $log_dir/core*
    endif
  endif
endif

onintr interrupt_end

if (-e /dev/sky) then
   mv -i /dev/sky /dev/sky-sd
endif

if (-e $ttys_dir/ttys-sd) then
  mv $ttys_dir/ttys-sd $ttys_dir/ttys
  chmod 644 $ttys_dir/ttys
  if ($ttys_dir != $sd_dir) kill -1 1
endif

while ($op != q)
  if ("$k" == '') then
    clear
    echo " "
    echo " " 
    echo " " 
    echo " " 
    echo " " 
    echo " " 
    echo " SYSTEM DIAGNOSTICS                                   sun microsystems"
    echo " "
    echo '                                                         Version ' $SYSDIAG_VERSION
    echo ' SELECTIONS:'
    line
    echo ' 1 - Automatic mode of System Diagnostics.'
    echo ' 2 - Verify mode of System Diagnostics.'
    echo ' 3 - Select mode of System Diagnostics.' 
    echo ' 4 - Single test mode of System Diagnostics.' 
    if ($?EXTRA) then
        echo ' 5 - Device test mode of System Diagnostics.'
    endif
    echo ' '
    echo ' o - options for System Diagnostics.'
#    echo ' h - help information for System Diagnostics.'
    echo ' s - set date and time.'
    echo ' d - display System Diagnostic log files.'
    echo ' r - reset System Diagnostic log files.'
    echo ' c - change System Diagnostics console type: ('$TERM')'
    echo ' p - change System Diagnostics password.'
    echo " "
    echo ' q - quit System Diagnostics.'
    echo " "
    if ($?EXTRA) then
       echo $log_pass''$single_pass''$tape_retension''$clean_heads''$tape_sleep''$load_test'ipc='$IPC''$run_on_error
       if ("$sc1" != '' || "$sc2" != '' || "$sc3" != '' || "$sc4" != '') then
         echo 'c1 = '$sc1', c2 = '$sc2', c3 = '$sc3', c4 = '$sc4
       endif
       echo " "
       echo -n ' '$log_dir2''$cur_cmd''$testing': '
    else 
       sel_msg
    endif

    set kk = $<
    set k = ($kk)
    if ("$k" == '') continue
  endif

  if ("$k" == q) break
  if ("$k" == Q) break
  set running_tests

  set fin = ($k)
  set k

  if ($fin[1] =~ sc[1234] && $?EXTRA) then
    if ($#fin >= 2) then
      set cin = ($fin[2-$#fin])
    endif
    if ($fin[1] == sc1) set sc1 = ($cin)
    if ($fin[1] == sc2) set sc2 = ($cin)
    if ($fin[1] == sc3) set sc3 = ($cin)
    if ($fin[1] == sc4) set sc4 = ($cin)
    continue
  endif

  if ($fin[1] =~ c[1234] && $?EXTRA) then
    if ($fin[1] == c1) set cin = ($sc1)
    if ($fin[1] == c2) set cin = ($sc2)
    if ($fin[1] == c3) set cin = ($sc3)
    if ($fin[1] == c4) set cin = ($sc4)
    if ($#fin >= 2) set cin = ($cin $fin[2-$#fin])
    echo $cin
  else if ($fin[1] != R || (!($?EXTRA))) then
    set cin = ($fin)
  else if ($#fin >= 2) then
    set cin = ($cin $fin[2-$#fin]) 
  endif

  if ($?EXTRA) set cur_cmd = " ( $cin )"

  if ($#cin > 1) then
    set op = ($cin[1])
    set file = ($cin[2-$#cin])
  else 
    set op = $cin
    set file
  endif

  switch ($op)

  case 4:
    onintr int4
    set kk
    if ($op == 5) set kk = 5
    sysdiag.test $kk $file
    set return = $status
    if (0) then
int4:
      set return = $status
    endif
    if ($return == -1) goto interrupt_end
    onintr interrupt_end
    if ($return == 1) then
      set k = o
      set file
      set opt_from_stm
    endif
    continue

  case cc:
  case ccm:
  case clear_c*:
    if ($?COLOR_MONITOR) clear_colormap
    continue

  case d:
    set nonomatch
    echo ' '
    more $log_dir/log*
    echo ' '
    set r
    while ("$r" != y && "$r" != n)
      reset_log
    end   
    if ($r == y) then
      rm -f $log_dir/log*
    endif

  case r:
    if ($op == r) then
      set nonomatch
      rm -f $log_dir/log*
    endif
    if (-e $log_dir/core || -e $log_dir/core.1) then
      list_core
      set r
      while ("$r" != y && "$r" != n)
        remove_core
      end   
      if ($r == y) then
        rm -f $log_dir/core*
      endif
    endif
    unset nonomatch
    continue
  
  case s:
    time_msg
    echo ' '
    echo -n '  Enter yymmddHHMM and press return '
    echo `date $<` ' '
    echo ' '
    echo -n '  Verify date and time.'
    cont_msg2

  case c:
    set name_selected = no
    if ($#cin == 2) then
      set name_selected = yes
    else if ($#cin > 2) then
	   bad_term_msg
         endif
    endif
    while (1)
      while ($name_selected == no)
        echo ' '
        set r
        while ("$r" == '')
         echo -n 'Enter the console terminal type or n for no change: '
         usr_reply
        end
        if ("$r" == n) then
          break; break
        endif
        set file = ($r)
        if ($#file == 1) then
          set name_selected = yes
        else
          bad_term_msg
        endif
      end

      set ttyname = `tty | sed "s/\/dev\///"`
      grep -s $ttyname /etc/ttytype
      if ($status != 0) then
	echo ' '
        echo -n '?Can not set Sysdiag console to "'$file'".'
        cont_msg1
        break
      endif

      sed "/$ttyname/s/^.* /$file /" /etc/ttytype > /tmp/tmp.$$
      # this does not work for 4.0's /etc/ttytab file

      mv /tmp/tmp.$$ /etc/ttytype
      if ($status == 0) then
        chmod 644 /etc/ttytype
        echo ' '
        echo Sysdiag console is set to "'$file'", relogin to take effect.
        echo ' '
        goto interrupt_end
      else
        echo ' '
        echo -n '?Can not set Sysdiag console to "'$file'".'
	cont_msg1
	break
      endif
    end
    continue

  case p:
    echo ' '
    passwd
    continue

  case options:
    options
    cont_msg2

  case o:
   set sop
   set ok
   if ($#cin > 1) then 
     set ok = ($cin)
   endif
   while ($sop != q)
    if ("$ok" == '') then
      clear

if ("$RECON_TEST" == yes) then
  set run_recon="recon"
else
  set run_recon="no recon"
endif

echo ' '
echo ' '
echo ' '
echo ' '
echo ' SYSTEM DIAGNOSTIC OPTIONS'
line
echo ' i  - intervention tests ('$INTERVENTION_TESTS')'
if ($INTERVENTION_TESTS == enabled) then
  echo ' t  - 1/4 inch tape drive options ('$TAPE_TEST' test, '$TAPE_HEADS' tracks'$tape_option''$tape_controller', '$run_recon')'
  echo ' s  - serial port test options, loop port(s) tty: ('$sp_selected')'
  echo ' sl - SunLink test options, loop port(s) dcp/mcp: ('$slp_selected')'
  echo ' pp - printer port test options, loop ports(s) mcpp: ('$prt_selected')'
  if ($IPC != 0 || $?EXTRA) echo ' p  - IPC test options, disk drive(s) ('$ipc_disks_selected'), loop port(s) ('$ipc_ports_selected')'
endif
echo ' m  - manufacturing tests ('$manuf''$manuf_windows''$shoebox''$gp''$sp_0''$sp_1''$sp_2''$sp_3''$sp_4''$sp_5''$sp_6''$sp_7''$vmem_wait''$run_on_error')'
echo ' '
echo ' f  - file (save) current options.'
echo ' o  - use (restore) options previously filed.'
echo ' lo - list option names previously filed.'
echo ' h  - help information for options.'
echo ' q  - quit option menu and return to the selection menu.'
echo ' Q  - Quit System Diagnostics.'
sel_msg

      set kk = $<
      set ok = ($kk)

      if ("$ok" == '') continue
      if ("$ok" == Q) goto interrupt_end
      if ("$ok" == q) then
	 if ($?opt_from_stm) set k = 4
	 unset opt_from_stm
         break; continue;
      endif
    endif
    set kin = ($ok)
    set ok

    if ($#kin > 1) then
       set sop = ($kin[1])
       set file = ($kin[2-$#kin])
    else 
      set sop = $kin
      set file
    endif

    unset user_error

    switch ($sop)

    case h:
      pushd $sd_dir > /dev/null
      sysdiag.help o $file
      if ($status == -1) goto interrupt_end 
      popd > /dev/null
      continue

    case lo:
      echo ' '
      ls $options_dir
      echo ' '
      cont_msg2

    case o:
       set name_selected = no
       if ($#kin == 2) then
	 if ($file =~ */*) then
	   bad_fn_msg
	 else
           set name_selected = yes
	   set file = $options_dir/$file
	 endif
       else if ($#kin > 2) then
              bad_fn_msg
            endif
       endif
       while (1)
	 while ($name_selected == no)
	   echo ' '
	   set r
	   echo -n 'Enter the name of the saved options to use or press return for none: '
	   usr_reply
	   if ("$r" == "") then
	     break; break
           endif
           if ($#r == 1) then
	     if ($r =~ */*) then
	       bad_fn_msg
	     else
               set name_selected = yes
               set file = ($options_dir/$r)
	     endif
           else
             bad_fn_msg
           endif
	 end
         if (!(-e $file)) then
	   bad_fn_msg
	   set name_selected = no
         else
           foreach i ($sp_list $slp_list $prt_list $ipc_disks_list $ipc_ports_list)
             unsetenv $i
           end
	   eval set file_option_level  = `awk '/file_option_level/ {print $4}' $file`
	   if ($file_option_level != $option_level) then
	     echo ' '
	     echo 'The option file "'$file'" can no longer be used.'
	     echo 'Manually select the options and save them again.'
	     echo ' '
	     cont_msg1
	     break; continue
	   endif
	   source $file
	   unsetenv NO_CLEAN_HEADS
	   unsetenv NO_TAPE_RETENSION
	   unsetenv NO_TAPE_SLEEP
	   unsetenv SHOEBOX_TEST
	   unsetenv RUN_ON_ERROR
	   if ($tape_retension != "") setenv NO_TAPE_RETENSION yes
	   if ($clean_heads != "") setenv NO_CLEAN_HEADS yes
	   if ($tape_sleep != "") setenv NO_TAPE_SLEEP yes
	   if ($shoebox != "") setenv SHOEBOX_TEST yes
	   if ($run_on_error != "") setenv RUN_ON_ERROR yes
	   set sp_man_devs = ($sp_dev_0 $sp_dev_1 $sp_dev_2 $sp_dev_3 $sp_dev_4 $sp_dev_5 $sp_dev_6 $sp_dev_7)
	   if ("$sp_devs" != '' || "$sp_dev_1" != '') then
	      if (!(-e $ttys_dir/ttys-sd)) then
		cp $ttys_dir/ttys $ttys_dir/ttys-sd
	      endif
	      mv -f $ttys_dir/ttys $ttys_dir/ttys.sd.temp
	      set tty_ds = ($sp_man_devs $sp_devs)
              sed $tty_ds $ttys_dir/ttys.sd.temp > $ttys_dir/ttys 
              if ($ttys_dir != $sd_dir) kill -1 1 
	      unset tty_ds
           endif
           if ("$dcp_selected" != '') then
	      set return = 0
	      foreach dcp_file (load mon.image attach layer)
	         if (!(-e /usr/sunlink/dcp/dcp$dcp_file)) then
	           echo ' '
	           echo sysdiag: \'/usr/sunlink/dcp/dcp$dcp_file\'  not found, cannot 
	           echo '         initialize the data communications processor.'
	           set return = no_file
	           break
	         endif
	      end
	      if ($return == 0) then
	         echo ' '
	         set echo_init_msg
	         foreach i ($dcp_selected)
	           if ($i =~ a*) then
	              set dcp_board = a
	              set dcp_load_index = 1
	              set port_attached = ($dcp_a_ports_attach)
	           else if ($i =~ b*) then
	              set dcp_board = b
	              set dcp_load_index = 2
	              set port_attached = ($dcp_b_ports_attach)
	           else if ($i =~ c*) then
	              set dcp_board = c
	              set dcp_load_index = 3
	              set port_attached = ($dcp_c_ports_attach)
	           else if ($i =~ d*) then
	              set dcp_board = d
	              set dcp_load_index = 4
	              set port_attached = ($dcp_d_ports_attach)
	           endif
	           if ($i =~ *0) then
	              set dcp_port = 0
	           else if ($i =~ *1) then 
	              set dcp_port = 1
	           else if ($i =~ *2) then 
	              set dcp_port = 2
	           else if ($i =~ *3) then 
	              set dcp_port = 3
	           endif
	           @ attach_index = $dcp_port + 1
	           if ($?EXTRA) then
	             echo sysdiag: Initializing the data communications processor, $i.
	           else
	              if ($?echo_init_msg) then
	                 echo sysdiag: Initializing the data communications processor.
	                 unset echo_init_msg
	              endif
	           endif
	           if ($dcp_loaded[$dcp_load_index] == no) then
	              if ($?EXTRA) then
	                 echo sysdiag: Loading dcp board $dcp_board.
	              endif
	             /usr/sunlink/dcp/dcpload -b $dcp_board /usr/sunlink/dcp/dcpmon.image
	             set return = $status
	           endif
	           if ($return == 0) then
	              set dcp_loaded[$dcp_load_index] = yes
	              if ($port_attached[$attach_index] == no) then
		         if ($?EXTRA) then
	                    echo sysdiag: Attaching dcp $i.
	                 endif
	                 /usr/sunlink/dcp/dcpattach /dev/dcp$i >& /dev/null
	                 set return = $status
	                 if ($return == 0) then
		            if ($?EXTRA) then
	                       echo sysdiag: Loading dcp layer for $dcp_board$dcp_port
	                    endif
	                    /usr/sunlink/dcp/dcplayer dcp$i zss$dcp_port
	                    set return = $status
		            if ($return == 0) then
		               set port_attached[$attach_index] = yes
		               if ($dcp_board == a) set dcp_a_ports_attach = ($port_attached)
		               if ($dcp_board == b) set dcp_b_ports_attach = ($port_attached)
		               if ($dcp_board == c) set dcp_c_ports_attach = ($port_attached)
		               if ($dcp_board == d) set dcp_d_ports_attach = ($port_attached)
	                    endif
		         endif
	              endif
	           endif
	           if ($return != 0) break
 	         end
	      endif
	      if ($return != 0) then
	         if ($return != no_file) then
	           echo ' '
	           echo sysdiag: The data communications processor initalization failed for $i.
                 endif
	         echo ' '
		 cont_msg1
                 foreach i ($slp_list)
                   unsetenv $i
                 end
	         reset_sl_var
	      endif
           endif
           if ($?verify_options)  then
             set >! o$file
             printenv >> o$file
           endif
	   break
         endif
       end
       if ($?auto_start_cmd) then
	 if ($?EXTRA && "$auto_start_cmd" == extra) then
	   set k
	 else
           set k = ($auto_start_cmd)
	 endif
	 unset auto_start_cmd
	 set sop = q
       endif
       continue

    case f:
       set name_selected = no
       if ($#kin == 2) then
	 if ($file =~ */*) then
	   bad_fn_msg
	 else
	   set name_selected = yes
	   set file = $options_dir/$file
	 endif
       else if ($#kin > 2) then
	      bad_fn_msg
	    endif
       endif
       while (1)
	 while ($name_selected == no)
	    echo ' '
	    set r
	    echo -n 'Enter a name for the saved options or press return for none: '
	    usr_reply
	    if ("$r" == "") then
	      break; break; continue
            endif
	    if ($#r == 1) then
	      if ($r =~ */*) then 
		bad_fn_msg
	      else
	        set name_selected = yes
	        set file = ($options_dir/$r)
	      endif
	    else 
	      bad_fn_msg
	    endif
	 end
         if (-e $file) then
	     echo ' '
	     set r
	     echo 'The selected name is already in use.'
	     while ("$r" != r && "$r" != n && "$r" != d)
	       echo -n 'Enter r to reuse, n for no, d to delete, press return: '
	       usr_reply
	     end
	     if ($r == n) then
	       set name_selected = no 
	     else if ($r == d) then
	       rm -f $file
               break; continue
	     else break
	     endif
         else break
	 endif
       end

       echo set file_option_level = $option_level >! $file
       if ($file == $options_dir/auto_start || $?EXTRA) then
	 echo ' '
	 echo ' 1 for Automatic mode of System Diagnostics.'
	 echo ' 2 for Verify mode of System Diagnostics.'
	 echo ' 3 for Select mode of System Diagnostics.'
	 echo ' n for none'
	 echo ' '
	 set r
	 if (!($?EXTRA)) then
	   while ("$r" !~ [123] && "$r" != n)
	     auto_x
	   end
	 else 
	   while ("$r" == "")
	     auto_x
	   end
	 endif
	 if ("$r" != n) then
           echo set auto_start_cmd = \($r\) >> $file
	   if ("$r" =~ [123]) then
	     auto_start_msg
	   endif
	 endif
	 echo ' '
	 set r
	 while ("$r" != y && "$r" != n)
	   echo -n 'Clear the log files and core files? Enter y for yes, n for no, press return: '
	   usr_reply
	 end
	 if ($r == y) then
	   echo set nonomatch >> $file
	   echo 'rm -f $log_dir/log*' >> $file
	   echo 'rm -f $log_dir/core*' >> $file
	   echo unset nonomatch >> $file
	 endif
       endif

       echo setenv INTERVENTION_TESTS $INTERVENTION_TESTS >> $file
       echo setenv TAPE_TEST $TAPE_TEST >> $file
       echo setenv TAPE_HEADS $TAPE_HEADS >> $file
       echo setenv RECON_TEST $RECON_TEST >> $file
       echo set clean_heads = \"$clean_heads\" >> $file
       echo set tape_retension = \"$tape_retension\" >> $file
       echo set tape_sleep = \"$tape_sleep\" >> $file
       echo setenv EMULEX_CONTROLLER $EMULEX_CONTROLLER >> $file
       echo set tape_controller = \($tape_controller\) >> $file
       echo setenv QIC_24 $QIC_24 >> $file
       echo set tape_option = \($tape_option\)>> $file
       echo set dcp_selected = \($dcp_selected\) >> $file
       echo set mcp_selected = \($mcp_selected\) >> $file
       echo set slp_selected = \($slp_selected\) >> $file
       echo set slp_list = \($slp_list\) >> $file
       echo set prt_selected = \($prt_selected\) >> $file
       echo set prt_list = \($prt_list\) >> $file
       echo set ipc_disks_selected = \($ipc_disks_selected\) >> $file
       echo set ipc_disks_list = \($ipc_disks_list\) >> $file
       echo set ipc_ports_selected = \($ipc_ports_selected\) >> $file
       echo set ipc_ports_list = \($ipc_ports_list\) >> $file
       echo set sp_selected = \($sp_selected\) >> $file
       echo set sp_list = \($sp_list\) >> $file
       echo set sp_devs = \($sp_devs\) >> $file
       foreach i ($sp_list $slp_list $prt_list $ipc_disks_list $ipc_ports_list)
	 eval echo setenv "$i" ""`echo '$'$i`"" >> $file
       end
       echo setenv SUN_MANUFACTURING $SUN_MANUFACTURING >> $file
       echo set manuf = $manuf >> $file
       echo set manuf_windows = \"$manuf_windows\" >> $file
       echo set run_on_error = \"$run_on_error\" >> $file
       echo set vmem_wait = \"$vmem_wait\" >> $file
       echo set shoebox = \"$shoebox\" >> $file
       echo setenv VMEM_WAIT_TIME $VMEM_WAIT_TIME >> $file
       echo setenv GRAPHICS_PROCESSOR $GRAPHICS_PROCESSOR >> $file
       echo setenv GRAPHICS_BUFFER $GRAPHICS_BUFFER >> $file
       echo set gp = \"$gp\" >> $file
       echo setenv SP_BOARD $SP_BOARD >> $file
       foreach i (0 1 2 3 4 5 6 7) 
         eval echo setenv SP_FIXTURE_$i ""`echo '$'SP_FIXTURE_$i`"" >> $file
         eval set j = \"'$'sp_$i\"
         echo set sp_$i = \"$j\" >> $file
         eval set j = \"'$'sp_board_$i\"
         echo set sp_board_$i = \"$j\" >> $file
         eval set j = \"'$'sp_dev_$i\"
         echo set sp_dev_$i = \"$j\" >> $file
       end
       unset j
       chmod 777 $file
       if ($?verify_options)  then
         set >! f$file
         printenv >> f$file
       endif
       continue
		
    case i:
      set r
      if ("$file" != '') then
        if ("$file" == y || "$file" == n) set r = ($file)
      endif
      if ("$r" == '') echo ' '
      while ("$r" != y && "$r" != n)
        echo -n 'Allow intervention tests? y for yes/n for no, press return: '
        usr_reply
      end
      if ($r == y) then
        setenv INTERVENTION_TESTS enabled 
        echo ' '
        echo '***********************************************'
        echo '*   If testing tape drives, clean  the tape   *' 
        echo '*   drive  read/write  heads  and install a   *'
        echo '*   scratch tape in each drive to be tested.  *'
        echo '***********************************************'
        echo ' '
	cont_msg1
      else
        setenv INTERVENTION_TESTS disabled 
        foreach i ($sp_list $slp_list $prt_list $ipc_disks_list $ipc_ports_list)
          unsetenv $i
        end
        set sp_list
        set sp_selected = none
	set prt_list
	set prt_selected = none
        set sp_devs
        set ipc_disks_list
        set ipc_disks_selected = none
        set ipc_ports_list
        set ipc_ports_selected = none
	reset_sl_var
      endif
      continue

    case m:
      if ($SUN_MANUFACTURING == yes) then
        set r
        if ("$file" != '') then
          if ("$file" == y || "$file" == n) set r = ($file)
        endif
        if ("$r" == '') echo ' '
        while ("$r" != y && "$r" != n)
          echo -n 'Allow special manufacturing tests? y for yes/n for no, press return: '
          usr_reply
        end
        if ($r == n) then
          setenv SUN_MANUFACTURING no 
          setenv VMEM_WAIT_TIME 0
          set vmem_wait
          setenv GRAPHICS_PROCESSOR no
          setenv GRAPHICS_BUFFER no
          set gp
          unsetenv SHOEBOX_TEST
          set shoebox
          setenv SP_BOARD no
          foreach i (0 1 2 3 4 5 6 7)
            setenv SP_FIXTURE_$i 0
            set sp_dev_$i
            set sp_$i
          end
          set sp_man_devs
          set manuf = 'disabled'
          set manuf_windows
	  unsetenv RUN_ON_ERROR
	  set run_on_error
	  continue
        endif
      endif
      setenv SUN_MANUFACTURING yes 
      set manuf = 'enabled'
      set vmem_wait = ("  vmem wait= "$VMEM_WAIT_TIME"")
      set mop
      set mk
      if ($#cin > 1) then
        set mk = ($cin)
      endif
      while ($mop != q)
        if ("$mk" == '') then
clear
echo ' '
echo ' '
echo ' '
echo ' '
echo ' MANUFACTURING TEST OPTIONS'
line
echo ' v  - vmem wait time ('$vmem_wait' )'
echo ' s  - serial port boards ('$sp_0''$sp_1''$sp_2''$sp_3''$sp_4''$sp_5''$sp_6''$sp_7' )'
echo ' g  - graphics processor ('$gp' )'
echo ' sb - shoebox testing ('$shoebox' )'
echo ' mw - manufacturing windows ('$manuf_windows' )'
echo ' re - run on error ('$run_on_error' )'
echo ' '
echo ' h  - help information for manufacturing options.'
echo ' q  - quit manufacturing test option menu.'
echo ' Q  - Quit System Diagnostics.'
sel_msg

        set kk = $<
        set mk = ($kk)

        if ("$mk" == '') continue
        if ("$mk" == Q) goto interrupt_end
        if ("$mk" == q) then
           break; continue;
        endif
        if ("$mk" == -2) then
           break; break; continue;
        endif
      endif
      set kin = ($mk)
      set mk

      if ($#kin > 1) then
         set mop = ($kin[1])
         set file = ($kin[2-$#kin])
      else
        set mop = $kin
        set file
      endif

      switch ($mop)

      case re:
        if ($?RUN_ON_ERROR) then
          unsetenv RUN_ON_ERROR
          set run_on_error
        else
          setenv RUN_ON_ERROR yes
          set run_on_error = " re"
        endif
        continue
 
      case h:
        pushd $sd_dir > /dev/null
        sysdiag.help o m $file
        if ($status == -1) goto interrupt_end
        popd > /dev/null
        continue

      case v:
        set r
        if ("$file" != '') then
	  if ("$file" == 0 || "$file" == 30 || "$file" == 60 || "$file" == 90) set r = ($file)
        endif
        if ("$r" == '') echo ' '
        while ("$r" != 0 && "$r" != 30 && "$r" != 60 && "$r" != 90)
          echo -n 'Vmem wait time (in minutes). Enter 0, 30, 60, or 90, press return: '
          usr_reply
        end
        setenv VMEM_WAIT_TIME $r
        set vmem_wait = ("  vmem wait= "$VMEM_WAIT_TIME"")
        continue

      case sb:
        unsetenv SHOEBOX_TEST
        set shoebox
        set r
        if ("$file" != '') then
          if ("$file" == y || "$file" == n) set r = ($file)
        endif
        if ("$r" == '') echo ' '
        while ("$r" != y && "$r" != n)
          echo -n 'Shoebox testing? y for yes/n for no, press return: '
          usr_reply
        end
        if ($r == y) then
          setenv SHOEBOX_TEST yes
          set shoebox = ' sb'
        endif
        continue

      case mw:
        set manuf_windows
        set r
        echo ' '
	echo ' 1 for sun2/3 manufacturing windows using "gallant.r.19" fonts.'
	echo ' 2 for sun3/4 high resolution windows using "cour.b.24" fonts.'
	echo ' 3 for sun2/3 wide windows using "gallant.r.19" fonts.'
	echo ' n for normal sysdiag windows.'
	echo ' '
        while ("$r" != 1 && "$r" != 2 && "$r" != 3 && "$r" != n)
          echo -n 'Enter a window selection from above: '
          usr_reply
        end
	  if ($r != n) then
            set manuf_windows = (' mw'$r)
	  endif
        continue

      case g:
        setenv GRAPHICS_PROCESSOR no
        setenv GRAPHICS_BUFFER no
        set gp
        set r
	unset j
        if ("$file" != '') then
	  if ($#file > 1) then
	    set j = ($file[2-$#file])
	    set file = $file[1]
	  endif
          if ("$file" == y || "$file" == n) set r = ($file)
        endif
        if ("$r" == '') echo ' '
        while ("$r" != y && "$r" != n)
          echo -n 'Graphics processor? y for yes/n for no, press return: '
          usr_reply
        end
        if ($r == y) then
	  setenv GRAPHICS_PROCESSOR 1
 
	  gp2test sd v
	  set return = $status

	  if ($return == 0) then

	    set gp = ' gp2'
	    setenv GRAPHICS_PROCESSOR 2
	    echo -n 'gp2 graphics processor found, press return to continue: '
	    usr_reply

	  else

          set gp = ' gp'
          set r
          if ($?j) then
            if ("$j" == y || "$j" == n) set r = ($j)
          endif
          if ("$r" == '') echo ' '
          while ("$r" != y && "$r" != n)
            echo -n 'Graphics buffer? y for yes/n for no, press return: '
            usr_reply
          end
          if ($r == y) then
            setenv GRAPHICS_BUFFER yes
            set gp = ' gp&b'
          endif

	  endif

        endif
        continue

      case s:
	if ($SP_BOARD == no) then
	  @ sp_b = 0
          foreach kk (/dev/tty00 /dev/tty10 /dev/tty20 /dev/tty30 /dev/ttyh4 /dev/ttyi4 /dev/ttyj4 /dev/ttyk4)
	    if (-e $kk) then
              set sp_board_$sp_b = Disabled
	    else
              set sp_board_$sp_b = (No device drivers)
	    endif
	    @ sp_b++
	  end
        else
          set r
          if ("$file" != '') then
            if ("$file" == y || "$file" == n) set r = ($file)
          endif
          if ("$r" == '') echo ' '
          while ("$r" != y && "$r" != n)
            echo -n 'Testing serial port boards? y for yes/n for no, press return: '
            usr_reply
          end
          if ($r == n) then
            setenv SP_BOARD no
            set sp_devs
            foreach i (0 1 2 3 4 5 6 7) 
              setenv SP_FIXTURE_$i 0 
              set sp_dev_$i 
              set sp_$i 
            end 
            continue
	  endif
	endif

	unset sp_b_to_test
        set spop
        set spk
        while ($spop != q)
          if ("$spk" == '') then
clear
echo ' '
echo ' '
echo ' '
echo ' '
echo ' SERIAL PORT BOARDS'
line
echo ' 0 - '$sp_board_0' ('$sp_0' )'
echo ' 1 - '$sp_board_1' ('$sp_1' )'
echo ' 2 - '$sp_board_2' ('$sp_2' )'
echo ' 3 - '$sp_board_3' ('$sp_3' )'
echo ' h - '$sp_board_4' ('$sp_4' )'
echo ' i - '$sp_board_5' ('$sp_5' )'
echo ' j - '$sp_board_6' ('$sp_6' )'
echo ' k - '$sp_board_7' ('$sp_7' )'
echo ' '
echo ' q  - quit serial port board tests menu.'
echo ' Q  - Quit System Diagnostics.'
sel_msg
                  
          set kk = $<
          set spk = ($kk)
          if ("$spk" == '') continue
          if ("$spk" == Q) goto interrupt_end
        endif
        set kin = ($spk)
        set spk

        if ($#kin > 1) then
           set spop = ($kin[1])
           set file = ($kin[2-$#kin])
        else
          set spop = $kin
          set file
        endif

	switch ($spop)

	case [0-3]:
	case [h-k]:
          if ($spop =~ [0-3]) then
	    @ sp_b = $spop
	  else if ($spop == h) then
	    @ sp_b = 4
	  else if ($spop == i) then
	    @ sp_b = 5
	  else if ($spop == j) then
	    @ sp_b = 6
	  else if ($spop == k) then
	    @ sp_b = 7
	  endif

	  eval set r = '$'sp_board_"$sp_b"
	  if ($r == No && (!($?EXTRA))) then
	    echo ' '
	    echo -n 'No device drivers for board "'$spop'".'
	    cont_msg2
	  else
            set r = $file
	    unset kk
            while ("$r" != 8 && "$r" != 12 && "$r" != 14 && "$r" != 16 && "$r" != n)
	      if (!($?kk)) echo ' '
              echo -n 'Board "'$spop'" ports? Enter 8, 12, 14, 16 or n for none, press return: '
              usr_reply
            end
	    if ($r != n) then
	      set sp_board_$sp_b = Enabled
	      set sp_$sp_b = " sp$spop-$r"
	      setenv SP_FIXTURE_$sp_b $r
	      set sp_b_to_test
	    else
	      set sp_board_$sp_b = Disabled
	      set sp_$sp_b
	      setenv SP_FIXTURE_$sp_b 0
	    endif
	  endif
          continue

	case q:
	case -2:
	case -3:
	  if ($?sp_b_to_test) then
	    @ sp_b = 0
	    @ kk = 0
	    foreach sp_board (0 1 2 3 4 5 6 7)
	      eval set r = '$'sp_board_"$sp_board"
	      if ($r == Enabled) then
	        @ kk++
                if ($sp_board =~ [0-3]) then
                  set sp_b = $sp_board
                else if ($sp_board == 4) then
                  set sp_b = h
                else if ($sp_board == 5) then
                  set sp_b = i
                else if ($sp_board == 6) then
                  set sp_b = j
                else if ($sp_board == 7) then
                  set sp_b = k
                endif
	        eval set r = '$'SP_FIXTURE_"$sp_board"
		if ($r == 8 || $r == 14 || $r == 16) then
	        eval set sp_dev_$sp_board = `echo \(-e s/12tty"$sp_b"0/02tty"$sp_b"0/ -e s/12tty"$sp_b"1/02tty"$sp_b"1/ -e s/12tty"$sp_b"2/02tty"$sp_b"2/ -e s/12tty"$sp_b"3/02tty"$sp_b"3/ -e s/12tty"$sp_b"4/02tty"$sp_b"4/ -e s/12tty"$sp_b"5/02tty"$sp_b"5/ -e s/12tty"$sp_b"6/02tty"$sp_b"6/ -e s/12tty"$sp_b"7/02tty"$sp_b"7/\)`
		else if ($r == 12) then
	        eval set sp_dev_$sp_board = `echo \(-e s/12tty"$sp_b"4/02tty"$sp_b"4/ -e s/12tty"$sp_b"5/02tty"$sp_b"5/ -e s/12tty"$sp_b"6/02tty"$sp_b"6/ -e s/12tty"$sp_b"7/02tty"$sp_b"7/\)`
		endif
                if ($r == 14) then
	          eval set sp_dev_$sp_board = `echo \('$'sp_dev_$sp_board -e s/12tty"$sp_b"8/02tty"$sp_b"8/ -e s/12tty"$sp_b"9/02tty"$sp_b"9/ -e s/12tty"$sp_b"a/02tty"$sp_b"a/ -e s/12tty"$sp_b"b/02tty"$sp_b"b/ -e s/12tty"$sp_b"c/02tty"$sp_b"c/ -e s/12tty"$sp_b"d/02tty"$sp_b"d/\)`
                else if ($r == 16 || $r == 12) then
	          eval set sp_dev_$sp_board = `echo \('$'sp_dev_$sp_board -e s/12tty"$sp_b"8/02tty"$sp_b"8/ -e s/12tty"$sp_b"9/02tty"$sp_b"9/ -e s/12tty"$sp_b"a/02tty"$sp_b"a/ -e s/12tty"$sp_b"b/02tty"$sp_b"b/ -e s/12tty"$sp_b"c/02tty"$sp_b"c/ -e s/12tty"$sp_b"d/02tty"$sp_b"d/ -e s/12tty"$sp_b"e/02tty"$sp_b"e/ -e s/12tty"$sp_b"f/02tty"$sp_b"f/\)`
                endif
	      endif
	    end
	    if ($kk != 0) setenv SP_BOARD $kk

            if (!(-e $ttys_dir/ttys-sd)) then
              cp $ttys_dir/ttys $ttys_dir/ttys-sd
            endif
            mv -f $ttys_dir/ttys $ttys_dir/ttys.sd.temp

            if ("$sp_selected" != none) then
echo ' '
echo '**********************************************************'
echo '*            ----------- WARNING -----------             *'
echo '* A possible  conflict exists between  the manufacturing *'
echo '* serial  port test just  selected and the  serial ports *'
echo '* selected  earlier. Verify that the selection made does *'
echo '* not conflict with the serial  port test selections. If *'
echo '* there  are conflicts  and they  are not corrected, the *'
echo '* serial port  test  will  report  errors and  terminate.*'
echo '**********************************************************'
	    endif
install_fixtures
cont_msg

            set sp_man_devs = ($sp_dev_0 $sp_dev_1 $sp_dev_2 $sp_dev_3 $sp_dev_4 $sp_dev_5 $sp_dev_6 $sp_dev_7)
            sed $sp_man_devs $ttys_dir/ttys.sd.temp > $ttys_dir/ttys 
            if ($ttys_dir != $sd_dir) kill -1 1 
            set kin = $<
	  endif

          if ($spop == q) then
             breaksw; break; continue
          else if ($spop == -2) then
             breaksw; break; break; continue
          else if ($spop == -3) then
             breaksw; break; break; break; continue
          endif
	endsw
        end         # serial port board
      endsw
      end
      continue    # manufacturing tests
 
    case s:
    if ($INTERVENTION_TESTS == enabled)  then 
      foreach i ($sp_list)
        unsetenv $i
      end
      set sp_list
      set sp_selected = none
      set sp_devs
      if ("$file" == '') then
        select_port_msg serial
        set r = $<
      else
	set r = ($file)
      endif
      if ("$r" == '') continue
	@ n=1
	foreach i ($r)
	  if (($i !~ [a-b]) && ($i !~ [0-3]) && ($i !~ [0-3h-k][0-9]) && ($i !~ [0-3h-k][a-f]) && ($i != none) && ($i !~ [a-b]-*) && ($i !~ [0-3]-*) && ($i !~ [0-3h-k][0-9]-*) && ($i !~ [0-3h-k][a-f]-*)) then
	      set user_error
	      break
	  else 
	     if ($i =~ *-*) then
	        if (($i !~ *-[a-b]) && ($i !~ *-[0-3]) && ($i !~ *-[0-3h-k][0-9]) && ($i !~ *-[0-3h-k][a-f])) then
		  set user_error
	          break
                endif    
	     endif 
             if ("$r" != none) then
	        setenv SERIAL_PORTS_$n $i
	        set sp_list = ($sp_list "SERIAL_PORTS_${n}")
             endif
	     @ n++
          endif
	end
	if (!($?user_error)) then
	  set sp_selected = ($r)
        else
	  wrong_device port
          foreach i ($sp_list)
           unsetenv $i
          end
	  set sp_list
	  continue
        endif 
      endif
      if ("$sp_selected" != none) then
        if (!(-e $ttys_dir/ttys-sd)) then
	  cp $ttys_dir/ttys $ttys_dir/ttys-sd
        endif
        mv -f $ttys_dir/ttys $ttys_dir/ttys.sd.temp
	if ($SP_BOARD != no) then
          echo ' '
          echo '**********************************************************'
	  echo '*            ----------- WARNING -----------             *'
	  echo '* A possible  conflict  exists between  the serial ports *'
	  echo '* just  selected and the manufacturing serial  port test *' 
	  echo '* selected  earlier.  Verify that the selections made do *'
	  echo '* not conflict with the manufacturing  serial port test. *'
	  echo '* If there are conflicts and they are not corrected, the *'
	  echo '* serial port  test  will report errors  and  terminate. *'
          echo '**********************************************************'
	endif
	install_fixtures
	cont_msg
        set sp_devs
        foreach i ($sp_selected)
          if ($i !~ *-*) then
	    if ($i !~ [0-3]) then
              set sp_devs = ($sp_devs -e s/12tty$i/02tty$i/)
	    else
	      set sp_devs = ($sp_devs -e s/12ttys$i/02ttys$i/)
	    endif
          else
            set ii = `echo $i | awk -f- '{print $1, $2}'`
	    if ($ii[1] !~ [0-3]) then
	      set sp_devs = ($sp_devs -e s/12tty$ii[1]/02tty$ii[1]/)
	    else
	      set sp_devs = ($sp_devs -e s/12ttys$ii[1]/02ttys$ii[1]/)
	    endif
	    if ($ii[2] !~ [0-3]) then
	      set sp_devs = ($sp_devs -e s/12tty$ii[2]/02tty$ii[2]/)
	    else
	      set sp_devs = ($sp_devs -e s/12ttys$ii[2]/02ttys$ii[2]/)
	    endif
          endif
        end 
        sed $sp_devs $ttys_dir/ttys.sd.temp > $ttys_dir/ttys 
	if ($ttys_dir != $sd_dir) kill -1 1
        set kin = $<
    endif
    continue
  
    case sl:
    if ($INTERVENTION_TESTS == enabled) then 
      foreach i ($slp_list)
        unsetenv $i
      end
      reset_sl_var
      unset sl_reset
      if ("$file" == '') then
        select_port_msg Sunlink
        set r = $<
      else
	set r = ($file)
      endif
      if ("$r" == '') continue
	@ n=1
	foreach i ($r)
	  if (($i !~ [a-d][0-3] && $i !~ [0-9] && $i !~ 1[0-5]) && $i != none && ($i !~ [a-d][0-3]-* && $i !~ [0-9]-* && $i !~ 1[0-5]-*)) then
	    set user_error
	    break
	  else 
            if ($i =~ *-*) then
              if (($i !~ *-[a-d][0-3]) && ($i !~ *-[0-9]) && ($i !~ *-1[0-5])) then
                set user_error
                break
              endif
            endif
            if ("$r" != none) then

	      if ($i =~ *-*) then
		if ($i =~ [a-d]* || $i =~ 1[0-5]*) then
	          set jj = `expr substr $i 1 2`
	          set kk = `expr substr $i 4 2`
		else
	          set jj = `expr substr $i 1 1`
	          set kk = `expr substr $i 3 2`
		endif
	      else
	        set jj = $i
	        set kk
	      endif
	      if ($jj =~ [a-d]*) then
		 set dcp_selected = ($dcp_selected $jj)
	      else
		set mcp_selected = ($mcp_selected $jj)
	      endif
	      if ($kk =~ [a-d]*) then
		 set dcp_selected = ($dcp_selected $kk)
	      else
		set mcp_selected = ($mcp_selected $kk)
	      endif

	      setenv SL_PORTS_$n $i
	      set slp_list = ($slp_list "SL_PORTS_${n}")
            endif
	    @ n++
          endif
	end
	if (!($?user_error)) then
	  set slp_selected = ($r)
        else
	  wrong_device port
	  set sl_reset
        endif 
      endif
      if ("$slp_selected" != none) then
	install_fixtures
	cont_msg1
        if ("$dcp_selected" != '') then
	  foreach dcp_file (load mon.image attach layer)
	     if (!(-e /usr/sunlink/dcp/dcp$dcp_file)) then
	       echo ' '
	       echo sysdiag: \'/usr/sunlink/dcp/dcp$dcp_file\'  not found, cannot 
	       echo '         initialize the data communications processor.'
	       echo ' '
	       cont_msg1
	       set sl_reset
	       break;
	     endif
	  end
          if (!($?sl_reset)) then
	    echo ' '
	    set echo_init_msg
	    set return = 0
	    foreach i ($dcp_selected)
	      if ($i =~ a*) then
	        set dcp_board = a
	        set dcp_load_index = 1
	        set port_attached = ($dcp_a_ports_attach)
	      else if ($i =~ b*) then
	        set dcp_board = b
	        set dcp_load_index = 2
	        set port_attached = ($dcp_b_ports_attach)
	      else if ($i =~ c*) then
	        set dcp_board = c
	        set dcp_load_index = 3
	        set port_attached = ($dcp_c_ports_attach)
	      else if ($i =~ d*) then
	        set dcp_board = d
	        set dcp_load_index = 4
	        set port_attached = ($dcp_d_ports_attach)
	      endif
	      if ($i =~ *0) then
	        set dcp_port = 0
	      else if ($i =~ *1) then 
	        set dcp_port = 1
	      else if ($i =~ *2) then 
	        set dcp_port = 2
	      else if ($i =~ *3) then 
	        set dcp_port = 3
	      endif
	      @ attach_index = $dcp_port + 1
	      set dcp_dev
	      if ($?EXTRA) set dcp_dev = " $i"
	      if ($?echo_init_msg || $?EXTRA) then
	        echo sysdiag: Initializing the data communications processor$dcp_dev.
	        unset echo_init_msg
	      endif
	      if ($dcp_loaded[$dcp_load_index] == no) then
	        if ($?EXTRA) echo sysdiag: Loading dcp board $dcp_board.
	        /usr/sunlink/dcp/dcpload -b $dcp_board /usr/sunlink/dcp/dcpmon.image
	        set return = $status
	      endif
	      if ($return == 0) then
	         set dcp_loaded[$dcp_load_index] = yes
	         if ($port_attached[$attach_index] == no) then
		    if ($?EXTRA) echo sysdiag: Attaching dcp $i.
	            /usr/sunlink/dcp/dcpattach /dev/dcp$i >& /dev/null
	            set return = $status
	            if ($return == 0) then
		       if ($?EXTRA) echo sysdiag: Loading dcp layer for $dcp_board$dcp_port
	               /usr/sunlink/dcp/dcplayer dcp$i zss$dcp_port
	               set return = $status
		       if ($return == 0) then
		          set port_attached[$attach_index] = yes
		          if ($dcp_board == a) set dcp_a_ports_attach = ($port_attached)
		          if ($dcp_board == b) set dcp_b_ports_attach = ($port_attached)
		          if ($dcp_board == c) set dcp_c_ports_attach = ($port_attached)
		          if ($dcp_board == d) set dcp_d_ports_attach = ($port_attached)
	               endif
		    endif
	         endif
	      endif
	      if ($return != 0) break
 	    end
	    if ($return != 0) then
	       echo ' '
	       echo sysdiag: The data communications processor initalization failed for $i.
	       echo ' '
	       cont_msg1
	       set sl_reset
	    endif
          endif 		#!sl_reset
        endif 			#if ("$dcp_selected" != '') then
        if ($?sl_reset) then
          foreach i ($slp_list)
          unsetenv $i
        end
	reset_sl_var
      endif
    endif
    continue
  
    case pp:
    if ($INTERVENTION_TESTS == enabled) then 
      foreach i ($prt_list)
        unsetenv $i
      end

      set prt_list
      set prt_selected = none

      if ("$file" == '') then
        select_port_msg printer
        set r = $<
      else
	set r = ($file)
      endif

      if ("$r" == '') continue

	@ n=1
	foreach i ($r)
	  if ($i !~ [0-3]) then
	      set user_error
	      break
	  else if ("$r" != none) then
	      setenv PRINTER_PORTS_$n $i
	      set prt_list = ($prt_list "PRINTER_PORTS_${n}")
          endif

	  @ n++
	end

	if (!($?user_error)) then
	  set prt_selected = ($r)
        else
	  wrong_device port
          foreach i ($prt_list)
           unsetenv $i
          end
	  set prt_list
	  continue
        endif 

      if ("$prt_selected" != none) then
	install_fixtures
	cont_msg
        set kin = $<
      endif 
    endif
    continue
  
    case t:
    if ($INTERVENTION_TESTS == enabled) then 
      setenv TAPE_TEST short
      setenv QIC_24 no
      setenv EMULEX_CONTROLLER no
      set tape_option
      set tape_controller
      set r
      echo ' '
      while ("$r" != y && "$r" != n)
        echo -n 'Long 1/4 inch tape test? y for yes/n for no, press return: '
        usr_reply
      end
      if ($r == y) setenv TAPE_TEST long
      set r
      while ("$r" != 4 && "$r" != 9)
        echo -n '1/4 inch tape drive tracks =? Enter 4 or 9, press return: '
        usr_reply
      end
      if ($r == 9) then
        setenv TAPE_HEADS 9
        set r
        while ("$r" != y && "$r" != n)
          echo -n 'Test "QIC-24"? y for yes/n for no, press return: '
          usr_reply
        end
        if ($r == y) then
	  setenv QIC_24 yes
          set tape_option = ', QIC-24'
        endif
        set r
        while ("$r" != y && "$r" != n)
          echo -n 'Is the tape controller an Emulex? y for yes/n for no, press return: '
          usr_reply
        end
        if ($r == y) then
	  setenv EMULEX_CONTROLLER yes
          set tape_controller = (', Emulex ctlr')
        endif
      else setenv TAPE_HEADS 4
      endif

      set r
      while ("$r" != y && "$r" != n)
        echo -n 'SCSI disconnect/reconnect test? y for yes/n for no, press return: '
        usr_reply
      end
      if ($r == y) then
	 setenv RECON_TEST yes
      else
	 setenv RECON_TEST no
      endif

    endif
    continue

    case p:
    if (($IPC != 0 || $?EXTRA) && ($INTERVENTION_TESTS == enabled))  then
      foreach i ($ipc_ports_list $ipc_disks_list)
        unsetenv $i
      end
      set ipc_ports_list
      set ipc_ports_selected = none
      set ipc_disks_list
      set ipc_disks_selected = none
      echo ' '
      echo -n 'Select IPC disk drives to test, press return: '
      set r = $<
      if ("$r" != '' && "$r" != none) then
        foreach i ($r)
          if ($i !~ d[0-3]) then
            set user_error
            break
	  else
	    setenv IPC_DISK_$i $i
	    set ipc_disks_list = ($ipc_disks_list "IPC_DISK_${i}")
          endif
        end
        if ($?user_error) then
	  wrong_device IPC disk
          foreach i ($ipc_disks_list)
           unsetenv $i
          end
          set ipc_disks_list
        else
          set ipc_disks_selected = ($r)
          echo ' '
          echo '************************************************'
          echo '*   Install  test diskette(s) in the selected  *'
	  echo '*   IPC B disk drive(s) before starting tests. *'
	  echo '*   NOTE:  Do not install a  diskette  in any  *'
	  echo '*          A drive, the  ipc test  will  fail. *'
          echo '************************************************'
        endif   
      endif   

      select_port_msg IPC parallel
      set r = $<
      if ("$r" == '' || "$r" == none) continue
      foreach i ($r)
        if ($i !~ pp[0-3]) then
          set user_error
          break
        else
          setenv IPC_PPORT_$i $i
          set ipc_ports_list = ($ipc_ports_list "IPC_PPORT_${i}")
        endif
      end
      if ($?user_error) then
	wrong_device IPC port
        foreach i ($ipc_ports_list)
         unsetenv $i
        end
        set ipc_ports_list
      else
        set ipc_ports_selected = ($r)
	install_fixtures
	cont_msg1
      endif   

    endif
    continue

    endsw

    if (!($?EXTRA)) continue

    switch ($sop)
      case mh:
        via $sd_dir/sysdiag.help
        continue

      case pe:
          printenv
          cont_msg2

      case set:
          set | more
          cont_msg2

      case rtf:
          if ($#kin > 1) then
            rm -f $file
          else
            rm -i *
          endif
          continue

      case x:
          if ($#kin > 1) then
            pushd $sd_dir
            $file
            popd
          endif
          cont_msg2

      case lst:
          ls -la | more
          cont_msg2

      case etf:
          via $file
          continue
 
    endsw
   end
  continue

  case extra:
     if ($?EXTRA) then
       unsetenv EXTRA
       set cur_cmd
       unsetenv LOG_PASS_MSG
       set log_pass
     else 
       stty erase ^H
       setenv EXTRA yes
       set cur_cmd = " ( $cin )"
       setenv LOG_PASS_MSG yes
       set log_pass = "lpm "
     endif
     continue

  case [1-3]:
    if ($TERM == sun) then
	if ($IPC != 0) then
	  set sun_tools = .suntools-ipc
	else
	  if ($manuf_windows == '') then
	    set sun_tools = .suntools
	  else if ($manuf_windows == 'mw1') then
	    set sun_tools = .suntools-manuf_1
	  else if ($manuf_windows == 'mw2') then
	    set sun_tools = .suntools-manuf_2
	  else if ($manuf_windows == 'mw3') then
	    set sun_tools = .suntools-manuf_3
	  endif
	endif
	setenv SYSDIAG_MODE auto
	if ($op == 2) then
	  time_msg
	  ipctop verify
          disktop verify
	  memtop verify
	  tapetop verify
	  devtop verify
	  set r
	  while ("$r" != y && "$r" !=  n)
	    verify_msg
	    usr_reply
	  end
	  if ($r == n) then 
	    correct_msg
	  endif
	else if ($op == 3) then
	  setenv SYSDIAG_MODE select
	endif
	start_msg Window
	if ($testing == '') then
	    suntools -s $sd_dir/$sun_tools $cgfour
	else echo suntools -s $sd_dir/$sun_tools $cgfour $SYSDIAG_MODE
	endif
	stop_msg Window
	unsetenv SYSDIAG_MODE
    else 
      rm -f $log_dir/disktop_selected
      rm -f $log_dir/ipctop_selected
      if ($op == 1) then
	  tty_end_msg
	  start_msg Terminal
	  if ($testing == '') then
#	    ipctop auto &   	not in terminal mode yet
	    disktop auto &
	    devtop auto
	    tapetop auto &
	    sleep 10
	    memtop auto &
          else
	    echo 'ipctop auto &    	not in terminal mode yet'
	    echo 'disktop auto &'
	    echo 'devtop auto'
	    echo 'tapetop auto &'
	    echo 'memtop auto &'
          endif
      else if ($op == 2) then
	       time_msg
	       ipctop verify
	       disktop verify 
               memtop verify 
               tapetop verify 
               devtop verify 
	       set r
	       while ("$r" != y && "$r" != n)
	         verify_msg
		 usr_reply
	       end   
	       if ($r == n) then
	         correct_msg
               endif
	       tty_end_msg
	       start_msg Terminal
	       if ($testing == '') then
#		 ipctop auto &       not in terminal mode yet
		 disktop auto &
		 devtop auto
		 tapetop auto &
		 sleep 10
                 memtop auto &
               else
                 echo 'ipctop auto &         not in terminal mode yet'
	         echo 'disktop auto &'
	         echo 'devtop auto'
	         echo 'tapetop auto &'
	         echo 'memtop auto &'
               endif
      else if ($op == 3) then  
		    start_msg Terminal
		    unset running_tests
#		    ipctop select 	not in terminal mode yet
		    set ipc_status = 0   # $status

		    disktop select 
		    set disktop_status = $status

		    memtop select 
		    set memtop_status = $status

		    tapetop select
		    set tapetop_status = $status

		    if ($testing == '') then
		      devtop select
		      if ($status == 0) set running_tests
		    else echo 'devtop select &' 
		    endif

		    if ($ipc_status == 1) then
		      set running_tests
		      if ($testing == '') then
		        ipctop auto &
		      else echo 'ipctop auto &'
		      endif
                    endif
		        
		    if ($disktop_status == 1) then
		      set running_tests
		      if ($testing == '') then
		        disktop auto &
		      else echo 'disktop auto &'
		      endif
                    endif
		        
	     	    if ($tapetop_status == 1) then
		      set running_tests
		      if ($testing == '') then
	                tapetop auto &
		      else echo 'tapetop auto &'
		      endif
                    endif
		        
	     	    if ($memtop_status == 1) then
	       	      set running_tests
		      if ($testing == '') then
	         	memtop pmem &
		      else echo 'memtop pmem &'
		      endif
                    else if ($memtop_status == 2) then
		      set running_tests
		      if ($testing == '') then
		        memtop vmem &
		      else echo 'memtop vmem &'
		      endif
                    else if ($memtop_status == 3) then
		      set running_tests
		      if ($testing == '') then
	  	        memtop auto &
		      else echo 'memtop auto &'
		      endif
                    endif
      endif
      if ($?running_tests) then 
	if ($op == 3) then
	  tty_end_msg
        endif
        if ($testing == '') then
          echo ' '
	  /bin/csh
          echo ' '
        else
   	  echo '/bin/csh'
          stop_msg Terminal
 	endif
      else
        echo sysdiag: No tests selected. >> $log_dir/logtimes
	stop_msg Terminal
      endif
    endif
    echo ' '
    more $log_dir/log*
    echo ' '
    set r
    while ("$r" != y && "$r" != n)
      reset_log
    end   
    if ($r == y) then
      rm -f $log_dir/log*
    endif
    if (-e $log_dir/core || -e $log_dir/core.1) then
      list_core
      set r
      while ("$r" != y && "$r" != n)
        remove_core
      end   
      if ($r == y) then
        rm -f $log_dir/core*
      endif
    endif
    continue
  endsw

  if (!($?EXTRA)) continue
    switch ($op)

    case pe: 
      printenv
      input_msg

    case cs:
      pushd $sd_dir
      /bin/csh
      popd
      continue

    case ts:
      /bin/csh
      continue

    case cf:
      set nonomatch
      echo ' '
      file core*
      unset nonomatch
      input_msg
 
    case rcf:
      rm -f core*
      input_msg
 
    case lst:
      ls -la | more
      input_msg

    case lo:
      echo ' '
      ls -l $options_dir
      echo ' '
      cont_msg2

    case eipc:
      via ipc-sd
      continue
 
    case etf:
      via $file
      continue
 
    case rtf:
      if ($#cin > 1) then
        rm -f $file
      else
	rm -i *
      endif
      input_msg

    case e:
      pushd $sd_dir
      via $file
      popd
      continue

    case m:
      via $sd_dir/$0
      continue

    case it:
      setenv ROUTINE_TEST yes
      onintr iti
      ipctop select $file 
      if ($status == 1) ipctop $file
iti:
      onintr interrupt_end
      unsetenv ROUTINE_TEST
      input_msg

    case dt:
      setenv ROUTINE_TEST yes
      onintr dti
      devtop select $file
dti:
      onintr interrupt_end
      unsetenv ROUTINE_TEST
      input_msg

    case dst:
      setenv ROUTINE_TEST yes
      onintr dsti
      disktop select $file
      if ($status == 1) disktop $file
dsti:
      onintr interrupt_end
      unsetenv ROUTINE_TEST
      input_msg

    case mt:
      setenv ROUTINE_TEST yes
      onintr mti
      memtop select $file
      set memtop_status = $status
      if ($memtop_status == 1) then
        memtop pmem $file
      else if ($memtop_status == 2) then
        memtop vmem $file
      else if ($memtop_status == 3) then
        memtop auto $file
      endif
mti:
      onintr interrupt_end
      unsetenv ROUTINE_TEST
      input_msg

    case tt:
      setenv ROUTINE_TEST yes
      onintr tti
      tapetop select $file
      set tapetop_status = $status
      if ($tapetop_status == 1) then
	tapetop auto $file
      endif
tti:
      onintr interrupt_end
      unsetenv ROUTINE_TEST
      input_msg

    case rt:
      date
      mt -f /dev/rst0 retension
      date
      input_msg

    case ps:
      ps ax | more
      input_msg

    case l:
      ls -la $sd_dir | more
      input_msg

    case ntr:
      if (!($?NO_TAPE_RETENSION)) then
        setenv NO_TAPE_RETENSION yes
        set tape_retension = "ntr "
      else
        unsetenv NO_TAPE_RETENSION
	set tape_retension
      endif
      input_msg
 
    case ntc:
      if (!($?NO_CLEAN_HEADS)) then
        setenv NO_CLEAN_HEADS yes
        set clean_heads = "ntc "
      else
        unsetenv NO_CLEAN_HEADS
	set clean_heads
      endif
      input_msg
 
    case nts:
      if (!($?NO_TAPE_SLEEP)) then
        setenv NO_TAPE_SLEEP yes
        set tape_sleep = "nts "
      else
        unsetenv NO_TAPE_SLEEP
	set tape_sleep
      endif
      input_msg
 
    case sp:
      if (!($?SINGLE_PASS)) then
        setenv SINGLE_PASS yes
        set single_pass = "sp "
      else
        unsetenv SINGLE_PASS
	set single_pass
      endif
      input_msg
 
    case lpm:
      if (!($?LOG_PASS_MSG)) then
        setenv LOG_PASS_MSG yes
        set log_pass = "lpm "
      else
        unsetenv LOG_PASS_MSG
	set log_pass
      endif
      input_msg
 
    case ipc:
      if ($#cin > 1) then
	if ("$file" =~ [1-4]) then
	  setenv IPC $file
        else setenv IPC 0
        endif
      endif
      input_msg
 
    case lt:
      if ($?SD_LOAD_TEST) then
        unsetenv SD_LOAD_TEST
	set load_test
      else
        setenv SD_LOAD_TEST yes
	set load_test = "lt "
      endif
      input_msg

    case re:
      if ($?RUN_ON_ERROR) then
        unsetenv RUN_ON_ERROR
        set run_on_error
      else
        setenv RUN_ON_ERROR yes
        set run_on_error = " re"
      endif
      input_msg
 
    case test:
      if ("$testing" != '') then
        set testing
        unsetenv TEST_SCRIPT
      else
        set testing = ' testing'
        setenv TEST_SCRIPT yes
      endif
      input_msg
 
    case term:
      if ($#cin > 1) then
	setenv TERM $file
	if ($file != sun) stty -tostop
      else 
	echo TERM = $TERM
        input_msg
      endif
      continue
 
    case tf:
      set rm_file = test
    case ttf:
      if ($op == ttf) set rm_file = tape.test
      set r = $file
      while ("$r" != y && "$r" != n)
        echo ' '
        echo  -n ' y/n test file: '
        usr_reply
      end
      if ($r == y) then
          cp /dev/null  $log_dir/$rm_file
      else rm -f $log_dir/$rm_file
      endif
      input_msg

    case mot:
    case motd:
      echo ' '
      more /etc/motd
      input_msg

    case x:
     if ($#cin > 1) then
       pushd $sd_dir
       setenv ROUTINE_TEST yes
       onintr xi
       $file
xi:
       onintr interrupt_end
       unsetenv ROUTINE_TEST
       popd
     endif
     input_msg

    case ver:
      echo ' '
	pushd $sd_dir > /dev/null
        foreach device_test ($test_list)
	  if (-e $device_test) then
            $device_test sd ver
	  else
	    echo $device_test': Not found.'
	  endif
        end
	popd > /dev/null
      input_msg

    case mh:
      via $sd_dir/sysdiag.help
      continue

    case h:
      pushd $sd_dir > /dev/null
      sysdiag.help $file
      if ($status == -1) goto interrupt_end
      popd > /dev/null
      continue

    case f:
      set fop
      set f_command
      set kk

      while ($fop != q)
       if ("$kk" == '') then
         echo ' '
         echo 'a)adm/messages   l)ist       la)st      c)lear files'
	 echo 'mr)make root     t)ar tape   lt)ape     xt)ract tape'
	 echo -n $log_dir2''$f_command': '
	 set kk = $<
       endif

       set fk = ($kk)
       set kk
       if ("$fk" == '') continue
       if ("$fk" == q) break
       if ("$fk" == Q) goto interrupt_end

       set fin = ($fk)
       if ($fin[1] != r) then
         set kinf = ($fin)
       endif
 
       set f_command = " ( $kinf )"

       if ($#kinf > 1) then
         set fop = ($kinf[1])
         if ($#kinf == 2) then
            set f_file = ($kinf[2])
         else set f_file = ($kinf[2-$#kinf])
         endif
       else
          set fop = $kinf
	  set f_file
       endif

       switch ($fop)

         case a:
           via /usr/adm/messages
           continue

         case la:
           echo ' '
           last | more
           f_msg

         case l:
           echo ' '
	   df
           echo ' '
           echo -n '/usr/adm - '
           ls -l /usr/adm
           echo ' '
           echo -n '/usr/crash - '
           ls -l /usr/crash
	   disktop list
           f_msg
 
         case mr:
           pushd $sd_dir
           if ($#kinf > 1) then
             chmod 777 $f_file
             chown root $f_file
	   else
             chmod 777 *
             chmod 777 .*
             chown root *
             chown root .*
           endif
           popd
           f_msg

         case x:
           if ($#kinf > 1) then
             pushd $sd_dir
             $f_file
             popd
           endif
           f_msg

         case t:
           pushd $sd_dir
           chmod 777 *
           chmod 777 .*
           chown root *
           chown root .*
           if ($#kinf > 1) then
	     tar cvf /dev/rst0 $f_file
	   else
	     tar cvf /dev/rst0 .
	   endif
           popd
           f_msg

         case lt:
	   tar tvf /dev/rst0
           f_msg

         case xt:
	   pushd $sd_dir
	   tar xvf /dev/rst0
	   exit

         case c:
	   foreach i (/usr/adm /usr/crash /tmp /usr/tmp)
             set r
             echo ' '
             while ("$r" != y && "$r" != n)
               echo -n ' Clear "'$i'" files? Enter y for yes, n for no, press return. '
               usr_reply
             end 
             if ($r == y) then
               if ($i == /usr/adm) then
                 cp /dev/null /usr/adm/acct
                 cp /dev/null /usr/adm/aculog
                 cp /dev/null /usr/adm/lastlog
                 cp /dev/null /usr/adm/messages
                 cp /dev/null /usr/adm/newsyslog
                 cp /dev/null /usr/adm/savacct
                 cp /dev/null /usr/adm/shutdownlog
                 cp /dev/null /usr/adm/usracct
                 cp /dev/null /usr/adm/wtmp
               else
                 set nonomatch
                 rm -i $i/*
                 unset nonomatch
	       endif
             endif
	   end
	   disktop clean
           echo ' '
	   df
           f_msg
       endsw
      end 
    endsw # end of extra options
end

interrupt_end:

  if (-e $ttys_dir/ttys-sd) then
    mv $ttys_dir/ttys-sd $ttys_dir/ttys
    chmod 644 $ttys_dir/ttys
    if ($ttys_dir != $sd_dir) kill -1 1
  endif

  if (-e $ttys_dir/ttys.sd.temp) then
    rm $ttys_dir/ttys.sd.temp
  endif

  if (-e /dev/sky-sd) then
      mv -i /dev/sky-sd /dev/sky
  endif
endif
