#!/bin/csh
#
# @(#)dni_install.sh 7.13 91/02/28 SMI
#
#   Copyright (c) 1988 , 1989, 1990 by Sun Microsystems, Inc.
#
#
#  dni_install:  Cshell Script to  Configure the Kernel for Sunlink
#	DNI support.
#
#   Arguments:
#
#   Exit Conditions:
#
#	In any case where an error in this procedure is detected, an immediate
#  exit with a negative code will be taken.
#
#
unalias rm
unalias cat
unset noclobber
setenv PATH /usr/ucb:/bin:/usr/bin:/etc:/usr/etc
set INS = "install"
set ulib = "/usr/lib"
#
#   The old /sys directory is now a link to /usr/kvm/sys ..
#
set SYS = "sys"
set SYS_PATH = "/usr/kvm/$SYS"
set SYS_INS = "sys_$INS"
set SYSN = "System Config File"
set DNI_PATH = "/usr/sunlink/dni"
set DNIS_PATH = "$DNI_PATH/$SYS"
set DNNI_PATH = "$DNI_PATH/$INS"
set CONF = "conf"
set FILE = "files"
set osh = "dni_rc_ins"
set GEN_ADD = "GENERIC.add"
set DEV_MAKE = "MAKEDEV.dni"
set DEV_NUM = 47
set SunX = `arch -k`
#	
#	The old "roadrunner" flag was used to install dni 6.0 on the Sun 386i
#
#	The major difference of the 386i installation was that the dni driver (the
#  kernel stuff) could be loaded at boot time, so that kernel reconfiguration was
#  unneccessary.  Since the "loadable driver" feature of Sun O/S 4.1 may be used
#  someday, it was felt the conditional code for the Sun 386i should be maintained.
#
#	When loadable driver support becomes available, set loadable to 1
#

@ loadable = 0
set oname = $0

set DEF_DNI_PATH = /usr/sunlink/dni
set DEF_DNID_PATH = $DEF_DNI_PATH/deinstall
set CLSHELL = dni_deinstall
set floc = "/etc/rc.local"
set extest = "dniserver dnaserver"
#
#		Verify operator's desire to run this script
#
echo " "
echo "	You have invoked the $oname:t script.  It is designed"
echo "	to install the DNI software on this node."
echo " "
echo "	This script will only work successfully if DNI has NEVER been"
echo "	previously installed on this node.  If a version of DNI 7.0 or"
echo "	DNI 6.0 has already been installed, you must invoke the"
echo " "
echo "		$DEF_DNID_PATH/$CLSHELL"
echo " "
echo "	 script before continuing."
echo " "
echo " "
echo "	In addition the following must be true:"
echo " "
@ num = 1
echo "	$num. $floc must either have 'exit 0' as its last line"
echo "	or simply end with no 'exit' statement at all"
echo " "
@ num++
echo "	$num. You must be logged in as root."
echo " "
@ num++
echo "	$num.  You must know the Ethernet Controller type (le or ie),"
echo "	and the unit number (0 / 1) of the ethernet interface you are"
echo "	going to run dni through. You have the following ethernet "
echo "	interfaces built into your current kernel :- "
echo " "
echo -n "        "
netstat -ria | egrep '(le|ie)'| awk '{print $1}'
echo " "
@ num++
echo "	$num.  You must know the DECnet area and local node numbers"
echo "	which you wish to assign to this node.  The DECnet address must"
echo "	be unique on your network."
echo " "
@ num++
echo "	$num.  If you want to install the VMS Mail Gateway, make sure,"
echo "	that a file /etc/sunlink/dni/dnihosts exists. This file contains"
echo "	a list of (VAX) node names that you want to send mail to. "
echo " "
echo " "
echo "	If you need to satisfy one of these conditions, exit the script."
echo "	You then either restart this script or follow the instructions"
echo "	for manually configuring your workstation in the DNI System"
echo "	Administrator's Guide."
echo " "
echo " "
echo -n "Do you wish to continue with this script [Y/N]:  "
set ans = ($<)
if (($ans != "y") && ($ans != "yes") && ($ans != "Y") && ($ans != "YES")) then
   echo " "
   echo "	You have chosen to complete the installation in manual mode"
   echo "	or will be running deinstall before continuing."
   echo "			Good Luck"
   exit (1)
