#!/bin/sh

tld_list="./tld_list.txt"
if [ ! -e $tld_list ]; then
    dig @f.root-servers.net AXFR . | \
	awk -F. '/^[A-Z]+\..*\tNS\t/ {print $1}' | \
	sort | uniq \
	> $tld_list
fi

tlds="$@"
if [ -z $tlds ]; then
    tlds=`cat $tld_list`
fi

temp=`mktemp`

for tld in $tlds; do
    echo "Testing ${tld}..." 1>&2 
    allns=`dig +short NS ${tld}.` # Not every nameserver will send back the IPv6
                                  # glue so we test all of them
    > $temp
    for ns in $allns; do
	echo "  Testing ${ns} for ${tld}..." 1>&2 
	dig @${ns} NS ${tld}. >> $temp
    done
    cat $temp | \
	awk "/^[a-zA-Z0-9\.\-]+\.$tld\.\t.*IN\tAAAA/ {found++; addr[\$5] = \$1} \
              END {if (found) print \"$tld:\" ; for (a in addr) print addr[a] \" (\" a \")\"}"  
    rm $temp
done
