#!BOURNESHELL
#	program:	cachebuild
#	purpose:	Build a new root cache file for a BIND nameserver.
#	input:		none.
#	output:		"A" and "NS" records, with comments, in the form of
#			a root.cache file for a BIND nameserver.
#	exit value:	0 if succeeds, 1 if "root" tool fails,
#			2 if "root" returned no records,
#			3 if could not obtain NS records for any nameserver.
#			Errors go to stderr.
#

# Temporary storage file
TMP=/tmp/`basename $0`.$$

# --------------------------------------------------------------------------

# Determine if we're using SysV or BSD style echo's.

if test `echo "a\c" | wc -l` -eq 1; then
	ECHON=-n ECHOC=			# BSD
else
	ECHON= ECHOC='\c'		# SysV
fi

# Locate all root nameservers

DOMBIN/root > $TMP
if test $? -ne 0; then
	echo "cachebuild: ERROR: 'root' tool returned failure." >&2
	rm -f $TMP
	exit 1
fi
if test `wc -l < $TMP` -lt 1; then
	echo "cachebuild: ERROR: 'root' tool returned no records." >&2
	rm -f $TMP
	exit 2
fi

# Generate header info

echo ";"
echo "; root.cache built by $USER@`hostname` on `date`"
echo "; Automatically built by cachebuild, any changes you make may go away."
echo ";"
echo ""
echo ";"
echo "; Initial cache data for root domain servers."
echo ";"
echo ""

# List NS records for root nameservers

echo $ECHON ".$ECHOC"
for ns in `cat $TMP`; do
	echo "			99999999	IN	NS	$ns"
done

# More header info

echo ""
echo ";"
echo "; Prep the cache (hotwire the addresses).  Order does not matter."
echo ";"
echo ""

# List A records for root nameservers

for ns in `cat $TMP`; do
	addrs=`DOMBIN/address $ns`
	if test $? -ne 0; then
		echo "cachebuild: ERROR: could not obtain address RRs for $ns" >&2
		rm -f $TMP
		exit 3
	fi
	for addr in $addrs; do
		echo $ECHON "$ns	$ECHOC"
		len=`echo $ECHON "$ns$ECHOC" | wc -c`
		if test $len -lt 8; then
			echo $ECHON "		$ECHOC"
		elif test $len -lt 16; then
			echo $ECHON "	$ECHOC"
		fi
		echo "99999999	IN	A	$addr"
	done
done

rm -f $TMP
exit 0
