#!/bin/csh -f
#
# This dumps the entire hosts database, in a form ready to be sent
# directly to badhost, to the file defined by the first argument on the
# command line, or to addhostdb.dump.XXX if no argument is found on the
# command line.  XXX is the 3 letter abreviation for the current day of
# the week, and so is a handy way to keep 7 days of dumps of the database
# around.
#
umask 022

if ($#argv == 1) then
	set tmpoutput = $argv[1].tmp
	set output = $argv[1]
else
	set tmpoutput = tmp.addhostdb.dump.`date | awk '{ print $1}'`
	set output = addhostdb.dump.`date | awk '{ print $1 }'`
endif

set path = (/bin /usr/local/etc /usr/local/share/etc)
set dhconf = /tmp/dh.config.$$
set mailto		= hardt

cat /dev/null > $dhconf
echo 'list'   >> $dhconf
echo 'quit'   >> $dhconf

# use echo to put all field names on one line
echo `dumphost $dhconf` | sed -e 's/ /	/g' > $tmpoutput

cat /dev/null > $dhconf
echo 'set hostname *' >> $dhconf
echo 'show all'       >> $dhconf
echo 'match'          >> $dhconf
echo 'quit'           >> $dhconf

dumphost $dhconf >> $tmpoutput

set STATUS = $status
if ($STATUS != 0) then
	echo look in $tmpoutput for errors | mail -s "mkhostsbackup" $mailto
else
	mv $tmpoutput $output
endif

rm $dhconf
exit($STATUS)
