#!/bin/csh -f
#
# Install the CMIOP/VMEIO device drivers
#

# ----------------------------------------------------------------------------
set path=(/bin /usr/bin /usr/ucb /usr/local /usr/local/etc /etc /usr/etc .)

set VMEIO_MAJOR=104
set STC_MAJOR=105
set DEBUG=""
alias BackUpFile 'set now=`date`; if ( ! -f \!*%TMC%${now[2]}_${now[3]}_${now[6]}) sh -c "${DEBUG} mv \!* \!*%TMC%${now[2]}_${now[3]}_${now[6]} && echo \!* saved as \!*%TMC%${now[2]}_${now[3]}_${now[6]}"'
alias CopyBackUpFile 'set now=`date`; if ( ! -f \!*%TMC%${now[2]}_${now[3]}_${now[6]}) sh -c "${DEBUG} cp \!* \!*%TMC%${now[2]}_${now[3]}_${now[6]} && echo \!* saved as \!*%TMC%${now[2]}_${now[3]}_${now[6]}"'

set home = `pwd`
set date = `date`
set whoiam = `whoami`

# Determine the version of SunOS

/usr/ucb/strings /vmunix | grep SunOS > /tmp/$$
if ($status == 0) then
	set version=`cat /tmp/$$`
	set SunOSVersion=`echo $version[3] | sed -e 's/_EXPORT//' | sed -e 's/_ER//'`
else
	set SunOSVersion="Unknown"
endif

echo ""
echo "You are running SunOS version $SunOSVersion"

rm -f /tmp/$$

switch ("$SunOSVersion")
case "4.1.2":
     set VMEMAJOR=105
     breaksw
case "4.1.1":
     set VMEMAJOR=104
     breaksw
default:
     echo "The version of SunOS running on this system, $SunOSVersion"
     echo "is not directly supported by the installation script"
     echo " "
     echo "See Appendix A of the installation guide for instructions"
     echo "on how to configure the SunOS kernel to support the"
     echo "Connection Machine system hardware and software."
     echo " "
     goto done
endsw

#
# Determine what kind of sun we are
#
if (! { sun4 } ) then
	if ( ! { sun4m } ) then
		echo "This is not an Sun4 or Sun4M system"
		exit 1
	else
		set fearch=sun4m
	endif
else
	set fearch=sun4
endif

#
# Find out where the admin. builds their kernels
#
qfindkvm:

set qtag = findkvm
set q = "Where do you build your kernels? "
set default = "/sys"
goto ask

help-findkvm:
   /bin/echo This section of the installation is designed to modify your kernel
   /bin/echo building area to install the VMEIO, Ciprico SCSI and StorageTek tape
   /bin/echo drivers.  The pathname you give at this prompt should be the root
   /bin/echo of a SunOS kernel area \(/sys on most systems, /usr/kvm/sys on others\).
   goto qfindkvm

ret-findkvm:

set sysdir = $a

if ( ! -d $sysdir/. ) then
   /bin/echo
   /bin/echo Directory $sysdir does not exist\!
   /bin/echo
   goto qfindkvm
endif

BackUpFile $sysdir/sundev/sc_conf.c
cp $home/sys/sundev/sc_conf.c $sysdir/sundev

BackUpFile $sysdir/sun/conf.c
cp $home/sys/sun/conf.c.$VMEMAJOR $sysdir/sun/conf.c