endif
#
#   Verify if script was run before
#
echo " "
foreach idx ($extest)
    grep $idx $floc
    if ($status != 1) then
	echo " Traces of a previous DNI/DNA installation in $floc"
	echo " Please run deinstall script and retry installation"
	exit (-1)
    endif
end

#
#  ***********************************************
#

cd /
echo " "
echo "	$SunX installation is proceeding"
echo " "
#			Verify that owner is root
set me = `whoami`
if ($me != "root") then
   echo "This script must be run as root ... fatal error"
   exit (-1)
endif
if (!(-w $DNI_PATH)) then
    echo "No root write access to $DNI_PATH .. fatal error"
    exit (-1)
endif
#
#		Verify operator's desire to run this script
#
echo "	This script is designed to continue the DNI installation"
echo "	on this node by:"
echo " "
#
if ($loadable) then
   echo "		Running $SYS_INS"
else
   echo "		Creating/confirming the $SYS_PATH Link"
   echo " "
   echo "		Selecting a preexisting $SYSN"
   echo " "
   echo "		Changing protections on various files"
   echo " "
   echo "		Running $SYS_INS"
   echo " "
   echo "		Modifying the system config file"
   echo " "
   echo "		Configuring and building the new kernel"
   echo " "
   echo "		Setting up the new Kernel image for booting"
   echo " "
   echo "		Creating the DNI devices in /dev"
endif
echo " "
echo "		Optionally running the $osh Cshell script."
echo " "
echo " "
echo "	If this is a client node rather than a server node, this script"
echo "	should NOT be run.  You should exit and run the $osh "
echo "	Cshell script directly."
echo " "
echo "	After the $osh script finishes, you must halt the system"
echo "	and reboot using the new kernel."
echo " "
echo -n "Do you wish to continue with this script [Y/N]:  "
set ans = ($<)
echo " "
if (($ans != "y") && ($ans != "yes") && ($ans != "Y") && ($ans != "YES")) then
   echo " "
   echo "	You have chosen to exit the $0 DNI Installation script."
   echo "			Good Luck"
   exit (0)
endif
#
if ($loadable) then
    mount -o remount,rw /usr
endif
#
if (!(-w $ulib)) then
   echo "No root write access to $ulib ... fatal error"
   echo "This script cannot be run on a client"
   exit (-1)
endif
#
#   Create /etc/sunlink/dni directory path if it does not exist
#
set od = `pwd`
cd /etc
set dscnd = (sunlink dni)
foreach i ($dscnd)
   if (!(-d $i)) then
	mkdir $i
	if ($status) then
	    echo "Error in making /etc/sunlink/dni hierarchy"
	    exit (-1)
	endif
    endif
    cd $i
end
cd $od
#


#
if (!($loadable)) then
#
#				1. Create Link
#
   if (!(-d $SYS_PATH)) then
	ln -s ./$SYS_PATH $SYS
	if ($status) then
	   echo "Error in creating /$SYS link to ./$SYS_PATH ... fatal"
	   exit (-1)
	endif
	echo "	Link to ./$SYS_PATH created at /$SYS ..."
    else
	echo " /$SYS Directory link confirmed"
    endif
    echo " "
#
#				2A. Get System Name
#
    set CONF_PATH = /$SYS/$SunX/$CONF
    if (!(-d $CONF_PATH)) then
	echo "No $CONF_PATH directory ... fatal"
	exit (-2)
    endif
    if (!(-w $CONF_PATH)) then
	echo "$CONF_PATH directory not writable .. please install DNI on server"
	exit (-2)
    endif
    cd $CONF_PATH
    if (!(-e $FILE)) then
	echo "Error:  Must be entry $FILE in directory $CONF_PATH"
	exit (-3)
    endif
