#!BOURNESHELL
#	subdom2
#
#	This is a part of subdom, and should not be used by anyone else.
#
#	Print a list of sub-domains in this domain.  This is not recursive
#	unless the "-r" command-line flag is given.  This is the whole program
#	except that the output from here needs to be uniq'd, but we can't
#	easily do that from here because we may call ourselves recursively,
#	so the outer shell script "subdom" does that job.  Therefore this
#	program is NOT INTENDED FOR DIRECT USER USE.  Users should run the
#	"subdom" program instead.
#
TMP=/tmp/subdom1.$$
TMP2=/tmp/subdom2.$$

SUBDOMAWK=DOMLIB/subdom.awk

usage() {
	echo "usage: subdom [-r] dom.ain." >&2
	echo 'ERROR'
	exit 1
}

if test $# -lt 1 -o $# -gt 2; then
	usage
fi

domain="" RECURSIVE=false
for arg do
	case "$arg" in
		-r)	RECURSIVE=true; shift ;;
		*)	if test x"$domain" = x""; then
				domain="$arg"
			else
				usage
			fi ;;
	esac
done
if test x"$domain" = x""; then
	usage
fi

# Figure out the zone for this domain.

zone=`DOMBIN/zone $domain`
if test $? -ne 0; then
	echo "subdom: cannot determine what zone $domain is in." >&2
	echo 'ERROR'
	exit 1
fi

# Get the zone dump for this zone.

DOMBIN/axfr $zone > $TMP
if test $? -ne 0; then
	echo "subdom: cannot get zone dump of $domain" >&2
	echo 'ERROR'
	rm -f $TMP
	exit 1
fi

# Build a list of domains, one per line.  Add "subdomain" after it if this
# domain is definitely in another zone (was found on an SOA or NS record).
# This also sorts the records by domain, upper-level domains first.
# The extra "uniq" should stay in here to reduce the number of
# records that domsort must look at, which speeds things up a lot.
# This list may include the current domain, not just subdomains.
# Any given domain could be listed twice; with and without "subdomain"
# as its second field.

awk -f $SUBDOMAWK $TMP | uniq | DOMBIN/domsort -u | \
	PERLPROG -ne "@a=split(' ', \$_, 9999); print \$_ if \$a[0] =~ /\\b$domain\$/i" > $TMP2

# If the query to a nameserver succeeded but awk parsing returns no records,
# then we exit normally.

lines=`wc -l < $TMP2`
if test $lines -eq 0; then
	rm -f $TMP $TMP2
	exit 0
fi

# Loop through all subdomain info records we built above.

i=1
while test $i -le $lines; do

# Get each record from the file.  $1 is a domain name,
# $2 is "subdomain" if $1 is in another zone for sure;
# $2 nonexistent if we aren't sure.

	set `sed -e $i!d < $TMP2`

# If recursive, print all domains including the current one.
# If not recursive, print all but the current one.

	isme=`DOMBIN/isequal "$1" "$domain"`
	if test "$isme" = "false" -o $RECURSIVE = true; then
		echo $1
	fi

# If recursive mode specified and the current domain is in another zone
# and it's not the zone we're already in, then call ourselves recursively.

	if $RECURSIVE; then
		if test $# -gt 1; then
			if test "$2" = "subdomain"; then
				if test "$isme" = "false"; then
					DOMBIN/subdom2 -r $1
if test $? -ne 0; then
	echo "subdom: recursive call to subdom2 failed; skipping subdomain $1" >&2
fi
				fi
			fi
		fi
	fi

# Bottom of domain names loop.

	i=`expr $i + 1`
done

# Cleanup and exit.

rm -f $TMP $TMP2

exit 0
