#!/bin/csh -f
#
# @(#)@(#)tr_install.sh	1.26 10/7/93 Copyright SMI 1991
#
#   Copyright (c) 1988 by Sun Microsystems, Inc.
#
#
#  tr_install:  Cshell Script to Configure the Kernel for SunNet TRI/S 
#		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 PRODUCT="TRIS"
set product="tris"
set PRODUCT_NAME="TRI/S"

set ARCH  = `arch`
set ARCHK = `arch -k`

set INS = "install"
set SYS = "sys"
set CNF = "$SYS/$ARCHK/conf"
set SYS_INS = "sys_install"
set SYSN = "System Config File"

# XXX
# XXX hetero server installations may be screwed.  Fix this.
# XXX
set USR_PATH	= "/export/exec/$ARCH"
set SYS_PATH	= "/export/exec/kvm/$ARCHK/$SYS"

set ULIB	= "$USR_PATH/lib"
set DIAG_PATH	= "$USR_PATH/diag/sundiag"

set PRODUCT_PATH = "$USR_PATH/sunlink/tr"
set PRODSYS_PATH = "$PRODUCT_PATH/$SYS"
set PRODINS_PATH = "$PRODUCT_PATH/$INS"
set PRODCNF_PATH = "$PRODUCT_PATH/$CNF"
set PRODDIAG_PATH = "$PRODUCT_PATH/diag"

set CONF = "conf"
set FILE = "files"
set GEN_ADD = "$PRODUCT.gen.add"

set roadrun=0

cd /
echo " "
echo "	$ARCHK 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 $PRODUCT_PATH)) then
    echo "No root write access to $PRODUCT_PATH .. fatal error"
    exit (-1)
endif
#
#		Verify operator's desire to run this script
#
echo "	You have invoked the $0 script. "
echo "	This script is designed to continue the $PRODUCT_NAME installation "
echo "	on this network interface by: "
echo " "
#
echo "		Creating/confirming the $SYS_PATH Link"
echo " "
echo "		Creating a $SYSN based on a source Config File"
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 "	After this script is finished, you must follow the procedures "
echo "	for modifying the hosts and rc.boot files in the SunNet $PRODUCT_NAME "
echo "	Controller Administration Guide. "
echo " "
echo "	FOLLOW THE STEPS IN THE MANUAL *PRIOR* TO REBOOTING! "
echo " "
echo "	Then you must halt the system and reboot using the new kernel."
echo " "
echo -n "Do you wish to continue with this script [y/n]:  "
set ans = ($<)
echo " "
if (($ans == "n") || ($ans == "no") || ($ans == "N") || ($ans == "NO")) then
   echo " "
   echo "	Exiting $0 Token Ring Installation script. "
   echo " "
   exit (0)
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
#
#       Move sun4m_35 to sun4m on machines with
#       a Tsunami or Swift processor.
#
set proctype=`/usr/etc/devinfo | egrep '^.Node'`
if ( `echo $proctype | egrep 'TI,TMS390S10|FMI,MB86904' | wc -l` ) then
	set SPECIAL=_35
else    set SPECIAL=
endif

setenv TGTSYS  $ARCHK$SPECIAL

if ( $TGTSYS == sun4m_35 ) then
	cd $PRODSYS_PATH
	if ( -d sun4m_35 ) then
		if ( -d sun4m ) then
			mv sun4m sun4m-
		endif
		mv sun4m_35 sun4m
	endif
endif
#
#				0. Compile prtsrtab in usr.etc
#
set d=`pwd`
cd $PRODUCT_PATH/usr.etc
# cc -o prtsrtab prtsrtab.c
cd $d

#
#
#				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 "	Link to $SYS_PATH confirmed at /$SYS ..."
    endif
    echo " "
#
#				2A. Get System Name
#
    set CONF_PATH = /$SYS/$ARCHK/$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 Token Ring 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
