#!/bin/csh -f
#
# $Header: /cujo/users/src/local/common/bin/addhost/admintools/RCS/mkmailclasses,v 1.1 90/08/23 20:48:48 hardt Exp $
#
# This utility is used to create
#   /usr/local/lib/colorado.edu.smtp.hosts    and
#   /usr/local/lib/colorado.edu.decnet.hosts  and
#   /usr/local/lib/colorado.edu.alias.hosts
# which are included in the sendmail.cf file.
#
# John Hardt
# Mon Apr 16 13:27:09 MDT 1990
#

# /usr/ucb comes before /bin so we get the right mail program
set path = (/usr/ucb /bin /usr/bin /usr/local/etc)

set dhconf = /tmp/mkmc.dhconf.$$
set diffs = /tmp/mkmc.diffs.$$

set oldalias = /usr/local/lib/colorado.edu.alias.hosts
set alias = /tmp/mkmc.ainfo.$$
set aliastmp = /tmp/mkmc.alias.$$

set smtp = /tmp/mkmc.sinfo.$$
set oldsmtp = /usr/local/lib/colorado.edu.smtp.hosts
set smtptmp = /tmp/mkmc.stmp.$$

set dec = /tmp/mkmc.dinfo.$$
set olddec = /usr/local/lib/colorado.edu.decnet.hosts
set dectmp = /tmp/mkmc.dtmp.$$

set errors = /tmp/mkmc.errors.$$
set spid = /etc/sendmail.pid

set MAXDIFFS = 25

set mailto = trent

@ changes = 0
@ errs = 0

touch $errors

# Make the alias section

echo 'set mailpref smtp'	>  $dhconf
echo 'set clubuniquid y'	>> $dhconf
echo 'show hostname'		>> $dhconf
echo 'show inetname2'		>> $dhconf
echo 'show inetname3'		>> $dhconf
echo 'show inetaliases'		>> $dhconf
echo 'match'			>> $dhconf
echo 'quit'			>> $dhconf

dumphost $dhconf > $alias
if ($status != 0) then
	echo "Error: Could not make alias hosts" >> $errors
	@ errs ++
endif

# Make the smtp section

echo 'set mailpref smtp*'	>  $dhconf
echo 'show hostname'		>> $dhconf
echo 'show inetname2'		>> $dhconf
echo 'show inetname3'		>> $dhconf
echo 'show inetaliases'		>> $dhconf
echo 'match'			>> $dhconf
echo 'quit'			>> $dhconf

dumphost $dhconf > $smtp
if ($status != 0) then
	echo "Error: Could not make smtp hosts" >> $errors
	@ errs ++
endif

# Make the decnet section

echo 'set mailpref decnet'	>  $dhconf
echo 'show hostname'		>> $dhconf
echo 'show inetname2'		>> $dhconf
echo 'show inetname3'		>> $dhconf
echo 'show inetaliases'		>> $dhconf
echo 'match'			>> $dhconf
echo 'quit'			>> $dhconf

dumphost $dhconf > $dec
if ($status != 0) then
	echo "Error: Could not make decnet hosts" >> $errors
	@ errs ++
endif

if ($errs != 0) then
	echo "Error: Could not continue with list creations - Aborting\!" >> $errors
	mail -s "MKMAILCLASSES info" $mailto < $errors
	rm $dhconf $alias $smtp $dec $errors
	exit 1
else
	awk '{for (i=1; i<=NF; i++) print $i }' $alias | \
		grep -v \? > $aliastmp
	awk '{for (i=1; i<=NF; i++) print $i }' $smtp | \
		grep -v \? > $smtptmp
	awk '{for (i=1; i<=NF; i++) print $i }' $dec | \
		grep -v \? > $dectmp
	mv $aliastmp $alias
	mv $smtptmp $smtp
	mv $dectmp $dec
	echo "Host lists created" >> $errors
endif

# To this point, we have collected all necessary info - now we
# just need to do some sanity checks!!!

diff $oldalias $alias > $diffs
set STATUS = $status
set diffr = `wc -l $diffs | awk '{ print $1 }'`

if ($STATUS == 0) then
	echo No change to alias host info since no diffs found. >>& $errors
else if ($diffr < $MAXDIFFS) then
	cp $alias $oldalias
	echo "Overwrote old alias host info" >> $errors
	@ changes ++
	echo Changes since last update, diff old new: >>& $errors
	echo "##########" >>& $errors
	cat $diffs >>& $errors
	echo "########## end diffs" >>& $errors
else
	echo "Error: Too many changes to alias host list to be safely used - Aborting\!" >> $errors
	cp $alias $oldalias.err
	echo Placed old alias host list in $oldalias.err >> $errors
	@ errs ++
endif

diff $oldsmtp $smtp > $diffs
set STATUS = $status
set diffr = `wc -l $diffs | awk '{ print $1 }'`

if ($STATUS == 0) then
	echo No change to smtp host info since no diffs found. >>& $errors
else if ($diffr < $MAXDIFFS) then
	cp $smtp $oldsmtp
	echo "Overwrote old smtp host info" >> $errors
	@ changes ++
	echo Changes since last update, diff old new: >>& $errors
	echo "##########" >>& $errors
	cat $diffs >>& $errors
	echo "########## end diffs" >>& $errors
else
	echo "Error: Too many changes to smtp host list to be safely used - Aborting\!" >> $errors
	cp $smtp $oldsmtp.err
	echo Placed old smtp host list in $oldsmtp.err >> $errors
	@ errs ++
endif

diff $olddec $dec > $diffs
set STATUS = $status
set diffr = `wc -l $diffs | awk '{ print $1 }'`

if ($STATUS == 0) then
	echo No change to decnet host info since no diffs found. >>& $errors
else if ($diffr < $MAXDIFFS) then
	cp $dec $olddec
	echo "Overwrote old decnet host info" >> $errors
	@ changes ++
	echo Changes since last update, diff old new: >>& $errors
	echo "##########" >>& $errors
	cat $diffs >>& $errors
	echo "########## end diffs" >>& $errors
else
	echo "Error: Too many changes to decnet host list to be safely used - Aborting\!" >> $errors
	cp $dec $olddec.err
	echo Placed old decnet host list in $olddec.err >> $errors
	@ errs ++
endif

# Ok - now we need to refreeze and restart sendmail - this munged from 
# something coggs ripped out of somewhere.

if (($errs == 0) && ($changes != 0)) then
	echo "No errors while replacing old info with new" >> $errors
	/usr/lib/sendmail -bz
	if ($status != 0) then
		echo "Error: Could not freeze sendmail config file" >> $errors
		@ errs ++
	endif
endif

if (($errs == 0) && ($changes != 0)) then
	echo "Sendmail config file frozen" >> $errors
	set pid = `cat ${spid}`
	if (${pid} == "") then
		echo "Error: Could not find old sendmail pid - Aborting\!" >> $errors
		@ errs ++
	else
		echo "Killing sendmail pid #${pid}" >> $errors
		kill -9 $pid
	endif
endif

if (($errs == 0) && ($changes != 0)) then
	# Sendmail restarted as in /etc/rc.local

	rm -f /usr/spool/mqueue/lf*
	set restart = `grep sendmail /etc/rc.local | grep -v then | awk -F\; '{print $1}'`
	$restart

	set pid = `ps ax | grep sendmail | grep accepting | awk '{print $1}'`
	echo "Restarted sendmail with pid #${pid}" >> $errors

	echo "mkmailclasses completed" >> $errors
endif

mail -s "MKMAILCLASSES info" $mailto < $errors

# Clean up here.

rm $diffs $dhconf $alias $smtp $dec $errors
exit 0
