#!/bin/csh
#
# @(#)dni_deinstall.sh 7.6 91/02/11 SMI
#
# Copyright (c) 1988 by Sun Microsystems, Inc.
#
# dni_install: script to deinstall the DNI software
#
unalias rm
unalias cat
unset noclobber
unalias mv
setenv PATH /usr/ucb:/bin:/usr/bin:/etc:/usr/etc
set SYS_PATH = "/usr/share/sys"
set SYSN = "System Config File"
set DNI = "/usr/sunlink/dni"

set SunX = `arch -k`

cd /
echo ""
echo "$SunX deinstallation is proceeding"
echo ""
# Verify that owner is root
set me = `whoami`
if ($me != "root") then
   echo "This script must be run as root"
   exit (-1)
endif
#
#
echo "Removing links to libnfars.a and libnftl.a from /usr/lib" 
echo ""
rm -f /usr/lib/libnfars.a
rm -f /usr/lib/libnftl.a
#
echo "Removing DNI /dev entries, ignore any 'No match.' error messages"
echo ""
cd /dev
rm -f /dev/dni*
rm -f /dev/dna*
#
echo ""
echo "Removing DNI configuration lines from /etc/rc files"
echo ""
#
# save old versions of /etc/{rc, rc.boot, rc.local, sendmail.cf}
# remove DNI entries from /etc/{rc, rc.boot, rc.local}
#
#
cd /etc
set elist = "rc rc.boot rc.local sendmail.cf"
foreach i ($elist)
    cp $i $i.save
    if ($status) then
	echo "cp /etc/$i /etc/$i.save failed"
	exit (-2)
    endif
    /bin/awk '/DNI Start/,/DNI End/ {next} {print}' $i > /tmp/$i.new
    cp /tmp/$i.new $i
end
rm -f /etc/sunlink/dni/rc 
echo ""
#
#   Try to restore /etc/sendmail.cf
#
set OLD = "nodni"
set SAVE = "save"
set ULIB = "/usr/lib"
set INSD = "/usr/sunlink/dni/install"
set MAIN = "sendmail.main.cf"
set SUBSID = "sendmail.subsidiary.cf"
set NTEMPL = ("$INSD/$MAIN".dni "$INSD/$SUBSID.dni")
set MAILF = "/etc/sendmail.cf"
@ md = 0
if (-r $MAILF.$OLD) then
    echo "VMS Mail gateway had been installed"
    foreach  i ($NTEMPL)
	if (-r $i) then
	    /bin/diff $i $MAILF >& /dev/null
	    if (!($status)) then
		set ans = "Y"
		goto qb
	    endif
	endif
    end
    if (!($md)) then
	echo "    Either changes have been made to $MAILF since DNI"
	echo "  installation or the original DNI installation of the VMS Mail"
	echo "  Gateway was not done automatically through the installation scripts."
	echo " "
	echo "   A $MAILF.$OLD file exists which contains the state of"
	echo "  $MAILF when DNI was installed."
qa:
	echo " "
	echo -n "   Do you wish to restore $MAILF to its pre-DNI contents [Y/N]:  "
	set ans = ($<)
qb:
	echo " "
	if (($ans == "y")||($ans == "yes")||($ans == "Y")||($ans == "YES")) then
	    echo "The current $MAILF file will be saved in $MAILF.$SAVE"
	    /bin/mv -f $MAILF $MAILF.$SAVE
	    if ($status) then
		echo "Problems saving current $MAILF.  No changes."
	    else
		/bin/mv -f $MAILF.$OLD $MAILF
	 	if ($status) then
		    echo "Problems restoring $MAILF.  No changes."
		    /bin/mv -f $MAILF.$SAVE $MAILF
		else
		    echo "The $MAILF file has been automatically restored."
		    echo "You may want to restart sendmail after this script completes."
		endif
	    endif
	else if (($ans != "n")&&($ans != "no")&&($ans != "N")&&($ans != "NO")) then
	   echo "You have typed in an illegal choice.  Please enter Y or N"
	   goto qa
	endif
    endif
else
    echo " "
    echo "	DNI VMS Mail Gateway appears not to have been installed ..."
    echo " "
endif


#
# Remove kernel config info from kernel directories
# (this will fail on client, which is normal exit on client)
#
set CONF_PATH = $SYS_PATH/$SunX/conf
echo "Removing DNI entries from $CONF_PATH/files"
echo ""
if (!(-d $CONF_PATH)) then
    echo "No $CONF_PATH directory"
    exit (-5)
endif
if (!(-w $CONF_PATH)) then
    echo "$CONF_PATH directory not writable"
    echo "If this is being run on a client, this is the normal exit"
    exit (0)
