#!/bin/csh -f
# $Id: install-sun4-driver,v 1.4 1991/10/01 04:33:21 sean Rel $
#
# Auxiliary installation script 
#
# Configure an SunOS kernel to support the CM. This script is passed the CM
# runtime directory as its only argument.
#

set RTdir=$1
set sysdir=${RTdir}/sys/

cd ${sysdir}

if ($?DEBUG) then
	set DEBUG=/bin/echo
else
	set DEBUG=""
endif

set BackupSuffix=`date +"%%TMC%%%h_%d_%y"`

if (! { sun4 } ) then
	if ( ! { sun4c } ) then
		if ( ! { sun4m } ) then
			echo "This is not an Sun4 system"
			exit 1
		else
			set fearch=sun4m
		endif
	else
		set fearch=sun4c
	endif
else
	set fearch=sun4
endif

# set up to do reads with possible shell escape
# Depends on variable "rp" being set to something reasonable

rm -f /tmp/myread
cat << "EOSC" >/tmp/myread
set ans='\!'
while ("X$ans" =~ "X\!")
    set ans=$<
    switch ( "$ans" )
    case '\!':
	echo Entering a sub-shell. Type ctrl-D or \"exit\" to return.
	${SHELL} -i
	echo " "
	echo -n "$rp "
	breaksw
    endsw
end
set rp='Your answer:'
"EOSC"

# Determine where the kernel is made.

set dflt=/sys
set sys='blurfl/dyick'
while ( ! -d ${sys}/. )
	switch ( "$sys" )
	  case "blurfl*":
		breaksw;
	  default:
	    	echo "$sys does not appear to exist or is not a directory." ;
	endsw
	echo " "
	set rp="Where do you build kernels? [$dflt]"
        echo -n "$rp "
        source /tmp/myread
        set sys="$ans"
	if ("$sys" == "") set sys=$dflt
end

set machine=`hostname | sed -e 's/\..*//' | tr a-z A-Z`
set dflt=${sys}/${fearch}/conf/${machine}

again2:
echo " "
set rp="What is the name of your system configuration file? [$dflt]"
echo -n "$rp "
source /tmp/myread
set configfile="$ans"
if ("$configfile" == "") set configfile=$dflt
if ( ! -f $configfile ) then
	echo "$configfile does not exist"
	sleep 1
	goto again2
endif

# 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 CMMAJOR=105
     breaksw
case "4.1.1":
case "4.1":
     set CMMAJOR=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

# 
#
# Files in ${sys}/suncm
# (cm.c cmreg.h cmsoft.h)
#

set DriverFiles="cm.c cmreg.h cmsoft.h cmioctl.h cmacct.h nexus-code.h attach.h cm_acct.c"

echo ""
echo "Updating ${DriverFiles} in ${sys}/suncm"
if (! -e ${sys}/suncm ) then
    ${DEBUG} rm -f ${sys}/suncm
    ${DEBUG} mkdir ${sys}/suncm
endif

(${DEBUG} cd ${sys}/suncm; ${DEBUG} rm -f ${DriverFiles})

${DEBUG} cp ${DriverFiles} ${sys}/suncm

if (! -e /usr/include/suncm) then
	${DEBUG} ln -s ${sys}/suncm /usr/include/suncm
else
	set foo = `(cd ${sys}/suncm; /bin/pwd)`
	set bar = `(cd /usr/include/suncm; /bin/pwd)`
	if ("$foo" != "$bar") then
		${DEBUG} cp ${sys}/suncm/*.h /usr/include/suncm 
	endif
endif


#
# Files in ${sys}/sun (conf.c)

echo ""
echo "Updating ${sys}/sun/conf.c (original saved as ${sys}/sun/conf.c${BackupSuffix})"

${DEBUG} mv ${sys}/sun/conf.c ${sys}/sun/conf.c${BackupSuffix}
${DEBUG} cp sun4-${SunOSVersion}/conf.c ${sys}/sun/conf.c

#
# Files in /sys/conf.common
# May have to delete old driver entry.
#

if (! { grep -s "optional cm device-driver" ${sys}/conf.common/files.cmn } ) then
	echo ""
	echo "adding cm.c to ${sys}/conf.common/files.cmn"
	${DEBUG} cc -E files.cm | sed -e '/^#/d' | sed -e '/^$/d' >> ${sys}/conf.common/files.cmn
else
	echo ""
	echo "Updating ${sys}/conf.common/files.cmn ..."
	${DEBUG} ed - ${sys}/conf.common/files.cmn <<EoI >& /dev/null
/optional cm device-driver/d
/cm_acct.c/d
w
q
EoI
	${DEBUG} cc -E files.cm | sed -e '/^#/d' | sed -e '/^$/d' >> ${sys}/conf.common/files.cmn
endif

#
# Files in /sys/sun4/conf
#

#
# Make sure that SHMEM support is there
#
echo ""
echo "Checking for shared memory support in your kernel configuration ..."
if (! { grep -s "^option.*IPCSHMEM" $configfile } ) then
	echo options IPCSHMEM >> $configfile
	echo "option IPCSHMEM added to $configfile"
endif
if (! { grep -s "^option.*IPCSEMAPHORE" $configfile } ) then
	echo options IPCSEMAPHORE >> $configfile
	echo "option IPCSEMAPHORE added to $configfile"
endif
if (! { grep -s "^option.*IPCMESSAGE" $configfile } ) then
	echo options IPCMESSAGE >> $configfile
	echo "option IPCMESSAGE added to $configfile"
endif

if (! { grep -s "vector cmintr" $configfile } ) then
	cat cm.config.sun4 >> $configfile
	echo "Updated $configfile"
else
	echo "cm device already listed in $configfile"
endif
#

#
# Set up MAKEDEV.cm

echo ""
echo "Setting up /dev/MAKEDEV.cm"

${DEBUG} rm -f /dev/MAKEDEV.cm
if (-e /dev/cm00) then
  ${DEBUG} rm -f /dev/cm?? /dev/cm
endif
${DEBUG} sed -e "s/^cmmajordev=CMMAJOR/cmmajordev=${CMMAJOR}/" < MAKEDEV.cm >/dev/MAKEDEV.cm
${DEBUG} chmod 755 /dev/MAKEDEV.cm

#
# Update kern_exit.o
#
# We're lucky that in 4.1/4.1.1 kern_exit.o is the same in the sun4c
# architecture as in the sun4 architecture.
#
echo ""
echo "Installing new kern_exit.o"

${DEBUG} mv ${sys}/${fearch}/OBJ/kern_exit.o ${sys}/${fearch}/OBJ/kern_exit.o${BackupSuffix}
${DEBUG} cp sun4-$SunOSVersion/kern_exit.o.${fearch} ${sys}/${fearch}/OBJ/kern_exit.o

echo " "
echo "The kernel files have been installed"
echo " "
echo "You should now check ${sys}/sun/conf.c to verify"
echo "the CM major device number there, and that it matches the "
echo "CM major device number in /dev/MAKEDEV.cm"
echo " "
echo "Now rebuild your kernel, run /dev/MAKEDEV.cm, and reboot your system"
echo " "

done:



