#!/bin/sh


set -e


#-------------------------------------------------------------------------


install_conf()
{
	echo "Installing configuration file: $1"
	[ -s "$1" -a ! -f "$1.BACKUP" ] && cp -f "$1" "$1.BACKUP"
	[ -f "$1.yandex" ] && cp -f "$1.yandex" "$1"
}


#-------------------------------------------------------------------------


post_install()
{
	RC="/etc/resolv.conf"
	chattr -i $RC
	awk '!/nameserver /
		END {
			print   "nameserver ::1\n" \
				"nameserver 2a02:6b8:0:3400::1\n" \
				"nameserver 2a02:6b8::1:1\n" \
		}' $RC > ${RC}.bak
	mv -v ${RC}.bak $RC
	chattr +i $RC
	echo "Fixed $RC"
	cat $RC

	install_conf /etc/bind/named.conf.options

	service bind9 stop
	if which sysv-rc-conf > /dev/null 2>&1 ; then
		sysv-rc-conf bind9 on || true
	else
		update-rc.d bind9 defaults || true
	fi
	service bind9 start
}


#-------------------------------------------------------------------------


case "$1" in
	configure)
		post_install || true
	;;

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

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

#DEBHELPER#

exit 0