#				2. Select System Name
sloop:
    echo " Please select a preexisting $SYSN (ex: GENERIC, DNI, ...)."
    echo " Performance is improved if the $SYSN is tailored specially for"
    echo " your machine. Default is the $SYSN your current kernel was "
    echo " built from."
    echo " "
#get kernel name from motd to use as default
set name = `grep SunOS /etc/motd | tr '()' '  ' `
set name = $name[4]
    echo -n "$SYSN [$name]:"
    set ans = ($<)
    if ( $ans == "") then
	set ans = $name
    endif
    if (!(-e $ans)) then
        echo "Error:  No such $SYSN as $ans found in $CONF_PATH directory"
        goto sloop
    endif
    echo " "
        echo "The new $SYSN will be based on $ans"
    set SYS_NAME = $ans

dloop:
    echo " "
    echo " What name do you want it called after the DNI additions ?"
    echo " The default action is to use the same name, with the original"
    echo " being saved, (GENERIC and GENERIC_SMALL can't be altered so "
    echo " you must select another name eg HOSTNAME.DNI). "
    echo " "

#no default if copying from generic config files
    if (($SYS_NAME == "GENERIC") || ($SYS_NAME == "GENERIC_SMALL")) then
           set name = ""
    else
           set name = $SYS_NAME
    endif
    echo -n "new $SYSN [$name]:"
    set ans = ($<)
    set ans = `echo $ans | tr 'a-z' 'A-Z'`
    echo " "

# don't want people overwritting generic config files
# so no default if from generic
    if (($ans == "") && ($name == "")) then
            echo "GENERIC and GENERIC_SMALL MUST be renamed "
            goto dloop
    endif

# cant copy onto generic so choose another name
    if (($ans == "GENERIC") || ($ans == "GENERIC_SMALL")) then
            echo "cannot copy onto GENERIC or GENERIC_SMALL"
            goto dloop
    endif

    if (($ans == "") || ($ans == $SYS_NAME)) then
        echo " copying $SYS_NAME to $SYS_NAME.$$ for safety "
        cp $SYS_NAME $SYS_NAME.$$
	if (!($status == 0)) then
		echo " ERROR unable to copy backup file. This script can be"
		echo " restarted after the problem is fixed "
		exit (-2)
	endif
    else
	if (-e $ans) then
		echo " file $ans already exists, copying it to $ans.$$ "
		cp $ans $ans.$$
        	if (!($status == 0)) then
               	 echo " ERROR unable to copy backup file. This script can be"
                 echo " restarted after the problem is fixed "
                 exit (-2)
        	endif 
	endif
        echo " new $SYSN will be $ans "
        echo " copying $SYS_NAME to $ans "
        cp $SYS_NAME $ans
        if (!($status == 0)) then
                echo " ERROR unable to copy backup file. This script can be"
                echo " restarted after the problem is fixed "
                exit (-2)
        endif 
        set SYS_NAME = ($ans)
    endif
    echo ""
#				3. Change Protections
    chmod u+w $FILE $SYS_NAME
    if ($status) then
	echo "Change protection error for $FILE or $SYS_NAME in $CONF_PATH"
	exit (-4)
    endif
    echo "	File protection on $FILE and $SYS_NAME adjusted ...."
endif
echo " "
#				4. Run sys_install
if (!(-d $DNIS_PATH)) then
    echo "Error .. $DNIS_PATH does not exist"
    exit (-5)
endif
if (!(-x $DNIS_PATH/$SYS_INS)) then
    echo "Error .. can't find $DNIS_PATH/$SYS_INS"
    exit (-6)