endif
cd $CONF_PATH
if (!(-e files)) then
    echo "$CONF_PATH/files not found"
    exit (-6)
endif
#
chmod u+w files
if ($status) then
    echo "Change protection error for $CONF_PATH/files"
    exit (-7)
endif
#
# remove DNI entries from files file
#
cd $CONF_PATH
cp files files.save
sed -f $DNI/deinstall/files.clean files > /tmp/files.new
set sst = $status
if ($sst) then
    echo "sed script $DNI/deinstall/files.clean failed"
    exit (-8)
endif
cp /tmp/files.new files
#
# remove DNI objects from OBJ directory
#
set CONF_PATH = $SYS_PATH/$SunX/conf
echo "Removing DNI objects from $SYS_PATH/$SunX/OBJ"
echo ""
cd ../OBJ
rm -rf cxalarm.o cxbuf.o cxmbox.o cxproc.o cxsched.o cxunix.o decnet.o
rm -rf dna.o mirror.o netman.o nsp_acks.o nsp_chld.o nsp_init.o nsp_main.o
rm -rf nsp_misc.o nsp_ques.o nsp_recv.o nsp_xmit.o rou_init.o rou_main.o
rm -rf rou_nsp.o rou_unix.o rou_xmit.o ses_bufr.o ses_chld.o ses_clos.o
rm -rf ses_conc.o ses_main.o ses_node.o ses_nsp.o ses_open.o ses_read.o
rm -rf ses_serv.o ses_user.o ses_xmit.o
#
echo -n "Do you want to rebuild your old DNI kernel without DNI [Y/N]: "
set ans = ($<)
if (($ans != "y") && ($ans != "yes") && ($ans != "Y") && ($ans != "YES")) then
    exit(0)
endif
#
cd $CONF_PATH
sloop:
echo "Please select a preexisting DNI $SYSN (ex: GENERIC, DNI, ...)"
echo -n "$SYSN : "
set ans = ($<)
if (!(-e $ans)) then
    echo "No such $SYSN as $ans found in $CONF_PATH directory"
    goto sloop
endif
set SYS_NAME = $ans
#
echo ""
echo "Removing DNI configuration lines from $SYS_NAME"
echo ""
cp $SYS_NAME $SYS_NAME.save
chmod u+w $SYS_NAME
sed -f $DNI/deinstall/kernel.clean $SYS_NAME > /tmp/$SYS_NAME.new
set sst = $status
if ($sst) then
    echo "sed script $DNI/deinstall/kernel.clean failed"
    exit (-9)
endif
cp /tmp/$SYS_NAME.new $SYS_NAME
#
# Reconfigure and build kernel
#
echo "Configuring $SYS_NAME kernel"
echo ""
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 (-10)
endif
if (!(-d ../$SYS_NAME)) then
    echo "no such directory as `pwd`/../$SYS_NAME"
    exit (-11)
endif
#
cd ../$SYS_NAME
echo ""
echo "Rebuilding $SYS_NAME kernel"
make
set sst = $status
if ($sst) then
    echo "Error code $sst during execution of ++make++"
    exit (-12)
endif
echo ""
echo "$SYS_NAME kernel build is complete"
echo ""
#
# Set up the new kernel for reboot
#
set vmu = "vmunix"       
if (!(-e $vmu)) then
    echo "$vmu does not exist in $CONF_PATH"
    exit (-13)
endif
set excmd = "cp $vmu /new$vmu"
$excmd
set sst = $status
if ($sst) then
    echo "Error code $sst during execution of ++$excmd++"
    exit (-14)
endif
#
# Now give instructions for reboot
#
echo ""
echo "Apparently successful completion of this phase of DNI deinstallation."
echo "To reboot your new kernel that doesn't contain DNI, type the following:"
echo ""
echo "	1.  /etc/halt		(Halts the machine)" 
echo "	2.  b newvmunix -s	(Boots new kernel image)"
echo ""
echo "If the new kernel doesn't boot, reboot the original kernel by typing"
echo "/etc/reboot.  Otherwise the DNI deinstall has completed successfully."
echo ""
echo "The final step is to replace the old kernel image with"
echo "the one we have just created.  The simplest way to do this"
echo "is to type the following series of commands manually after"
echo "the boot completes successfully:"
echo ""
echo "	1.   cd /"
echo "	2.   mv vmunix oldvmunix"
echo "	3.   mv newvmunix vmunix"
echo "	4.   ^D  (depress 'Control' and 'D' keys down simultaneously)"
echo ""
exit (0)
