#!/bin/sh
#
# Shell script "BIT".
#
# This script extracts the configuration from the file "DATA.BIT" and uses it
# to start up the various tests used in the burn-in testing.
#
# After the various tests are spun off, this script starts up the shell script
# "watch.BIT".
#
# Since it is assumed that the "DATA.BIT" file was created by the script
# "config.BIT", and is therefore an allowable configuration, this script makes
# no configuration checks.
#
#set -x

CURPATH=`pwd`
export CURPATH

###########################################################################
# Figure out the testing configuration.
###########################################################################

case "$TERM" in
  "")
    echo "TERM=(dumb) \c"
    read TERM
    TERM=${TERM:=dumb};
    export TERM
    ;;
esac

if test "$USER" != "bit"
then
  echo "BIT ABORTED!"
  echo "BIT CAN ONLY BE RUN WHILE LOGGED IN AS 'bit'"
  echo "YOU'RE LOGGED IN AS '$USER'"
  exit 1
fi

echo "$CURPATH" > BITPATH

rm -f $CURPATH/LOCK.BIT.clean

if test -s $CURPATH/ERROR.FLAG
then
  rm -f $CURPATH/ERROR.FLAG
fi

#
# Let's see if the current directory needs cleaning up.
# If it does, then clean.BIT should be run before proceeding.
#
/bin/ls $CURPATH/*.ERR* $CURPATH/*.SAV* $CURPATH/*.PASS* \
	> $CURPATH/$$.temp 2> /dev/null
if test -s $CURPATH/$$.temp
then
  echo ""
  echo "This directory contains files that are normally cleaned up when BIT"
  echo "exits.  The last run may have aborted prematurely.  These files will"
  echo "be cleaned up and results appended to REPORT.BIT."
  echo ""
  /bin/echo "Perform cleanup? <y,n,q> (y) \c"
  read RESPONSE

  while test "$RESPONSE" != "y" -a "$RESPONSE" != "n" -a \
	     "$RESPONSE" != "q" -a "$RESPONSE" != ""
  do
    /bin/echo "PLEASE ENTER 'y' OR 'n' OR 'q' OR '': \c"
    read RESPONSE
  done

  if test "$RESPONSE" = "q"
  then
    exit
  fi

  if test "$RESPONSE" = "y" -o "$RESPONSE" = ""
  then
    $CURPATH/clean.BIT 0
  fi
fi
rm -f $CURPATH/$$.temp


if test ! -s $CURPATH/DATA.BIT
then
  $CURPATH/config.BIT
fi

RESPONSE=
while test "$RESPONSE" != "y"
do

  tput clear
  echo "              MIPS BURN-IN TEST CONFIGURATION"
  echo ""
  cat $CURPATH/DATA.BIT
  echo ""

  /bin/echo "IS THIS THE CORRECT CONFIGURATION? <y,n,q> \c"
  read RESPONSE

  while test "$RESPONSE" != "y" -a "$RESPONSE" != "n" -a "$RESPONSE" != "q"
  do
    if test "$RESPONSE" = ""
    then
      echo ""
    fi
    /bin/echo "PLEASE ENTER 'y' OR 'n' OR 'q': \c"
    read RESPONSE
  done
  echo ""

  if test "$RESPONSE" = "q"
  then
    exit
  fi

  if test "$RESPONSE" != "y"
  then
    $CURPATH/config.BIT
  fi
done


tput clear
SYSTYPE=`grep "^1)" $CURPATH/DATA.BIT | sed "s/^.*> //g"`
SOFTYPE=`grep "^2)" $CURPATH/DATA.BIT | sed "s/^.*> //g"`
SYSNAME=`uname -s`
SERIALN=`grep "^4)" $CURPATH/DATA.BIT | sed "s/^.*> //g"`
MEMORY=`grep  "^5)" $CURPATH/DATA.BIT | sed "s/^.*> //g"`
COMPNUM=`grep "^6)" $CURPATH/DATA.BIT | sed "s/^.*> //g"`
MOTHNUM=`grep "^7)" $CURPATH/DATA.BIT | sed "s/^.*> //g"`
PORTNUM=`grep "^8)" $CURPATH/DATA.BIT | sed "s/^.*> //g"`
DISKNUM=`grep "^9)" $CURPATH/DATA.BIT | sed "s/^.*> //g"`
CARTTAP=`grep "^10)" $CURPATH/DATA.BIT | sed "s/^.*> //g"`
NINETAP=`grep "^11)" $CURPATH/DATA.BIT | sed "s/^.*> //g"`
EXATAPE=`grep "^12)" $CURPATH/DATA.BIT | sed "s/^.*> //g"`
NETWORK=`grep "^13)" $CURPATH/DATA.BIT | sed "s/^.*> //g"`
FPBOARD=`grep "^14)" $CURPATH/DATA.BIT | sed "s/^.*> //g"`
GRAPHIC=`grep "^15)" $CURPATH/DATA.BIT | sed "s/^.*> //g"`
FLOPPY=`grep  "^16)" $CURPATH/DATA.BIT | sed "s/^.*> //g"`
#PRINTER=`grep "^17)" $CURPATH/DATA.BIT | sed "s/^.*> //g"`


if test \( "$NINETAP" != 0 -o "$CARTTAP" != 0 -o "$EXATAPE" != 0 \
           -o "$FLOPPY" != 0 \) \
     -a \( "$MOTHNUM" != 0 -o "$PORTNUM" != 0 \)
then
  echo
  echo ">> MAKE SURE SCRATCH TAPES AND SERIAL PORT JUMPERS ARE IN PLACE <<"
  echo
  /bin/echo "HIT 'RETURN' TO BEGIN TESTING: \c"
  read RETURNKEY

elif test "$NINETAP" != 0 -o "$CARTTAP" != 0 -o "$EXATAPE" != 0 \
          -o "$FLOPPY" != 0
then
  echo
  echo ">> MAKE SURE SCRATCH TAPES ARE IN PLACE <<"
  echo
  /bin/echo "HIT 'RETURN' TO BEGIN TESTING: \c"
  read RETURNKEY

elif test "$MOTHNUM" != 0 -o "$PORTNUM" != 0 
then
  echo
  echo ">> MAKE SURE SERIAL PORT JUMPERS ARE IN PLACE <<"
  echo
  /bin/echo "HIT 'RETURN' TO BEGIN TESTING: \c"
  read RETURNKEY
fi

###########################################################################
# Setup for testing
###########################################################################

trap '$CURPATH/clean.BIT; \
      exit 1' 1 2 3 15

STARTIME=`date`
echo "$STARTIME"         > $CURPATH/TIMESTART.BIT
echo
echo "BIT START TIME: $STARTIME"
sync

echo "First pass in progress " > $CURPATH/pass.full
rm -fr $CURPATH/[0-9]* $CURPATH/PASSCOUNT.BIT
rm -fr $CURPATH/cm.test* $CURPATH/*temp* $CURPATH/*.err

echo "$SYSNAME" > $CURPATH/SYSNAME

#
# The network is now configured by "make install"
#
# ifconfig.BIT 1> /dev/null 2>> $CURPATH/$$.biterrs

if test "$COMPNUM" != 0
then
  echo "CREATING 'cm.test' FILES FOR COMPILE TESTS..."
  cc -g3 -O2 -o $CURPATH/cm.test $CURPATH/src.d/test.c
fi




###########################################################################
# Start Testing
###########################################################################

sync
sync

echo
echo "STARTING SYSTEM MESSAGE MONITORING..."
$CURPATH/sysmsg.BIT 60 /usr/adm/console_log $CURPATH/$$.biterrs \
	1> /dev/null 2>> $CURPATH/$$.biterrs &

if test "$MOTHNUM" != 0
then
  echo "STARTING MOTHER-BOARD SERIAL PORT TESTING... "
  $CURPATH/mport.BIT $MOTHNUM 1> /dev/null 2>> $CURPATH/$$.biterrs &
fi

if test "$PORTNUM" != 0
then
  echo "STARTING SERIAL PORT TESTING... "
  $CURPATH/port.BIT $PORTNUM 1> /dev/null 2>> $CURPATH/$$.biterrs &
fi

# Give the system a chance to get started before disk tests slow it down.
DISK_STARTUP_DELAY=`expr $DISKNUM \* 20`	

CURDISK=0
while test $CURDISK -lt $DISKNUM
do
  echo "STARTING DISK TESTING... DRIVE NUMBER $CURDISK"
  DISKFILE=`grep "^DISK $CURDISK " $CURPATH/CONFIGURATION | \
	    sed -e "s/^.*> //g" -e "s/[ 	]//g"`
  ( sleep $DISK_STARTUP_DELAY ; \
    $CURPATH/disk.BIT $CURDISK $CURPATH $DISKFILE \
    > /dev/null 2>> $CURPATH/$$.biterrs ) &

  CURDISK=`expr $CURDISK + 1`
done
DISKCOUNT="$DISKNUM"
export DISKCOUNT

if test "$FLOPPY" != 0
then
  echo "STARTING FLOPPY DISK DRIVE TESTING... "
  $CURPATH/floppy.BIT 1> /dev/null 2>> $CURPATH/$$.biterrs &
fi

CURCART=0
while test $CURCART -lt $CARTTAP
do
  echo "STARTING CARTRIDGE TAPE DRIVE TESTING... DRIVE NUMBER $CURCART"
  $CURPATH/cart.BIT $CURCART 1> /dev/null 2>&1 &
  CURCART=`expr $CURCART + 1`
done

CURNINE=0
while test $CURNINE -lt $NINETAP
do
  echo "STARTING 9-TRACK TAPE DRIVE TESTING... DRIVE NUMBER $CURNINE"
  $CURPATH/nine.BIT $CURNINE 1> /dev/null 2>&1 &
  CURNINE=`expr $CURNINE + 1`
done

CUREXA=0
while test $CUREXA -lt $EXATAPE
do
  echo "STARTING EXABYTE TAPE DRIVE TESTING... DRIVE NUMBER $CUREXA"
  $CURPATH/exabyte.BIT $CUREXA 1> /dev/null 2>&1 &
  CUREXA=`expr $CUREXA + 1`
done

if test "$NETWORK" != 0
then
  echo "STARTING NETWORK TESTING... "
  $CURPATH/net.BIT 1> /dev/null 2>> $CURPATH/$$.biterrs &
fi

CURCOMP=0
while test $CURCOMP -lt $COMPNUM
do
  echo "STARTING COMPILATION TESTING... COMPILE NUMBER $CURCOMP"
  cp $CURPATH/src.d/test.c $CURPATH/cm.test$CURCOMP.c
  $CURPATH/comp.BIT cm.test$CURCOMP $CURCOMP 1> /dev/null 2>> $CURPATH/$$.biterrs &           
  CURCOMP=`expr $CURCOMP + 1`
done

# Thanks to Chris Rowen for 'fptype'.
if test "$FPBOARD" != "0"
then
  FPUTYPE=`fptype`
  case $FPUTYPE in
    software)
      /bin/echo "\c"
      ;;
    *)
      ;;
  esac
  echo "STARTING FLOATING POINT UNIT ($FPUTYPE) TESTING... "
  echo $FPUTYPE > $CURPATH/FPUTYPE
  $CURPATH/fp.BIT 1> /dev/null 2>> $CURPATH/$$.biterrs &

  if test "$FPUTYPE" = "R6010"
  then
    echo "STARTING FLOATING POINT UNIT ($FPUTYPE) TESTING FOR MIPS2... "
    echo $FPUTYPE > $CURPATH/FPUTYPE
    $CURPATH/fp.BIT mips2 1> /dev/null 2>> $CURPATH/$$.biterrs &
  fi

fi

DELAY=`grep "^GRAFLAP" $CURPATH/CONFIGURATION | sed "s/^.*> //g"`
if test "$GRAPHIC" != 0
then
  echo "STARTING GRAPHICS DISPLAY TESTING... "
  $CURPATH/graphic.BIT -server $SYSNAME:0 -delay $DELAY 1> /dev/null 2>> $CURPATH/$$.biterrs &
fi


# This test is started last.  This way, everyone else gets their
# share of memory.  The memory test will then detect and use whatever 
# free memory is left over.  
#
# Only the disk tests have not started yet.  Keeping the disk buffer space
# small is desirable so that real disk I/O will be performed by the disk tests.
#
# A mem test is started for each 32MB of available memory.  An extra test is
# is started for the remainder.  Some breathing room is left over for general 
# use by the OS -- 6MB-9MB on systems with lots of free memory, and 
# 0MB-6MB on systems with little memory (causing system to swap).  Also add
# in 1MB for each disk in the system for minimal disk buffer space.
#

if test "$MEMORY" != 0
then
  MAXALLOC=32014336
  MINFREE=`expr 6012928 + $DISKNUM \* 1015808`
  MAXFREE=`expr 9011200 + $DISKNUM \* 1015808`

  echo "STARTING MEMORY TESTING..."
  sleep 10	# let other tests get started, then see how much is left
  FREEMEM=`mem.BIT FREE | awk '{ print $1 }'`
  
  CURMEM=1
  while test $FREEMEM -gt $MAXFREE -o $CURMEM -eq 1
  do
    if test $CURMEM -ne 1
    then
      echo "STARTING EXTRA MEMORY TEST FOR LARGE MEMORY... TEST NUMBER $CURMEM"
    fi
  
    if test $FREEMEM -ge `expr $MAXALLOC + $MINFREE`
    then				# if >= (32+6)MB,
      ALLOC=$MAXALLOC			#   then malloc 32MB.
    elif test $FREEMEM -ge $MAXFREE	# if < 38MB && >= 9MB,
    then				#   then malloc all but 6MB
      ALLOC=`expr $FREEMEM - $MINFREE`	#
    else				# else, there is only one mem test,
      ALLOC=				#   and there is very little memory,
    fi					#   so let mem.BIT get what it can.
  
    $CURPATH/mem.BIT $ALLOC 1> /dev/null & # error output handled by parent
    until test -s $!.OPTmem
    do
      sleep 1	# wait for memory test to malloc memory
    done  
    USED=`cat $!.OPTmem | awk '{print $1}'`
  
    FREEMEM=`expr $FREEMEM - $USED \* 1000`
    CURMEM=`expr $CURMEM + 1`
  done
fi

echo "BIT JOB NUMBER IS $$"
echo
echo "SCREEN UPDATE MAY TAKE A WHILE.  PLEASE BE PATIENT."
$CURPATH/watch.BIT $$
