#!BOURNESHELL
#	program:	makestatic
#	purpose:	Build three static network tables based on DNS queries
#			in the exact syntax of /etc/hosts, /etc/networks, and
#			/etc/netmasks (the last primarily used on Suns), and
#			install them in some globally accessable directory.
#			There are static header and footer files for each,
#			which can be edited as needed to contain comments,
#			special addresses, and other stuff not available
#			via DNS queries.
#	input:		Domain name and destination directory on cmd line.
#	output:		Three files named "hosts", "networks", "netmasks".
#			or the word "ERROR" (distinguishable from a real
#			record because it has no period on the end).
#	exit value:	0 if succeeds, or exit status of the last file
#			we attempted to build.  We attempt to build each file
#			regardless of whether the previous one(s) succeeded
#			or failed to build.
#

# Where to put the static table files after we build them:
PUTDIR=/public/nau

TMPHOSTS=/tmp/`basename $0`-hosts.$$
TMPNETWORKS=/tmp/`basename $0`-networks.$$
TMPNETMASKS=/tmp/`basename $0`-netmasks.$$

# Get domain we're supposed to work with from command line.
if test $# -ne 2; then
	echo "usage: makestatic dom.ain. /dir/ectory" >&2
	exit 1
fi
domain="$1" PUTDIR="$2"

# Verify arguments.

if test ! -d "$PUTDIR"; then
	echo "$0: $PUTDIR is not a directory or doesn't exist." >&2
	echo "usage: makestatic dom.ain. /dir/ectory" >&2
	exit 1
fi
if test ! -w "$PUTDIR"; then
	echo "$0: $PUTDIR is not writable by me." >&2
	exit 1
fi

# Assume there won't be any problems.
status=0

# Figure out if networktbl and netmasktbl were built with -x option.
networksdomopt=""
test x"NETWORKSDOM" != x"" && networksdomopt="-x"


# 1. Build the /etc/hosts table from DNS queries.

DOMBIN/hosttbl $domain > $TMPHOSTS
hoststatus=$?
if test $hoststatus -eq 0; then
	rm $PUTDIR/hosts >/dev/null 2>&1
	cp $TMPHOSTS $PUTDIR/hosts
	chmod 644 $PUTDIR/hosts
else
	status=$hoststatus
fi
rm -f $TMPHOSTS


# 2. Build the /etc/networks table from DNS queries.

DOMBIN/networktbl -h $networksdomopt $domain > $TMPNETWORKS
netwstatus=$?
if test $netwstatus -eq 0; then
	rm $PUTDIR/networks >/dev/null 2>&1
	cp $TMPNETWORKS $PUTDIR/networks
	chmod 644 $PUTDIR/networks
else
	status=$netwstatus
fi
rm -f $TMPNETWORKS


# 3. Build the /etc/netmasks table from DNS queries.

DOMBIN/netmasktbl $networksdomopt $domain > $TMPNETMASKS
netmstatus=$?
if test $netmstatus -eq 0; then
	rm $PUTDIR/netmasks >/dev/null 2>&1
	cp $TMPNETMASKS $PUTDIR/netmasks
	chmod 644 $PUTDIR/netmasks
else
	status=$netmstatus
fi
rm -f $TMPNETMASKS


exit $status
