#!BOURNESHELL
#	program:	nsroot
#	input:		domain name on command line.
#	output:		one or more name servers for this domain listed one
#			per line as returned by a root domain server,
#			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 1; then
	echo "usage: $0 dom.ain." >&2
	echo 'ERROR'
	exit 1
fi

rootservers=`DOMBIN/root 2>/dev/null`
status=$?
if test $status -ne 0; then
	echo "$0: cannot find list of root domain name servers." >&2
	echo 'ERROR'
	exit 1
fi
for ns in `DOMBIN/rndarg $rootservers`; do
	dig @$ns $1 NS | awk -f $AWK | awk '$1=="NS"{print $2}'
	status=$?
	if test $status -eq 0; then
		break
	fi
done

if test $status -ne 0; then
	echo "$0: no root name servers had NS records for $1" >&2
	echo 'ERROR'
	exit 1
fi
exit $status