endif
cd $DNIS_PATH
$DNIS_PATH/$SYS_INS
set sst = $status
if ($sst) then
   echo "Error code $sst during execution of $DNIS_PATH/$SYS_INS"
   exit (-6)
endif
echo "	$SYS_INS script run successfully ...."
echo " "
#				5. Modify the System Config File
if (!($loadable)) then
    cd $CONF_PATH
    if (!(-e $DNIS_PATH/$GEN_ADD)) then
	echo "Error ... $DNIS_PATH/$GEN_ADD does not exist"
	exit (-7)
    endif
#
#    Due to the exciting addition of vmunix_small to 4.1 .. we have to get the
#  name of the resulting image in the new system config directory, so we can
#  copy it to /vmunix after a successful make.  This name is found as the second 
#  field of the line where the string "config" is the first field (saved in
#  $vmorig[2] below).
#
    set vmorig = `grep '^config' $SYS_NAME`
    @ st = $status
    if ($st) then
	echo "Error .. $SYS_NAME has no config line"
	exit (-8)
    endif
#
#    Determine if extra DNI lines already added to config file.  We will assume
# the worst if the string "dnalink32" is present.  In that case (grep -s returns 0)
# we will not append the new lines.
#
    grep -s dnalink32 $SYS_NAME
    @ st = $status
    if ($st == 2) then
	echo "Error ... Cannot $grep $SYS_NAME"
	exit (-8)
    else if ($st == 0) then
	echo " "
	echo " ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
	echo " "
	echo "	Warning ... DNI-like entries already in $SYS_NAME"
	echo " They will not be added now."
	echo " "
	echo " ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ "
   else
	cat $DNIS_PATH/$GEN_ADD >> $SYS_NAME
	set sst = $status
	if ($sst) then
	    echo "Error code $sst during append of $DNIS_PATH/$GEN_ADD to $SYSN"
	    exit (-8)
	endif
	echo "	System Config file $SYS_NAME successfully modified ..."
    endif
    echo " "
#				6. Configure and build kernel
    cd $CONF_PATH
    set excmd = "/etc/config $SYS_NAME"
    $excmd
    set sst = $status
    if ($sst) then
	echo "Error code $sst during execution of ++$excmd++"
	exit (-6)
    endif
    echo " "
    echo "	$excmd   run successfully ...."
    echo " "
    if (!(-d ../$SYS_NAME)) then
	echo "Error ... no such directory as `pwd`/../$SYS_NAME"
	exit (-7)
    endif
    cd ../$SYS_NAME
    echo "	Issuing make ...  suggest taking a coffee break about now ..."
    echo " "
    make
    set sst = $status
    if ($sst) then
	echo "Error code $sst during execution of ++make++"
	exit (-8)
    endif
    echo " "
    echo "	'make' ran successfully ...."
    echo " "
#				7. Set up the kernel for booting
    set vmu =  "vmunix"       
    if (!(-e $vmorig[2])) then
	echo "Error .. $vmorig[2] does not exist in $CONF_PATH"
	exit (-9)
    endif
# well the new kernel is in $vmorig[2] 
#				8.  Create /dev/dni device files
    cd /dev
    set excmd = $DNIS_PATH/$DEV_MAKE
    if (!(-x $excmd)) then
	echo "Cannot locate $excmd ..."
	exit (-11)
    endif
    $excmd $DEV_NUM
#
#  Note ... no error returns from $DEV_MAKE
#
    echo "	$excmd $DEV_NUM  run successfully  ..."
    echo "	/dev/dni'xx' device files created ..."
    echo " "