mkdir -p $sysdir/tmc
cp $home/sys/tmc/* $sysdir/tmc

rm -f /usr/include/tmc
ln -s $sysdir/tmc /usr/include/tmc

CopyBackUpFile $sysdir/$fearch/conf/files
ed - $sysdir/$fearch/conf/files <<EoI >& /dev/null
/optional stc device-driver/d
/optional cipc device-driver/d
/optional vmeio device-driver/d
w
q
EoI

cat $home/sys/$fearch/conf/files.tmc >> $sysdir/$fearch/conf/files

cp $home/sys/$fearch/conf/CMIOP $sysdir/$fearch/conf

#
# Configure the kernel
#

cd $sysdir/$fearch/conf

/bin/echo
/bin/echo "Configuring kernel..."

if { /etc/config CMIOP } then
	echo 'System configuration complete'
else
	echo '* * * * '
	echo '* Error in system configuration'
	echo '* Please check the kernel configuration'
	echo '* files and build your kernel manually'
	echo '* The special devices have NOT been created'
	exit 1
endif

/bin/echo "Building kernel..."
cd ../CMIOP
if { make vmunix } then
	/bin/echo "New kernel built successfully"
	/bin/echo
else
	echo '* * * * '
	echo '* Error building new operating system kernel'
	echo '* Please check the kernel configuration'
	echo '* files and build your kernel manually'
	echo '* The special devices have NOT been created'
	exit 1
endif

echo 'Installing new kernel as /vmunix'
echo '(old kernel saved as /vmunix.old)'
mv /vmunix /vmunix.old
mv vmunix /

echo 'Creating special device files'
rm -f /dev/{cmio*,nrstc*,rstc*}
mknod /dev/cmio0 c $VMEIO_MAJOR 0
mknod /dev/cmio1 c $VMEIO_MAJOR 1
mknod /dev/cmio2 c $VMEIO_MAJOR 2
mknod /dev/cmio3 c $VMEIO_MAJOR 3
mknod /dev/nrstc0 c $STC_MAJOR 4
mknod /dev/nrstc1 c $STC_MAJOR 5
mknod /dev/nrstc2 c $STC_MAJOR 6
mknod /dev/nrstc3 c $STC_MAJOR 7
mknod /dev/rstc0 c $STC_MAJOR 0
mknod /dev/rstc1 c $STC_MAJOR 1
mknod /dev/rstc2 c $STC_MAJOR 2
mknod /dev/rstc3 c $STC_MAJOR 3
chmod 666 /dev/{cmio*,nrstc*,rstc*}

cat <<EoStuff

CMIOP software installation is complete.  You should edit /etc/rc.local to
set up the proper CMIO bus id and station for the vioconfig command.  Also
set up the cm-logger parameters appropriate to your site.

Then reboot the system.

EoStuff

exit


#==========================================================================
# question asker. setup beforehand: q (question text), default (default answer) and
# qtag (question name). does a "goto ret-$qtag" on success, does a "goto help-$qtag"
# if the user asked for help. Returns users response in variable 'a'.

ask:
/bin/echo ""
reask:
/bin/echo -n "$q [$default] : "
set a = $<
if ("$a" == "") then
  if ("$default" != "") then
    set a = "$default"
  else
    /bin/echo '?There is no default'
    goto reask
  endif
endif
if (("$a" == "quit") || ("$a" == "QUIT") || ("$a" == "Quit")) goto abort
if ( "$a" == "\!" ) then
  echo 'Entering CSH subshell. Type ctrl-D or "exit" to return.'
  csh -i
  echo "\!"
  echo ""
  goto reask
endif
if (("$a" == "Y") || ("$a" == "YES") || ("$a" == "Yes") || ("$a" == "y") || ("$a" == "yes")) set a = 1
if (("$a" == "N") || ("$a" == "NO") || ("$a" == "No") || ("$a" == "n") || ("$a" == "no")) set a = 0
if (("$a" == "?") || ("$a" == "help") || ("$a" == "HELP")) then
  /bin/echo ""
  goto help-$qtag
endif
goto ret-$qtag

#yes/no version. sets "a" to 1 (yes) or 0 (no), otherwise, same as ASK above
ask-y-n:
/bin/echo ""
reask-y-n:
/bin/echo -n "$q [$default] : "
set a = $<
if ("$a" == "") then
  if ("$default" != "") then
    set a = "$default"
  else
    /bin/echo '?There is no default'
    goto reask-y-n
  endif
endif
if (("$a" == "?") || ("$a" == "help") || ("$a" == "HELP")) then
  /bin/echo ""
  goto help-$qtag
endif
if ("$a" == "\!") then
  echo 'Entering CSH subshell. Type ctrl-D or "exit" to return.'
  csh -i
  echo "\!"
  echo ""
  goto reask-y-n
endif
if (("$a" == "quit") || ("$a" == "QUIT") || ("$a" == "Quit")) goto abort
if (("$a" == "Y") || ("$a" == "YES") || ("$a" == "Yes") || ("$a" == "y") || ("$a" == "yes")) then
    set a = 1
else if (("$a" == "N") || ("$a" == "NO") || ("$a" == "No") || ("$a" == "n") || ("$a" == "no")) then
    set a = 0
else
    /bin/echo 'Please answer "yes" or "no"'
    goto reask-y-n
endif
endif
goto ret-$qtag
