#!/bin/sh
set -e

. /usr/share/debconf/confmodule

is_active()
{
	[ -s "$1" ] \
	&& [ -x "$1" ] \
	&& ! { \
            [ "$(cat "$1" | wc --chars)" -lt 11 ] \
            && [ "$(cat "$1")" = '#!/bin/sh' ] ; \
	} \
	&& ! grep -q resolvconf "$1" > /dev/null
}

adequate_eni()
{
	# Assume that if _any_ dns-nameservers line appears in
	# /etc/network/interfaces then that is adequate and we
	# don't need to ask the user if he wants to link the
	# original file to the dynamic file at medium priority
	grep -q dns-nameservers /etc/network/interfaces > /dev/null
}

case $1 in
configure|reconfigure)
	NOTIFIED_ABOUT_BAD_PPPCONFIG_HOOK=no
	if is_active /etc/ppp/ip-up.d/0dns-up ; then
		db_input low resolvconf/bad-pppconfig-hook || :
		NOTIFIED_ABOUT_BAD_PPPCONFIG_HOOK=yes
	fi
	if is_active /etc/ppp/ip-down.d/0dns-down ; then
		if [ "$NOTIFIED_ABOUT_BAD_PPPCONFIG_HOOK" != yes ] ; then
			db_input low resolvconf/bad-pppconfig-hook || :
		fi
	fi
	if is_active /etc/ppp/ip-up.d/000usepeerdns ; then
		db_input low resolvconf/bad-pppoeconf-hook || :
	fi
	NOTIFIED_ABOUT_BAD_XISP_HOOK=no
	if is_active /etc/ppp/ip-up.d/0xisp-dns ; then
		db_input low resolvconf/bad-xisp-hook || :
		NOTIFIED_ABOUT_BAD_XISP_HOOK=yes
	fi
	if is_active /etc/ppp/ip-down.d/0xisp-dns ; then
		if [ "$NOTIFIED_ABOUT_BAD_XISP_HOOK" != yes ] ; then
			db_input low resolvconf/bad-xisp-hook || :
		fi
	fi
	db_input low resolvconf/disable-bad-hooks || :
	db_go || :

	db_input low resolvconf/linkify-resolvconf || :
	db_go || :
	db_get resolvconf/linkify-resolvconf
	if \
		[ "$RET" = "true" ] \
		&& [ ! -e /etc/resolvconf/resolv.conf.d/tail ] \
		&& [ ! -L /etc/resolvconf/resolv.conf.d/tail ]
	then
		if adequate_eni ; then
			db_input low resolvconf/link-tail-to-original || :
		else
			db_input medium resolvconf/link-tail-to-original || :
		fi
		db_go || :
	else
		db_set resolvconf/link-tail-to-original false
	fi

	db_input medium resolvconf/downup-interfaces || :
	db_go || :
	;;
esac

db_stop

