#!/bin/csh -f
#
# $Header: /cujo/users/src/local/common/bin/addhost/admintools/RCS/mkannexhosts,v 1.1 90/08/23 20:48:44 hardt Exp $
#

#set dhconf = /tmp/dh.config.$$
set dhconf = dh.config.$$
#set annexhosts = /tmp/dh.annexinfo.$$
set annexhosts = dh.annexinfo.$$
#set oldannexhosts = /etc/annex/annexhosts
set oldannexhosts = annexhosts
#set annexhoststmp = /tmp/dh.annexhosts.$$
set annexhoststmp = dh.annexhosts.$$
#set error = /tmp/dh.error.$$
set error = dh.error.$$
#set MAXDIFFS = 10
set MAXDIFFS = 1000
set mailto = hardt

/usr/bin/touch $error
set errs = 0

/bin/echo 'set csannex y'		>  $dhconf
/bin/echo 'show hostname'		>> $dhconf
/bin/echo 'show inetaliases'	>> $dhconf
/bin/echo 'show inetdomain'		>> $dhconf
/bin/echo 'match'				>> $dhconf
/bin/echo 'quit'				>> $dhconf

/usr/local/etc/dumphost $dhconf > $annexhosts
if ($status != 0) then
	/bin/echo "Error: Could not make annexhosts" >> $error
	set errs = 1
endif

if (${errs} == 1) then
	/bin/echo "Error: Could not continue with list creations - Aborting\!" >> $error
	/usr/ucb/mail -s "MKANNEXHOSTS info" $mailto < $error
	/bin/rm $dhconf $annexhosts $error
	exit 1
else


	/bin/awk 										\
		'{											\
			for (i=1; i<NF; i++) print $i "." $NF	\
		}' $annexhosts | /usr/ucb/grep -v \? >> $annexhoststmp

	/bin/mv $annexhoststmp $annexhosts
	/bin/echo "annexhosts created" >> $error
endif

set errs = 0

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

set diffr = `/bin/diff $annexhosts $oldannexhosts | wc | awk '{print $1}'`
if ($diffr < $MAXDIFFS) then
	/bin/echo "Overwrote old annexhosts info" >> $error
	/bin/cp $annexhosts $oldannexhosts
else
	/bin/echo "Error: Too many changes to annexhosts list to be safely used - Aborting\!" >> $error
	set errs = 1
endif

/bin/echo "mkannexhosts completed" >> $error

/usr/ucb/mail -s "MKANNEXHOSTS info" $mailto < $error

# Clean up here.

/bin/rm $dhconf $annexhosts $error
exit 0