#				2A. Obtain source configuration file
sloop:
    echo "	Please enter an existing Config file (ex: GENERIC)"
    echo "	This script uses GENERIC by default."
    echo ""
    echo -n "Source $SYSN : "
    set ans = ($<)
    if ($ans == "") then
	set ans = "GENERIC"
    endif
    if (!(-e $ans)) then
	echo "Error:  No such $SYSN as $ans found in $CONF_PATH directory"
	goto sloop
    endif
    echo " "
    set SRC_NAME = $ans

#				2B. Obtain new configuration file name
dloop:
    echo "	Please enter new system configuration file name (ex: TOKEN)"
    echo "	This script uses TOKEN by default."
    echo ""
    echo -n "New $SYSN : "
    set ans = ($<)
    if ($ans == "") then
	set ans = "TOKEN"
    endif
    set SYS_NAME=$ans
    if (-e $SYS_NAME || $SYS_NAME == $SRC_NAME) then
	echo ""
	echo -n "File already exists, OK to overwrite [y/n]:  "
	set ans = ($<)
	echo " "
	if (($ans != "y") && ($ans != "yes") && ($ans != "Y") && ($ans != "YES")) then
	goto dloop
	endif
    endif
    echo " "
    if ($SRC_NAME != $SYS_NAME) then
        cp $SRC_NAME $SYS_NAME
    endif

#				3. Change Protections, Copy /usr/include files
    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 ...."
echo " "
#

#
# Copy /usr/include files and sundiag files
#
cd $PRODUCT_PATH/usr
echo "copying include files ..."
tar cf - include | (cd $USR_PATH; tar xvfp -)
echo ""

#OBSOLETE cd $PRODDIAG_PATH
#OBSOLETE # echo "copying sundiag files ..."
#OBSOLETE if (!(-e $DIAG_PATH)) then
#OBSOLETE 	mkdir -p $DIAG_PATH
#OBSOLETE endif
#OBSOLETE # tar cf - . | (cd $DIAG_PATH; tar xvfp -)
#OBSOLETE # \ln -s $DIAG_PATH/tr.usertest $DIAG_PATH/.usertest >& /dev/null
#OBSOLETE echo ""
#
#				4. Run sys_install
#
if (!(-d $PRODINS_PATH)) then
    echo "Error .. $PRODINS_PATH does not exist"
    exit (-5)
endif
if (!(-x $PRODINS_PATH/$SYS_INS)) then
    echo "Error .. can't find $PRODINS_PATH/$SYS_INS"
    exit (-6)
endif
cd $PRODINS_PATH
echo "Running $SYS_INS script ...."
$PRODINS_PATH/$SYS_INS
set sst = $status
if ($sst) then
   echo "Error code $sst during execution of $PRODINS_PATH/$SYS_INS"
   exit (-6)
