#!/bin/sh -e
#
# Interactively configure BIND for Debian
#
# Robert Leslie <rob@mars.org>

reload="yes"
umask 022

###############################################################################

while [ $# -gt 0 ]
do
    case "$1" in
	--no-reload)
	    reload=""
	    shift
	;;

	*)
	    echo "Usage: $0 [--no-reload]" >&2
	    exit 1
	;;
    esac
done

###############################################################################

descrip() {
    echo ""
    echo "$1"
    echo "$1" | sed -e 's/./-/g'
    cat -
    echo ""
}

yesno() {
    local N="N" Y="Y"
    local q="$1" def=$(eval "echo -n \$$2")

    while :
    do
	echo -n "$q? [$def] "
	read REPLY
	REPLY=$(echo "$REPLY" | sed -e 's/^[ 	]*//' -e 's/[ 	]*$//')
	test -n "$REPLY" || REPLY="$def"

	case "$REPLY" in
	    [Yy]*)
		eval "$2=\"Y\""
		return 0
	    ;;

	    [Nn]*)
		eval "$2=\"N\""
		return 1
	    ;;
	esac
    done
}

input() {
    local q="$1" def=$(eval "echo -n \$$2")

    echo -n "$q? [$def] "
    read REPLY
    REPLY=$(echo "$REPLY" | sed -e 's/^[ 	]*//' -e 's/[ 	]*$//')
    test -n "$REPLY" || REPLY="$def"

    if [ NONE = "$3" ]
    then
	test none != "$REPLY" -a NONE != "$REPLY" || REPLY=""
    fi

    eval "$2=\"$REPLY\""
}

testconfig() {
    CONFIG=$(grep "$1" $options 2>/dev/null)
}

reload() {
    if [ "$reload" ] &&  \
	yesno "Reload named now with the new configuration" Y
    then
	echo "Reloading named ..."
	/etc/init.d/bind reload >/dev/null
    fi
}

###############################################################################

options="/var/named/boot.options"
zones="/var/named/boot.zones"

exec 3>$options.new
trap "rm -f $options.new" 0

###############################################################################

descrip "BIND Configuration" <<EOT
By answering the following questions, you can configure BIND for your system.
If your system has already been configured, the default values will allow you
to verify your existing configuration.
EOT

echo -n "Press [ENTER] "
read REPLY

cat >&3 <<EOT
;
; Options for name server
; Use \`bindconfig' to automatically configure this file
;

EOT

###############################################################################

descrip "Forwarder Hosts" <<EOT
If you are close to a well-connected host or set of hosts which accept
recursive DNS queries, it would be to your advantage to use them as forwarders
in order to reduce traffic over links to outside servers.

Your DNS server will send all queries not in its cache to the forwarders
first. Each forwarder will be asked in turn until an answer is returned or the
list is exhausted. If no answer is forthcoming from a forwarder, the server
will continue as it would have without the forwarders.

To answer this question, separate each address with a space, or answer \`none'
to eliminate all forwarder hosts.
EOT

forwarders=""
! test -f $options ||  \
    forwarders=$(sed -n -e '/^forwarders/s/^forwarders//p' $options |
		 tr -s '\n \t' ' ' | sed -e 's/^ *//' -e 's/ *$//')

input "Forwarder IP addresses" forwarders NONE

test -z "$forwarders" || echo "forwarders	$forwarders" >&3

###############################################################################

if [ -n "$forwarders" ]
then

descrip "Forward Only" <<EOT
You can configure your DNS server as a "slave" -- that is, it will query only
the forwarder host(s) for answers to queries not in its cache. This is
recommended if your machine is not directly connected to the Internet, because
your server will then not try to contact hosts other than the forwarders.
EOT

forwardonly="N"
! testconfig "^options[ 	]*forward-only" || forwardonly="Y"

! yesno "Enable forward-only mode" forwardonly ||  \
    echo "options		forward-only" >&3

fi

###############################################################################

descrip "Localhost Entries" <<EOT
With this option, BIND will contain entries for the \`localhost' pseudo-host
and its reverse mapping (127.0.0.1). This is recommended.
EOT

localhost="Y"
if [ -f $options ]
then
    testconfig "^primary[ 	]*localhost" || localhost="N"
fi

! yesno "Enable localhost entries" localhost || cat >&3 <<EOT

; type		domain			source		file
primary		localhost				named.local
primary		127.in-addr.arpa			named.rev-local
EOT

###############################################################################

echo "" >&3

if [ -f $options ]
then
    sed -n -e '/^;; Custom/,$p' $options >&3
else
    echo ";; Custom configurations below (will be preserved)" >&3
fi

if [ ! -f $zones ]
then
    cat >$zones <<EOT
;
; Name server zone boot file
; See named(8) for syntax and further information
;

; type		domain			source		file
EOT
fi

descrip "Configuration Complete" <<EOT
Advanced configuration, such as sortlists, xfrnets, limits, and other options
can be accomplished by manually editing the /var/named/boot.options
configuration file and reloading your nameserver. You may wish to refer to
the named(8) man page or review the documentation in /usr/doc/net/named to
assist in further customization.

This automatic configuration does not manipulate zone files; you should ensure
the proper boot entries are made in /var/named/boot.zones for each primary and
secondary zone you are serving. If you leave this file empty, your server will
act conveniently as a caching-only name server.
EOT

###############################################################################

exec 3>&-

if [ -f $options ]
then
    echo "Saving old $options to $options.old ..."
    mv -f $options $options.old
fi

mv -f $options.new $options
trap - 0

# Make sure we have a good /etc/named.boot

test ! -L /etc/named.boot || rm -f /etc/named.boot

if [ -e /etc/named.boot ]
then
    echo "Moving your /etc/named.boot to /var/named and leaving a symlink ..."
    mv -vi /etc/named.boot /var/named/named.boot.old
fi

ln -s ../var/named/named.boot /etc/named.boot

if [ -x /usr/sbin/named-bootconf ]
then
    /usr/sbin/named-bootconf
fi

reload
exit 0