endif
#
#	Now give instructions for next script
#
cd $DNNI_PATH
echo " "
echo "	Successful completion of this phase."
echo "	To complete the DNI installation you must  'cd' to"
echo " "
echo "		$DNNI_PATH"
echo " "
echo "	and invoke the $DNNI_PATH/$osh Cshell"
echo "	script to modify the /etc/rc.local file for DNI use."
echo " "
echo "	You can do this automatically by saying yes to the"
echo "	next question.   Otherwise refer to the DNI System Administrator's "
echo "	Guide for further instructions."
echo " "
echo -n "Do you wish to automatically invoke the $osh script [Y/N]:  "
set ans = ($<)
if (($ans != "y") && ($ans != "yes") && ($ans != "Y") && ($ans != "YES")) then
   echo " "
   echo "	You have chosen to exit the $0 DNI Installation script."
   echo "			Good Luck"
   exit (0)
endif
echo " "
if (!(-x $osh)) then
   echo "Error .. $osh does not exist in $DNNI_PATH or is not executible"
   exit (0)
endif
$DNNI_PATH/$osh
set sst = $status
if ($sst) then
    echo " "
    if ($sst == 1) then
	echo " "
	echo "Normal exit, but DNI installation is still incomplete."
	echo " "
	exit (1)
    endif
    echo "Error code $sst during execution of  $osh"
    echo " "
    echo -n "Do you wish to continue DNI installation [Y/N]:  "
    set ans = ($<)
    if (($ans != "y") && ($ans != "yes") && ($ans != "Y") && ($ans != "YES")) then
	echo " "
	echo "	You have chosen to exit the $0 DNI Installation script."
	echo "			Good Luck"
	exit (0)
    endif
else
   echo "  $osh completed successfully ...."
endif
echo " "
echo " "
echo "	About to copy the new kernel to /vmunix, the current /vmunix "
echo "	will be saved in /vmunix.$$         ( write this down NOW ). "
echo " "
cp /$vmu /$vmu.$$
if ( $status ) then
	echo " Error in saving  original /vmunix, original won't be "
	echo " overwritten, The installation will not abort, but you"
	echo " must arrange for the new vmunix to be booted correctly"
	echo -n " Hit return to continue:  "
	set ans = ($<)
	echo " "
else
    cd $CONF_PATH
    cd ../$SYS_NAME
    set excmd = "cp $vmorig[2] /vmunix"
    $excmd
    set sst = $status
    if ($sst) then
	echo "Error code $sst during execution of ++$excmd++"
	exit (-8)
    endif
    echo "	++$excmd++  run successfully ...."
    echo " "
endif
#
#	Now give instructions for reboot
#
if (!($loadable)) then
    echo " "
    echo "********************************************************************"
    echo " "
    echo "	DNI has apparently been installed correctly.  To verify the"
    echo "	installation you must type the following:"
    echo " "
    echo "	hostname# /etc/reboot  ( boots the new /vmunix)"
    echo " "
    echo "	At that point, the DNI installation procedure will be"
    echo "	complete and the system will be ready for user logins."
    echo " "
    echo " "
    echo " "
    echo "	If the new kernel doesn't boot, reboot the original kernel "
    echo "	that was saved earlier in the scripts, once this has booted "  
    echo "	to single user mode copy the saved /vmunix onto /vmunix , "
    echo "	move the dni rc script out of the way, then go to multi- "
    echo "	user with a ^D "
    echo " "
    echo "	eg from the monitor prompt :-"
    echo " "
    echo "	>b vmunix.$$ -s  ( boot saved kernel to single user mode )"
    echo "	# cp /vmunix.$$ /vmunix         (restore original vmunix )"
    echo "	# mv /etc/sunlink/dni/rc        /etc/sunlink/dni/rc-old "
    echo "	# ^D                             ( go multi-user          )"
    echo " "
    echo "	You can now find out what went wrong with the installation "
    echo " "
    echo "********************************************************************"
    echo " "	
    echo "	$0 will now exit.  Please follow the instructions for"
    echo "	halting and rebooting your system as described above."
    echo " "
else
    echo ""
    echo "	$0 will now exit.  You should reboot to verify that DNI"
    echo "	starts successfully at boot time."
    echo ""
endif
exit (0)
