#!/bin/csh -f
#
# $Header: /cujo/users/src/local/common/bin/addhost/admintools/mkhostsections/RCS/mkhostsections,v 1.1 90/08/23 20:52:52 hardt Exp $
#
# This utility is used to create /usr/local/adm/hosts/hostsrc/* which
# are used to create /etc/hosts and to create the named database.
#

# DIAG controls the amount of verbosity to the info mail message
set DIAG = 0

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

set diffs		= /tmp/mkhs.diff.$$
set dumphostout	= /tmp/mkhs.dump.$$
set dhconf		= /tmp/mkhs.dhconf.$$
set errors		= /tmp/mkhs.errors.$$
set temp		= /tmp/mkhs.temp.$$
set parseoutput	= /tmp/mkhs.parse.$$

set DESTDIR		= /usr/local/adm/hosts/hostsrc
set MAXDIFFS	= 50
set mailto		= trent
set errs		= 0

set sections	= ( cnsjones chemistr symbolic plasma mcdb unixops csops woz seusslab ccar misc clipr-hp hpops averynet )

cat /dev/null > $errors

foreach SECTION ($sections)
	set sectiondest = $DESTDIR/$SECTION
	set newsection = /tmp/$SECTION.hosts.$$
	set oldsection = $DESTDIR/mk.$SECTION/$SECTION
	echo Starting $SECTION section. >>& $errors

	cat /dev/null > $dhconf
	echo set adminaffil $SECTION	>> $dhconf
	echo show inetdomain			>> $dhconf
	echo show model					>> $dhconf
	echo show location				>> $dhconf
	echo show inetaddr1				>> $dhconf
	echo show hostname				>> $dhconf
	echo show inetaddr2				>> $dhconf
	echo show inetname2				>> $dhconf
	echo show inetaddr3				>> $dhconf
	echo show inetname3				>> $dhconf
	echo show inetaliases			>> $dhconf
	echo show comment1				>> $dhconf
	echo match						>> $dhconf
	
	dumphost $dhconf > $dumphostout
	
	if ($status != 0) then
		echo "Error getting data from dumphost." >>& $errors
		echo "##########" >>& $errors
		cat $dumphostout >>& $errors
		echo "##########" >>& $errors
		goto DONE
	endif
	if ($DIAG) then
		echo completed dumphost >>& $errors
	endif
	
	cat $dumphostout | parsehosts > $parseoutput

	if ($status != 0) then
		echo "Error parsing data from dumphost." >>& $errors
		goto DONE
	endif
	if ($DIAG) then
		echo completed parsehosts >>& $errors
	endif

	echo "##" > $newsection.date
	echo "# Following section autogenerated from AddHost:   `date`" >> $newsection.date
	grep -v "^?" $parseoutput >> $newsection
	
	if ($status != 0) then
		echo Error removing errors/warnings from parsed $SECTION file. >>& $errors
		goto DONE
	endif
	if ($DIAG) then
		echo completed greping out errors/warnings from dumphost output >>& $errors
	endif
	
	unexpand -a $newsection > $temp
	if ($status != 0) then
		echo error trying to run unexpand >>& $errors
		goto DONE
	endif
	
	sort $temp -o $temp
	if ($status != 0) then
		echo error trying to run sort >>& $errors
		goto DONE
	endif

	cat $temp | tr A-Z a-z > $newsection
	if ($status != 0) then
		echo error trying to translate to lower case >>& $errors
		goto DONE
	endif

	diff $oldsection $newsection > $diffs
	set STATUS = $status
	set diffr = `wc -l $diffs | awk '{ print $1 }'`
	
	if ($STATUS == 0) then
		echo No change to $SECTION section since no diffs found. >>& $errors
	else if ($diffr < $MAXDIFFS) then
		echo Overwrote old $SECTION section. >>& $errors
		echo Changes to $SECTION section since last update, diff old new: >>& $errors
		echo "##########" >>& $errors
		cat $diffs >>& $errors
		echo "##########" >>& $errors
		cp $newsection $oldsection
		cp $newsection.date $oldsection.date
		cat $oldsection.date	 > $temp
		cat $oldsection.header	>> $temp
		cat $newsection			>> $temp
		cat $oldsection.footer	>> $temp
		cp $temp $sectiondest
	else
		echo Error: Too many changes to $SECTION section to be safely used - Aborting\! >>& $errors
		cp $newsection $oldsection.err
		echo Put bad file in $oldsection.err >>& $errors
		goto DONE
	endif
	
	DONE:
	if ($DIAG) then
		grep "^?error" $parseoutput >& $temp
		set errorcount = `wc -l $temp | awk '{ print $1 }'`
		if ($errorcount != 0) then
			echo non-fatal errors and warnings from parsehosts: >>& $errors
			echo "##########" >>& $errors
			cat $temp >>& errors
			echo "##########" >>& $errors
		endif
	else
		grep "^?error" $parseoutput >& $temp
		set errorcount = `wc -l $temp | awk '{ print $1 }'`
		if ($errorcount != 0) then
			echo non-fatal errors from parsehosts: >>& $errors
			echo "##########" >>& $errors
			cat $temp >>& errors
			echo "##########" >>& $errors
		endif
	endif
	rm $diffs $parseoutput $newsection $dumphostout $dhconf $newsection.date $temp
end

/usr/ucb/mail -s "daily mkhostsections" $mailto < $errors
rm $errors

/usr/local/adm/hosts/tools/updatehosts.daily
