#!BOURNESHELL
#	program:	root
#	input:		<none>
#	output:		one or more root domain name servers for the root domain
#			as returned by one of the root domain servers, one per
#			line, or the word "ERROR" (distinguishable from a real
#			record because it has no period on the end).
#			If error occurs, stderr describes it.
#	exit value:	0 if succeeds, 1 if fails (and "ERROR" output).

AWK=DOMLIB/digoutany.awk

if test $# -ne 0; then
	echo "usage: $0" >&2
	echo 'ERROR'
	exit 1
fi

# I don't like how "doc" requires you to type the parent zone name by hand with each query.

rootnslist=`dig @. . ns 2>/dev/null | awk -f digoutany.awk | awk '{print $2}'`
if test $? -ne 0 -o x"$rootnslist" = x""; then
	rootnslist='C.ROOT-SERVERS.NET. E.ROOT-SERVERS.NET.'		# failsafe, sorta.
fi

domain=.

realrootnslist=''
for ns in $rootnslist; do
	if test x"$realrootnslist" = x""; then
		realrootnslist=`dig @$ns $domain NS | awk -f $AWK | awk '$1=="NS"{print $2}'`
	fi
done

if test x"$realrootnslist" = x""; then
	echo "$0: None of the root name servers were reachable." >&2
	echo 'ERROR'
	exit 1
fi

for ns in $realrootnslist; do
	echo $ns
done
exit 0
