#!/bin/sh
#	program:	hosttbl
#	release:	1.5.0
#	purpose:	Build an /etc/hosts type table on stdout.
#			We also append the header and a trailer files.
#			Output is categorized by domain, but sub-categorization
#			is by sorted IP address (first white-spc delimited
#			column).  Any line in the header or trailer with
#			the string "TIMEDATE" on it will be replaced with
#			the output from the "date" cmd.
#	input:		Domain name on command line, with optional alias level
#			number before it (default is 2),
#			optional nameserver to direct queries to.
#	output:		/etc/hosts like table generated on stdout
#			or the word "ERROR", with a message on stderr.
#	exit value:	0 if succeeds, 1 if fails (and "ERROR" output).
#
# Copyright (C) 1993-2000 Paul A. Balyoz <pab@domtools.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

TMP="/tmp/`basename $0`.$$"
TMPPERL="/tmp/`basename $0`-perl.$$"

AWK="/usr/local/domtools/lib/hosttbl.awk"
PERL="/usr/local/domtools/lib/hosttbl.perl"

usage() {
	echo "usage: $0 [@nameserver] [aliaslevel] dom.ain." >&2
	exit 1
}

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

domain=
atnameserver=
while test $# -gt 0; do
        case "$1" in
                @*)  atnameserver="$1" ;;
                *)   if test x"$domain" = x""; then
			domain="$1"
		     else
			if test x"$al" = x""; then
				al="$domain"		# the ol' shiftarooney
				domain="$1"
			else
				usage
			fi
		     fi ;;
        esac
        shift
done

if test x"$domain" = x""; then
        echo "$0: No domain name specified; aborting." >&2
        usage
fi

if test x"$al" = x""; then
	# Default number of alias levels to generate per host on output.
	al="2"		# macro substituted from Makefile
else
	if test "$al" -lt 0 -o "$al" -gt 99; then
		echo "$0: please keep the alias level small, but no smaller than 0." >&2
		exit 1
	fi
fi

# Sanity Check
if test x"$al" = xALIAS""LEVELS; then		# embedded quotes to avoid macro here!
	echo "$0: I haven't been properly installed, see README and Makefile." >&2
	exit 1
fi

HOST=`hostname`
cat /usr/local/domtools/lib/hosttbl.header | \
	sed -e "s/TIMEDATE/`date`/g" | \
	sed -e "s/USER/$USER/g" | \
	sed -e "s/HOST/$HOST/g"

subdomains=`/usr/local/domtools/bin/subdom -r $atnameserver $domain`
if test $? -ne 0; then
	echo "hosttbl: cannot get list of subdomains for $domain" >&2
	echo 'ERROR'
	exit 1
fi

for sd in $subdomains; do
	if test x"$sd" = x"ERROR"; then
		continue
	fi
	echo '#########################################'
	echo "# Hosts in domain $sd"
	echo '#########################################'

# Scan the zone for only three specific types of records:
# SOA records for $sd domain, NS records for $sd domain,
# and any records for anyhost.$sd domain.
	sed -e "s/XXXX/$sd/" $PERL > $TMPPERL
	zone=`/usr/local/domtools/bin/zone $atnameserver $sd`
	/usr/local/domtools/bin/axfr $atnameserver $zone | /usr/local/bin/perl5.00502 -n $TMPPERL > $TMP
	if test $? -ne 0; then
		echo "hosttbl: cannot get zone dump of $sd; skipping it." >&2
	else
#echo "DEBUG: working on subdom $sd in zone $zone" >&2
		/usr/bin/awk -f $AWK -v DOMAIN="$sd" -v ALIASLV="$al" < $TMP | /usr/local/domtools/bin/ipsort
	fi
	echo ''
	rm -f $TMPPERL
done

cat /usr/local/domtools/lib/hosttbl.footer | \
	sed -e "s/TIMEDATE/`date`/g" | \
	sed -e "s/USER/$USER/g" | \
	sed -e "s/HOST/$HOST/g"

rm -f $TMP
exit 0
