#!BOURNESHELL
#	program:	zone
#	input:		domain name on command line.
#	output:		the name of the zone within which this domain exists,
#			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).

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

domain=$1

# Find the zone for this domain by trying dom.ain., ain., ., (etc)
# If none of these domains has an SOA record, we must fail.

soa=''
zone=$domain
keepgoing=true
while $keepgoing; do
	soa=`DOMBIN/soa $zone 2>/dev/null`
	status=$?
	if test $status -ne 0 -o X"$soa" = X; then
		zone=`DOMBIN/basedomain $zone`
		basestatus=$?
		if test $basestatus -ne 0; then
			keepgoing=false
		fi
	else
		keepgoing=false
	fi
done

if test $status -ne 0 -o X"$soa" = X; then
	echo "$0: could not figure out what zone $domain is in." >&2
	echo 'ERROR'
	exit 1
fi

echo $zone
exit 0
