#!/bin/sh -e
#
# Debian package postinst
# Version 1.2
#
# Robert Leslie <rob@mars.org>

if [  "$(hostname --long)" = "netgod.net" ]
then
    echo postinst $*
fi

case "$1" in
    configure)
	# continue below
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
	exit 0
    ;;

    *)
	echo "postinst called with unknown argument \`$1'" >&2
	exit 0
    ;;
esac

# Make sure we're running a kernel >= 1.3.56
set -- $(uname -r | sed -e 's/\./ /g' -e 's/-.*$//')
if [ `expr $1 '*' 100000 + $2 '*' 1000 + $3` -lt 103056 ]
then
    echo "Warning: BIND requires kernel 1.3.56 or later."
    echo "You will probably encounter problems unless or until you upgrade."
    echo -n "Press [ENTER] "
    read line
fi

umask 022

update-rc.d bind defaults 19 >/dev/null

# 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

# Handle named.boot configuration

cd /var/named

# Ensure a named.boot file exists

test -e named.boot || cat >named.boot <<EOF
;
; Boot file for name server
;

directory /var/named

; type		domain			source		file
cache		.					named.root

; Zone boot information and daemon options are kept in other files

include boot.zones
include boot.options
EOF

if [ -f named.boot.old ]
then
    cat <<EOF

This version of the Debian bind package uses a special arrangement of files
in /var/named that makes it easy to modify your configuration using the
included \`bindconfig' utility. Would you like your existing files to be
EOF
    echo -n "automatically converted to use the new arrangement? [Y] "
    read yn
    test -n "$yn" || yn="Y"

    case "$yn" in
	[Nn]*)
	    echo ""
	    echo "Okay, leaving your named.boot in place."  \
		"You will not be able to use"
	    echo "\`bindconfig' to adjust your configuration."

	    mv -f named.boot.old named.boot
	;;

	*)
	    cat <<EOT

Your named.boot file is being restructured as follows:

  /var/named/named.boot.old   - your original named.boot
  /var/named/named.boot       - new file; should not be modified
  /var/named/boot.options     - named options file; you may modify some
  /var/named/boot.zones       - named zone configurations; you may modify all
EOT

	    (
		echo "; Please run \`bindconfig' to configure this file" >&4
		echo "" >&4

		while read line
		do
		    set -- `echo "$line" | tr A-Z a-z`
		    test $# -gt 0 || { echo "" >&3; continue; }

		    case "$1" in
			directory|cache)
			    continue
			;;

			primary)
			    if [ "localhost" = "$2" -o  \
				"0.0.127.in-addr.arpa" = "$2" -o  \
				"127.in-addr.arpa" = "$2" ]
			    then
				echo "$line" >&4
			    else
				echo "$line" >&3
			    fi
			;;

			\;*|secondary)
			    echo "$line" >&3
			;;

			*)
			    echo "$line" >&4
			;;
		    esac
		done

		echo "" >&4
		echo ";; Custom configurations may appear below"  \
		    "(will be preserved)" >&4
	    ) <named.boot.old 3>boot.zones 4>boot.options

#	    mv -f named.boot.old named.boot.old

	    bindconfig --no-reload
	;;
    esac
elif [ -f boot.options ]
then
    if grep -qi '0\.0\.127\.in-addr\.arpa' boot.options
    then
	sed -e '/primary.*named\.rev-local/s/0\.0\.127/127/'  \
	    boot.options > boot.options.new
	mv -f boot.options.new boot.options
    fi

    echo -n "Examine or modify your existing BIND configuration? [N] "
    read yn
    test -n "$yn" || yn="N"

    case "$yn" in
	[Yy]*)
	    bindconfig --no-reload
	;;
    esac
else
    bindconfig --no-reload
fi

# Verify /etc/resolv.conf contains a local resolver

if [ -f /etc/resolv.conf ] &&  \
    ! grep -q 'nameserver.*127\.0\.0\.1' /etc/resolv.conf
then
    cat <<EOF

To be most effective, /etc/resolv.conf should list the IP address of your
local machine (127.0.0.1) as a nameserver. It currently does not.

EOF

    echo -n "Would you like this to be added? [Y] "
    read yn
    test -n "$yn" || yn="Y"

    case "$yn" in
	[Yy]*)
	    trap "rm -f /etc/resolv.conf.new" 0
	    exec 3>/etc/resolv.conf.new
	    echo "nameserver 127.0.0.1" >&3
	    cat /etc/resolv.conf >&3
	    exec 3>&-
	    mv -f /etc/resolv.conf.new /etc/resolv.conf
	    trap - 0
	;;
    esac
fi

# Update named.conf

if [ ! -f /etc/named.conf ]
then
    named-bootconf
fi

# Start server

echo -n "Start nameserver daemon now? [Y] "
read yn
test -n "$yn" || yn="Y"

case "$yn" in
    [Nn]*)
	echo "Not started; to start later, do: /etc/init.d/bind start"
	echo -n "Press [ENTER] "
	read line
    ;;

    *)
	/etc/init.d/bind start
    ;;
esac

exit 0