endif
echo "$SYS_INS script run successfully ...."
echo " "
#
#				5. Modify the System Config File
#
if (!($roadrun)) then
    cd $CONF_PATH
    if (!(-e $PRODCNF_PATH/$GEN_ADD)) then
	echo "Error ... $PRODCNF_PATH/$GEN_ADD does not exist"
	exit (-7)
    endif
    set tr_added=`grep "$PRODUCT_NAME" $SYS_NAME`
    if (! ${#tr_added}) then
	echo "	adding $GEN_ADD"
        cat $PRODCNF_PATH/$GEN_ADD >> $SYS_NAME
        set sst = $status
        if ($sst) then
	    echo "Error code $sst during append of $PRODCNF_PATH/$GEN_ADD to $SYSN"
	    exit (-8)
        endif
        echo "	System Config file $SYS_NAME successfully modified ..."
        echo " "
    endif
#				6. Configure and build kernel
    cd $CONF_PATH
    set vmu = `egrep "^config" $SYS_NAME|awk '{print $2}'`
    set excmd = "/etc/config $SYS_NAME"
    echo -n "	"
    $excmd
    set sst = $status
    if ($sst) then
	echo "Error code $sst during execution of ++$excmd++"
	exit (-6)
    endif
    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 short coffee break 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
if (!(-e $vmu)) then
    echo "Error .. $vmu does not exist in $CONF_PATH"
    exit (-9)
endif
# On a SS-1 need to check for enough space in sd0a partition
# This is due to the VERY LIMITED root partition on the SS-1.
#
set DISK_AVAIL=`df / | awk 'NR == 2 {print $4 }'`
set ROOT_SPACE=1400
set answer = ""
if ( $DISK_AVAIL < $ROOT_SPACE ) then
    echo " "
    echo "	*********************************************************"
    echo " "
    echo "	Insufficient space available in root partition"
    echo "	to copy over the kernel. You may want to remove some"
    echo "	extra files you have in the root partition. Additionally,"
    echo "	you can link /var/tmp to another partition to create"
    echo "	enough free space. You need approximately 1.5MBytes free"
    echo "	space. Use the df command to check for available space."
    echo "	When there is enough space copy over the new vmunix."
    echo "	An example path is shown below: "
    set KERNPATH = "$CONF_PATH/../$SYS_NAME"
    echo "		cp $KERNPATH/$vmu /new$vmu"
    echo " "
    echo "	*********************************************************"
    echo " "
    echo -n "Do you want to see the final instructions? {y|n] :  "
    set ans = ($<)
    echo " "
    if (($ans != "y") && ($ans != "yes") && ($ans != "Y") && ($ans != "YES")) then
        echo "	REMEMBER TO FOLLOW THE STEPS IN THE MANUAL *PRIOR* TO REBOOTING! "
        exit (-8)
    endif
else
    set excmd = "cp $vmu /new$vmu"
    $excmd
    set sst = $status
    if ($sst) then
        echo "Error code $sst during execution of ++$excmd++"
        exit (-8)
    endif
    echo "	++$excmd++  run successfully ...."
    echo " "
endif
#
if (!($roadrun)) then
    echo " "
    echo "	********************************************************************"
    echo " "
    echo "	$PRODUCT_NAME has been installed correctly.   "
    echo "	After this script is finished, you must follow the procedures "
    echo "	for modifying the hosts and rc.boot files in the $PRODUCT_NAME "
    echo "	Controller Administration Guide. "
    echo " "
    echo "	FOLLOW THE STEPS IN THE MANUAL *PRIOR* TO REBOOTING! "
    echo " "
    echo "	Then you must halt the system and reboot using the new kernel."
    echo " "
    echo "		1. /etc/halt 		(Halts the machine) " 
    echo "		2. b new$vmu -s		(Boots new kernel image) "
    echo " "
    echo "	If the new kernel boots, the installation of $PRODUCT_NAME is successful."
    echo " "
    echo "	The final step is to replace the old kernel image with "
    echo "	the one we have just created.  To do this, type the following "
    echo "	series of commands after your test boot completes successfully:"
    echo " "
    echo "		1. cd / "
    echo "		2. mv vmunix old$vmu "
    echo "		3. mv new$vmu vmunix "
    echo "		4. ^D (depress 'Control' and 'D' keys simultaneously) "
    echo " "
    echo "	At that point, the $PRODUCT_NAME installation procedure will be "
    echo "	complete and the system will be ready for user logins. "
    echo " "
    echo "	If the new kernel doesn't boot, reboot the original kernel by "
    echo "	typing /etc/reboot.  See the $PRODUCT_NAME Controller "
    echo "	Administration Guide for more information."
    echo " "
    echo "	******************************************************************** "
    echo " "	
    echo "	$0 will now exit." 
    echo "	Please follow the instructions in the manual for modifying the " 
    echo "	hosts and rc.boot files, and then halt and reboot your " 
    echo "	system as described above. "
    echo " "
else
    echo ""
    echo "	$0 will now exit."
    echo "	You should reboot to verify that Token Ring starts successfully "
    echo "	at boot time. "
    echo " "
endif
exit (0)
