#!/bin/csh
#
# @(#)dni_mail_ins.sh 7.5 91/02/11 SMI
#
#   Copyright (c) 1989 by Sun Microsystems, Inc.
#
#   dni_mail_ins:  Cshell Script to attempt to automate DNI VMS Mail Gateway
# installation.  This script must be run as root.  Files modified may include:
#
#	/etc/sendmail.cf
#	/etc/sunlink/dni/dniserver.reg
#
#  and the existence of /etc/sunlink/dni/dnihosts will be verified.
#
#  sendmail.cf will be automatically modified ONLY if it EXACTLY matches either
#
#	1. /usr/lib/sendmail.main.cf
#
#	2. /usr/lib/sendmail.subsidiary.cf
#
#   In this case, the user has not modified the distribution sendmail.cf
#  files and the normal modifications to the sendmail.cf file (described 
#  in detail in the documentation) may automatically be performed.
#
#   Since the /etc/sendmail.cf file may be a symbolic link (perhaps to its
#  /usr/lib/sendmail.xxx.cf equivalent) it will always be copied to a backup
#  file and then replaced.
#  
#   Note:  It is assumed the definition for the mailer daemon may already be
#   defined in dniserver.reg.
#
#   Return Status:  0 = success, 1 = user decided against installation
#			-1 = error occured
#
#
unalias rm
unalias cp
unalias mv
unset noclobber
set ETC = /etc
set ULIB = /usr/lib

set vers = "1.0"
set MAIN = "main"
set SUB = "subsidiary"
set SMAIL = "sendmail"
#
echo " "
echo "	You have invoked the $vers $0 script.  It is designed"
echo "	to automate the installation of the DNI VMS Mail Gateway extension"
echo "	software to DNI.  It assumes the $ETC/$SMAIL.cf file is identical"
echo "	to one of the two templates provided in $ULIB.  If you have a"
echo "	modified $SMAIL.cf the script will notify you and exit gracefully," 
echo "	and you will have to perform the installation manually."
echo " "

#
#   Verify if /etc directory override arguments (testing)
#
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\!\!"
    echo " "
    exit (-1)
else if ($ETC != /etc) then
    echo "Test directory set to $ETC"
    echo " "
endif
#
setenv PATH /usr/ucb:/bin:/usr/bin:/etc:/usr/etc
#
set DNI_PATH = /usr/sunlink/dni
set DNII_PATH = $DNI_PATH/install
set DNI_CONFIG = /etc/sunlink/dni
set HOSTS = $DNI_CONFIG/dnihosts
set OHOSTS = $DNI_PATH/dnihosts

set OEXT = "nodni"
#
#  Verify the basics
#
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
#
foreach i ($DNI_CONFIG:h $DNI_CONFIG)
    if (!(-e $i)) then
	echo "   Issuing mkdir $i ..."
	mkdir $i
	if ($status) then
	    echo "	... failure ... fatal error"
	    exit (-1)
	endif
    endif
end
echo " "
#
#		Verify operator's desire to run this script
#
ques1:
echo -n "Do you wish to make this node a DNI VMS Mail Gateway [Y/N]:   "
set ans = ($<)
echo " "
if (($ans == "n")||($ans == "no")||($ans == "N")||($ans == "NO")) then
   echo "	If you later change your mind, simply rerun this script."
   echo "			Good Luck"
   exit (1)
else if (($ans != "y")&&($ans != "yes")&&($ans != "Y")&&($ans != "YES")) then
   echo "You have typed in an illegal choice.  Please enter Y or N"
   goto ques1
endif
#
#
#  Now diff the sendmail.cf virgin files to see if a match with /etc/sendmail.cf
#
set x1 = "$ULIB/$SMAIL.$MAIN.cf $ULIB/$SMAIL.$SUB.cf $ETC/$SMAIL.cf"
set x2 = "$DNII_PATH/$SMAIL.$MAIN.cf.dni $DNII_PATH/$SMAIL.$SUB.cf.dni"
set x = "$x1 $x2"
foreach i ($x)
    if (!(-e $i)) then
	echo "$i does not exist ... installation aborted"
	exit (-1)
    endif
end
set mtype = ""
foreach i ($MAIN $SUB)
    /bin/diff $ETC/$SMAIL.cf $ULIB/$SMAIL.$i.cf >/dev/null
    if (!($status)) then
	set mtype = $i
	break
    endif
end
if ($mtype =="") then
    echo "    Your $ETC/$SMAIL.cf file has been modified from the standard"
    echo "  templates $ULIB/$SMAIL.$MAIN.cf and $ULIB/$SMAIL.$SUB.cf".
    echo " "
    echo "    Please see the installation instructions for details about how"
    echo "  $ETC/$SMAIL.cf must be modified to support the DNI VMS Mail"
    echo "  Gateway functionality."
    exit (1)
endif

#
#  Hookay .. mtype has the main or subsidiary flag.  Get the VMS host list.
#

echo "	Your $ETC/$SMAIL.cf file reveals that this system is a standard"
echo "	$mtype mail node, and the installation process can therefore"
echo "	continue."
echo " "
while (!(-r $HOSTS))
    echo " "
    echo "     You will now need to create a file called $HOSTS"
    echo "   which must contain a list of those VAX/VMS node names (one on"
    echo "   a line, lower case) which you wish to directly send mail to"
    echo "   from this Sun node."
    echo " "
    echo "     It is not necessary to identify ALL accessible VMS nodes in"
    echo "   this file, as a listed node can deliver mail to a VMS node that"
    echo "   you didn't list.  Note that this would require a listed VMS node"
    echo "   name to precede any unlisted name in the email address."
    echo " "
    echo " "
    echo "     Please create the $HOSTS file before responding to"
    echo "   the next question.  If you already have such a file, you need"
    echo "   only create a symbolic link to it from $HOSTS."
    echo " "
    if (-e $OHOSTS) then
	echo "     Note:  the $OHOSTS file may contain this"
	echo "   information."
	echo " "
    endif
    echo "     If you wish to stop the installation at this point, respond 'N'. "
#
ques2:
    echo " "
    echo -n "Continue with the DNI VMS Mail Gateway installation [Y/N]:   "
    set ans = ($<)
    echo " "
    if (($ans == "n")||($ans == "no")||($ans == "N")||($ans == "NO")) then
	echo "	  After you have created the $HOSTS file,"
	echo "	you may rerun this installation script."
	echo " "
	exit (1)
    else if (($ans != "y")&&($ans != "yes")&&($ans != "Y")&&($ans != "YES")) then
	echo "You have typed in an illegal choice.  Please enter Y or N"
	goto ques2
    else if (!(-r $HOSTS)) then
	    echo "    $HOSTS is still unreadable."
	    echo " "
    endif
end
#
echo " "
echo "    Based upon the data provided in $HOSTS, the following"
echo "  VMS nodes may be directly addressed in email messages to this system:"
echo " "
echo " "
cat $HOSTS
echo " "
#
ques3:
echo " "
echo -n "Continue with the DNI VMS Mail Gateway installation [Y/N]:   "
set ans = ($<)
echo " "
if (($ans == "n")||($ans == "no")||($ans == "N")||($ans == "NO")) then
    echo "	  After you have recreated the $HOSTS file,"
    echo "	you may rerun this installation script."
    echo " "
    exit (1)
else if (($ans != "y")&&($ans != "yes")&&($ans != "Y")&&($ans != "YES")) then
   echo "You have typed in an illegal choice.  Please enter Y or N"
   goto ques3
endif
#
#	VMS hosts file is set ... now do the substitutions
#
echo "    If you decide to continue with this script the following actions"
echo " will occur:"
echo " "
echo "	1.  The current $ETC/$SMAIL.cf file will be backed up as"
echo "	$ETC/$SMAIL.cf.$OEXT."
echo " "
echo "	2. It will then be replaced by $DNII_PATH/$SMAIL.$mtype.cf.dni"
echo "	which contains modifications necessary for the DNI VMS Mail Gateway"
echo "	software to run on this node." 
echo " "
ques4:
echo " "
echo -n "Continue with the DNI VMS Mail Gateway installation [Y/N]:   "
set ans = ($<)
echo " "
if (($ans == "n")||($ans == "no")||($ans == "N")||($ans == "NO")) then
    echo " You may rerun this installation script at a later time"
    echo " "
    exit (1)
else if (($ans != "y")&&($ans != "yes")&&($ans != "Y")&&($ans != "YES")) then
   echo "You have typed in an illegal choice.  Please enter Y or N"
   goto ques4
endif
#
#   Carefully replace sendmail.cf to prevent problems with possible symlink.
#

#  Delete old backups
if (-e $ETC/$SMAIL.cf.$OEXT) then
   rm -f $ETC/$SMAIL.cf.$OEXT
endif
echo "Issuing ...."
echo " "
echo "   cp $ETC/$SMAIL.cf $ETC/$SMAIL.cf.$OEXT  ..."
cp $ETC/$SMAIL.cf $ETC/$SMAIL.cf.$OEXT
if ($status) then
    echo "	...failure ... error exit"
    exit (-1)
endif
#
echo "   cp $DNII_PATH/$SMAIL.$mtype.cf.dni $ETC/$SMAIL.cf  ..."
cp $DNII_PATH/$SMAIL.$mtype.cf.dni $ETC/$SMAIL.cf
if ($status) then
    echo "	...failure ... error exit"
    exit (-1)
endif
echo " "
echo "	  $ETC/$SMAIL.cf  upgrades successfully completed.  You must"
echo "	 either abort and restart $SMAIL or reboot the system to complete"
echo "	 the DNI VMS Mail Gateway installation."
#
ques5:
echo " "
echo -n "Do you wish to abort and then restart $SMAIL at this time [Y/N]:   "
set ans = ($<)
echo " "
if (($ans == "n")||($ans == "no")||($ans == "N")||($ans == "NO")) then
    echo " The DNI VMS Mail installation will complete upon system reboot."
    echo " "
    exit (0)
else if (($ans != "y")&&($ans != "yes")&&($ans != "Y")&&($ans != "YES")) then
   echo "You have typed in an illegal choice.  Please enter Y or N"
   goto ques5
endif
#
#  Get ps line for sendmail .. find active process #
#
set x = `/bin/ps -ax | /bin/grep $ULIB/$SMAIL | /bin/grep -v grep`
if (!($#x)) then
    echo "   It appears that $SMAIL is not active ... should be restarted."
    echo "	DNI VMS Mail Gateway Installation completed."
    exit (0)
endif
#
set y = `echo $x | /bin/grep -c $ULIB/$SMAIL`
if ($y != "1") then
    echo "   Failure to interpret ps output ... system should be restarted"
    echo "   to complete DNI VMS Mail Gateway Installation.    <$y>"
    exit (0)
endif

@ tnum = $x[1]
echo "   $SMAIL running ..."
echo "	$x"
echo " "
echo "   Kill it ..."
echo "	kill -9 $tnum ..."
kill -9 $tnum
echo " "

set y = `/bin/ps -ax | /bin/grep $ULIB/$SMAIL | /bin/grep -v grep`
if ($#y) then
    echo " $SMAIL process cannot be killed."
    echo " The DNI VMS Mail installation will complete upon system reboot."
    echo " "
    exit (0)
endif
#
#  Get field # for sendmail in ps line .. this to the end is the restart command
#
@ snum = 2
while ($snum < $#x)
    if ($x[$snum] == $ULIB/$SMAIL) then
	break
    else
	@ snum++
    endif
end
if ($snum >= $#x) then
    echo "   Bad ps line $x .... $SMAIL process may be dead \!\!"
    echo " The DNI VMS Mail installation will complete upon system reboot."
    echo " "
    exit (0)
endif
#
set rcommand = `echo $x[$snum-]`
echo "Issuing $rcommand ...."
echo " "
$rcommand
set y = `/bin/ps -ax | /bin/grep $ULIB/$SMAIL | /bin/grep -v grep`
if (!($#y)) then
    echo "	.... failure to restart $SMAIL .... process may be dead \!\!"
    echo " The DNI VMS Mail installation will complete upon system reboot."
    echo " "
    exit (0)
else
    echo "	... $y"
endif
echo " "
#
echo "  The sendmail program has been successfully restarted."
echo "  Installation of DNI's VMS Mail Gateway on top of DNI is now complete."
echo " "
exit (0)

