#!/bin/csh
#
# @(#)dni_rc_ins.sh 7.11 91/12/23 SMI
#
#   Copyright (c) 1988, 1989, 1990 by Sun Microsystems, Inc.
#
#   dni_rc_ins:  Cshell Script to complete dni installation.  This script
#	must be run as root.  Files modified may include:
#
#	/etc/rc.local
#	/etc/passwd
#	/etc/sendmail.cf
#
#   In all cases the original file (xxx) will be preserved (xxx.nodni).
#
#   The operator is prompted for Ethernet controller and DECnet area/node #
#  which will be validated.
#
#   An optional -d xxx argument will override the /etc directory.  This is
#  mainly used for testing purposes.
#
#
set ETC_DEF = /etc
unalias rm
unalias cp
unalias mv
unset noclobber
set ETC = $ETC_DEF
set oname = $0
#
#   Verify if directory override arguments
#
if ($#argv >= 2) then
    switch ($1)
	case -d:
	case -D:
	    set ETC = $2
	    breaksw
	default:
	    echo "Extra Arguments ignored"
    endsw
endif
if ((!(-d $ETC)) || (!(-w $ETC))) then
   echo "$ETC either not a directory or not writable"
   exit (-1)
else if ($ETC != $ETC_DEF) then
    echo "Test directory set to $ETC"
    echo " "
endif
#
setenv PATH /usr/ucb:/bin:/usr/bin:/etc:/usr/etc
#
set DEF_DNI_PATH = /usr/sunlink/dni
set DEF_NCP_PATH = /etc/sunlink/dni
set DEF_DNII_PATH = $DEF_DNI_PATH/install
set OD = /dev/console
set INSSHELL = dni_insert_file
set MLSHELL = dni_mail_ins
set AGSHELL = install_dni_agent
set SUBSHELLS = "$INSSHELL $MLSHELL $AGSHELL"
set CLSHELL = dni_deinstall
set OEXT = "nodni"
set DEXT = "delta"
set startm = "# 7.0 DNI Start"
set endm = "# 7.0 DNI End"

#
#			Verify that owner is root
#
set me = `whoami`
if ($me != "root") then
   echo "This script must be run as root"
   exit (-1)
endif

#
#	Instead of the old roadrunner flag, we change it to "loadable"
# .. not currently set, but may be since loadable drivers are supported
#  under Sun O/S 4.1
#
#			Set loadable flag if applicable
@ loadable = 0

#
#  Find path to subshells .. better be in dni/install
#
if (!(-d $DEF_DNI_PATH)) then
    echo "Missing Default DNI Directory $DEF_DNI_PATH"
    exit (-1)
endif
foreach i ($SUBSHELLS)
    if (!(-x $DEF_DNII_PATH/$i)) then
	echo "Missing Subshell file $i in directory $DEF_DNII_PATH"
	exit (-1)
    endif
end

set INSERT_FILE = $DEF_DNII_PATH/$INSSHELL
#
set ecn = (le ie)
set floc = "$ETC/rc.local"
set rcloc = "$ETC/sunlink/dni/rc"
set fpass = $ETC/passwd
set ftotal = "$floc"
set fall = "$ftotal $fpass"
#
set dps = "dniserver"
set dpn = "dnincp"
set env_str = "DNI_NCP_DIR"
set extest = "dniserver dnaserver"
#			Avoid nasty aliases
unalias rm
unalias cat
set ARCH = `/bin/arch`
echo "	$ARCH installation is proceeding"
echo " "
#
echo "	You have invoked the $oname:t script.  It is designed"
echo "	to install the DNI software on this node."
echo " "
echo "	You will now be asked a series of questions.  After all" 
echo "	input has been gathered, you will be given a chance to"
echo "	decide whether to continue with the installation."
echo " "
#			Verify key files writable
foreach i ($fall)
   if (!(-w $i)) then
	echo "File $i does not exist or cannot be written"
	exit (-2)
   endif
end
#			Get and verify key config parameters
#
echo "	You are going to have to specify your Ethernet Controller type"
echo "	and unit here.  The following output shows the available Ethernet"
echo "	interfaces."
echo "  "
netstat -ria | egrep '(le|ie)' | awk '{print $1}'
# need to use a different method to get ethernet controller here, as
# we have moved vmunix in dni_install. lets use dmesg !!
echo "  "
#dmesg | grep at | egrep '(le|ie)' | awk '{print $1}' | sort | uniq
ecloop:
echo -n "Please indicate Ethernet controller as $ecn[1] (1) or $ecn[2] (2) [1/2]: "
set ans = ($<)
if (( $ans != 1) && ($ans != 2)) then
   echo "You have typed in an illegal value.  Please enter 1 or 2"
   goto ecloop
endif
if ($ans == 1) then
   set ethtype = "le"
   @ ethnum = 1
else
   set ethtype = "ie"
   @ ethnum = 2
endif
#
uloop:
echo -n "Please enter Ethernet controller unit for DECnet as 0 or 1 [0/1]: "
set ans = ($<)
if (( $ans != 0) && ($ans != 1)) then
   echo "You have typed in an illegal value.  Please enter 0 or 1"
   goto uloop
endif
@ ethunit = $ans
echo " "
#
#  Now we need numbers in a range .. a slightly more tricky proposition
#
aloop:
echo -n "Please identify the DECnet area number for this node [1 - 63]:  "
set ans = ($<)
set stans = `echo $ans | grep -c -v "[^0-9]"`
if ((!($stans)) || ($ans < 1) || ($ans > 63)) then
   echo "You have typed in an illegal area number.  Please reenter"
   goto aloop
endif
@ area = $ans
#
nloop:
echo -n "Please identify the DECnet node number for this node [1 - 1023]:  "
set ans = ($<)
set stans = `echo $ans | grep -c -v "[^0-9]"`
if ((!($stans)) || ($ans < 1) || ($ans > 1023)) then
   echo "You have typed in an illegal node number.  Please reenter"
   goto nloop
endif
@ node = $ans
echo " "
#
#	Need to get the DECnet node name  (usually first 6 chars of sun
#  node name, but maybe not).
#
#
naloop:
echo -n "Please identify the DECnet node name for this node (<= 6 chars): "
set nname = ($<)
@ nl = `echo $nname | wc -c`
if (($nl < 2) || ($nl > 7)) then
    echo "You have typed in an illegal DECnet node name of length $nl"
    goto naloop
endif
#
#
#		Recap Information
#
echo " "
echo "	At this point the data gathering is completed.  Please review"
echo "	the following information before deciding whether to proceed."
echo " "
echo "		Ethernet controller code and unit = $ethtype$ethunit"
echo "		DECnet Area.Node numbers = $area.$node"
echo "		DECnet Node Name = $nname"
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 "			Good Luck"
   exit (1)
endif
#
echo " "
#
#   Set up rc.local
#
# test for /dni in rc.local, if there exit
# should catch 6.0 and this new 7.0 stuff
#
grep -s "/dni" $floc
    if (!($status)) then
	echo " "
	echo "	It appears that old DNI commands are already in $floc"
	echo "	Please run deinstall before retrying this script"
	exit (1)
    endif
#
# else go the same way as all other sunlink products
# and use a seperate rc file
#
set tfile = "/tmp/$floc:t.$DEXT"
rm -f $tfile
echo "#" > $tfile
echo "# DNI Start" >> $tfile
echo "#" >> $tfile
echo "if [ -f /etc/sunlink/dni/rc ]; then" >> $tfile
echo "        /etc/sunlink/dni/rc"  >> $tfile
echo "fi" >> $tfile
echo "#" >> $tfile
echo "# DNI End" >> $tfile
echo "#" >> $tfile
#
#
   echo "	The following line(s) will be added to $floc .."
   echo " "
   cat $tfile
   echo " "
#
#   Check for "exit 0" in /etc/rc.local.  If not there, append new stuff
#
   grep -s "exit 0" $floc
   if ($status) then
	cat $floc $tfile > /tmp/$floc:t
	if ($status) then
	   echo "'cat' error in creating merged /tmp/$floc:t File"
	   exit (-1)
	endif
   else
	$INSERT_FILE $floc $tfile "exit 0" B /tmp/$floc:t
	set rst = $status
	if ($rst < 0) then
           echo "Error $rst in merge attempt in /tmp/$floc:t"
           rm -f $tfile
           exit (-1)
	endif
   endif
endif
rm -f $tfile
set pdpn = $DEF_DNI_PATH/$dpn
set pdps = $DEF_DNI_PATH/$dps
#
# this stuff now goes in /etc/sunlink/dni/rc
#

@ prev = 0
    if ( -f $rcloc ) then
	echo " "
	echo "	$rcloc already exists. 	Please run deinstall before "
	echo "	retrying this script"
	exit (1)
    endif
set tfile = "/tmp/$rcloc:t.$DEXT"
rm -f $tfile
if (!($prev)) then
   echo "$startm" >> $tfile
   echo "#" >> $tfile
   echo "(echo -n 'starting dni') > $OD" >> $tfile
   echo "#" >> $tfile
   echo "$pdpn set line $ethtype$ethunit protocol ethernet   > $OD" >> $tfile
   echo "$pdpn set line $ethtype$ethunit state on            > $OD" >> $tfile
   echo "$pdpn set circuit $ethtype$ethunit state on         > $OD" >> $tfile
   echo "$pdpn set exec address $area.$node           > $OD" >> $tfile
   echo "$pdpn set exec name $nname            > $OD" >> $tfile
   echo "$pdpn set exec state on               > $OD" >> $tfile
   echo "$pdpn set known nodes all             > $OD" >> $tfile
   echo "$pdps &                            " >> $tfile
   echo "#" >> $tfile
   echo "(echo '.') > $OD" >> $tfile
   echo "#" >> $tfile
   echo "$endm" >> $tfile
#
   echo "	Copying the DNI rc script from $tfile to $rcloc"
   echo " "
cp $tfile $rcloc
if ( $status ) then
	echo "Error: copying rc script,please do this manually"
	echo "before rebooting"
	echo " "
endif
chmod 500 $rcloc
rm -f $tfile

#
#  All that's left is to add line to password file
#	tfile: new file, dfile:  delta to change,  sfile: stripped of dni entry
#		 nfile:  all
#	dnil:  dni line for the passwd file
#	plmatch:  0 if match for $bmatch, else 1
#
set bmatch = "+:"
set tfile = "/tmp/$fpass:t"
set dfile = $tfile.$DEXT
set sfile = $dfile.in
set nfile = "$tfile $dfile $sfile"
foreach i ($nfile)
    if (-e $i) then
	rm -f $i
	if ($status) then
	    echo "Cannot delete $i"
	    exit (-1)
	endif
   endif
end
#
#   Strip out any dni line in password file
#
set amatch = "^dni:"
grep -v $amatch $fpass >$sfile
@ plmatch = $status
if (($plmatch <0) || ($plmatch >1)) then
    echo "Cannot grep $fpass"
    exit (-1)
endif
#
set dnil = "dni:*:376:376:Default DECnet account:/tmp:/bin/true"
echo "$dnil" > $dfile
grep  -s '^+:' $sfile
@ plmatch = $status
if ($plmatch >1) then
    echo "grep for ^$bmatch failed in $sfile"
    exit (-1)
endif
echo "	The following line(s) will be added to $fpass .."
echo " "
echo "$dnil"
echo " "
grep -s $amatch $fpass
if (!($status)) then
    echo "	replacing...."
    grep $amatch $fpass
    echo " "
endif
#
#  If $plmatch is 1, concatenate the new password line,
#  else you have to insert it before +:
#
if (!($plmatch)) then
    $INSERT_FILE $sfile $dfile $bmatch B $tfile
    set rst = $status
    if ($rst < 0) then
	echo "Error $rst in merge attempt in $fpass"
	rm -f $nfile
	exit (-1)
    endif
else
    mv $sfile $tfile
    if ($status) then
	echo "Could not mv $sfile to $tfile"
	exit (-1)
    endif
    cat $dfile >> $tfile
    if ($status) then
	echo "Could not cat $dfile to $tfile"
	exit (-1)
    endif
endif
rm -f $sfile $dfile

#
#   Hookay ... all files set .. ask user for final okay
#
echo " "
echo "    At this point a temporary version of the $floc file has been"
echo " created containing additional DNI installation lines.  A temporary"
echo " version of $fpass has also been created to define a new dni"
echo " user entry. If you wish you can examine these new files; they are"
echo " located in /tmp."
echo " "
echo "    If you decide to continue with this script the following actions"
echo " will occur:"
echo " "
echo "	1.  The $floc file will be backed up as $floc.$OEXT"
echo " "
echo "	2. It will then be replaced by its newly created /tmp equivalent"
echo "	which contains the additional DNI command lines as listed above." 
echo " "
echo "	3. The $fpass file will be treated in a manner similar to"
echo "	the $floc file.  ** Any previous dni user entry will be"
echo "	destroyed. **"
echo " "
echo "	4. All temporary files will be deleted."
echo " "
echo "	5. The VMS Mail gateway installation script may be invoked."
echo " "
echo "	6. The DNI NetManager Agent installation script may be invoked."
echo " "
echo " "
echo "    On the other hand, you may wish to stop the script at this point"
echo " and examine the new /tmp $DEXT files more closely."
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 "        Run dni_deinstall and then dni_install."
   echo "			Good Luck"
   exit (1)
endif
#
#  Delete old Backup Files if any
#
foreach idx ($fall)
   if (-e $idx.$OEXT) then
	rm -f $idx.$OEXT
	if ($status) then
	    echo "Cannot delete old $idx.$OEXT File.  No changes occured."
	    exit (-1)
	endif
    endif
end
#
#   Write copies of existing files
#
foreach idx ($fall)
    cp $idx $idx.$OEXT
    if ($status) then
	echo "Cannot copy $idx to $idx.$OEXT backup.  No changes occured."
	exit (-1)
    endif
end
#
#  The biiiig one ... copy new files over old equivalents
#
echo " "
foreach idx ($fall)
    set tfile = "/tmp/$idx:t"
    cp $tfile $idx
    if ($status) then
	echo "Cannot copy $tfile to $idx.  THIS IS BAD NEWS."
	exit (-1)
    else
	echo "	$idx file updated with DNI installation information."
    endif
end
#
#  Remove temporary files
#
echo " "
foreach idx ($fall)
    set dfile = "/tmp/$idx:t.$DEXT"
    set tfile = "/tmp/$idx:t"
    rm -f $tfile $dfile
end
echo "All $ETC files updated successfully with DNI installation info."
echo " "

#
#   Now for final NCP database initialization - just run dninmars
#
set NCP_INIT = dninmars
@ ncp_bad = 0
echo " "
echo -n "Do you wish to run $NCP_INIT to create the NCP Database [Y/N] : "
set ans = ($<)
if (($ans == "y") || ($ans == "yes") || ($ans == "Y") || ($ans == "YES")) then
    $DEF_DNI_PATH/$NCP_INIT
    echo " "
    if ($status) then
	echo "Problems creating initial NCP Database files in $DEF_NCP_PATH"
	    @ ncp_bad++
    else
	echo "	Successfully created NCP Database"
    endif
    echo " "
else
    echo "	NCP Database not initialized by this script"
endif
echo " "

cp $DEF_DNI_PATH/dniserver.reg $DEF_NCP_PATH/dniserver.reg

#
#    Now optionally fire off VMS Mail Gateway and DNI Agent installation
# scripts.
#
echo " "
echo "	Successful completion of this phase."
echo "	To continue the DNI installation you may  'cd' to"
echo " "
echo "	$DEF_DNII_PATH"
echo " "
echo "	and invoke the $MLSHELL Cshell script to install"
echo "	the DNI 7.0 VMS Mail Gateway software.  Installation of"
echo "	the Mail Gateway software is not mandatory for the successful"
echo "	operation of DNI on this node."
echo " "
echo "	You can install the Mail Gateway automatically by saying yes to the"
echo "	next question.   Otherwise refer to the DNI System Administrator's "
echo "	Guide for further instructions."
echo " "
#
mans:
echo -n "Do you wish to automatically invoke the $MLSHELL script [Y/N]:  "
set ans = ($<)
if (($ans == "n")||($ans == "no")||($ans == "N")||($ans == "NO")) then
   echo " "
   echo "	You have chosen to bypass the VMS Mail Gateway Installation."
else if (($ans == "y")||($ans == "yes")||($ans == "Y")||($ans == "YES")) then
    set ord = `pwd`
    cd $DEF_DNII_PATH
    $DEF_DNII_PATH/$MLSHELL
    if ($status) then
	echo " "
	echo "Problems in DNI 7.0 VMS Mail Gateway Installation on this node."
	echo " "
    else
	echo " "
	echo "	Successful DNI 7.0 VMS Mail Gateway Installation on this node."
    endif
    cd $ord
else
    goto mans
endif
#
echo " "
echo "	To complete the DNI installation you may  'cd' to"
echo " "
echo "	$DEF_DNII_PATH"
echo " "
echo "	and invoke the $AGSHELL Cshell script to install"
echo "	the DNI 7.0 Agent for the SunNet Manager.  Installation of"
echo "	the DNI Agent software is not mandatory for the successful"
echo "	operation of DNI on this node."
echo " "
echo "	You can install the SunNet DNI Agent automatically by saying yes"
echo "	to the next question.   Otherwise refer to the DNI System"
echo "	Administrator's Guide for further instructions."
echo " "
#
aans:
echo -n "Do you wish to automatically invoke the $AGSHELL script [Y/N]:  "
set ans = ($<)
if (($ans == "n")||($ans == "no")||($ans == "N")||($ans == "NO")) then
   echo " "
   echo "	You have chosen to bypass the DNI Agent Installation."
else if (($ans == "y")||($ans == "yes")||($ans == "Y")||($ans == "YES")) then
    set ord = `pwd`
    cd $DEF_DNII_PATH
    $DEF_DNII_PATH/$AGSHELL
    if ($status) then
	echo " "
	echo "Problems in DNI 7.0 SunNet Agent Installation on this node."
	echo " "
    else
	echo " "
	echo "	Successful DNI 7.0 SunNet Agent Installation on this node."
    endif
    cd $ord
else
    goto aans
endif
echo " "
exit (0)
