#!/bin/sh
set -e

# Source confmodule early because it re-runs this script
. /usr/share/debconf/confmodule

MYNAME=resolvconf.postinst
report() { echo "${MYNAME}: $*" ; }
report_err() { report "Error: $*" >&2 ; }

### Disable rogue hook scripts ###
# This should be kept in sync with the config script

db_get resolvconf/disable-bad-hooks
if [ "$RET" = "true" ] ; then
	PERMITTED_TO_DISABLE_BAD_HOOKS=yes
else
	PERMITTED_TO_DISABLE_BAD_HOOKS=""
fi

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

case "$1" in
configure|reconfigure)
	for F in /etc/ppp/ip-up.d/0dns-up \
	         /etc/ppp/ip-down.d/0dns-down \
	         /etc/ppp/ip-up.d/000usepeerdns \
	         /etc/ppp/ip-up.d/0xisp-dns \
	         /etc/ppp/ip-down.d/0xisp-dns
	do
		if is_active "$F" ; then
			if [ "$PERMITTED_TO_DISABLE_BAD_HOOKS" = yes ] ; then
				chmod ugo-x "$F"
			else
				report "Script $F must be disabled or deleted."
				report "It is recommended that you do: chmod ugo-x $F"
			fi
		fi
	done
	;;
# *)
	# In other modes we don't need to do anything
	# because we didn't take any action in the prerm
esac

### Create /etc/resolvconf/run ###
case "$1" in
configure|reconfigure)
	# Unfortunately we can't keep state files in /var/run/ because 
	# resolvconf, as it must, initializes before networking does and
	# /var/ can be on the network.
	#
	# Only use an existing symlink if it is canonicalizable
	if [ -L /etc/resolvconf/run ] ; then
		RUN_CANONICALPATH="$(readlink -f /etc/resolvconf/run || :)"
		if [ -z "$RUN_CANONICALPATH" ] ; then
			report_err "Deleting /etc/resolvconf/run symlink whose canonical path could not be determined"
			rm -f /etc/resolvconf/run
		fi
	fi
	# Delete it if it isn't a directory or a link to one
	if [ -e /etc/resolvconf/run ] && [ ! -d /etc/resolvconf/run ] ; then
		report_err "Deleting /etc/resolvconf/run which isn't a directory"
		rm -f /etc/resolvconf/run
	fi
	# Now /etc/resolvconf/run is either
	# * a directory, or
	# * a symlink to a directory, or
	# * a nonexistent (if you'll pardon the expression), or
	# * a dangling but canonicalizable symlink
	if [ -d /etc/resolvconf/run ] ; then
		# It's a directory or a symlink to one
		[ -d /etc/resolvconf/run/interface ] || mkdir -v /etc/resolvconf/run/interface
	elif [ -L /etc/resolvconf/run ] ; then
		# It's a dangling but canonicalizable symlink
		mkdir -v "$RUN_CANONICALPATH"
		mkdir -v "${RUN_CANONICALPATH}/interface"
	else
		# It's a nonexistent
		# Use /dev/shm if possible
		if \
			[ -d /dev/shm ] \
			&& [ -w /dev/shm ] \
			&& [ -r /proc/mounts ] \
			&& grep -qs "^tmpfs[[:space:]]\+/dev/shm[[:space:]]\+tmpfs[[:space:]]\+\([^[:space:]]\+,\)\?rw" /proc/mounts \
			&& ! grep -qs '[[:space:]]/dev[[:space:]]devfs[[:space:]]' /proc/mounts \
			&& { [ -d /dev/shm/resolvconf ] || mkdir -v /dev/shm/resolvconf ; } \
			&& { [ -d /dev/shm/resolvconf/interface ] || mkdir -v /dev/shm/resolvconf/interface ; }
		then
			ln -s /dev/shm/resolvconf /etc/resolvconf/run
		else
			mkdir -v /etc/resolvconf/run
			mkdir -v /etc/resolvconf/run/interface
		fi
	fi
	;;
# *)
	# In other modes we don't need to do anything
	# because we didn't take any action in the prerm
esac


case "$1" in
configure|reconfigure)
	# Deleting the old runlevel config is unavoidable
	# because there is currently no way in Debian
	# to update the config iff it is still the factory default
	update-rc.d -f resolvconf remove > /dev/null 2>&1
	;;
esac

# We use dh_installinit with --no-start
#DEBHELPER#

### Link tail to original if appropriate ###
TIME_TO_CHANGE_TAIL_LINK=no
case "$1" in
configure|reconfigure)
	TIME_TO_CHANGE_TAIL_LINK=yes
	;;
# *)
	# In other modes we don't need to do anything
	# because we didn't take any action in the prerm
esac

if [ "$TIME_TO_CHANGE_TAIL_LINK" = yes ] ; then
	if [ ! -e /etc/resolvconf/resolv.conf.d/tail ] ; then
		db_get resolvconf/link-tail-to-original
		if [ "$RET" = "true" ] ; then
			ln -nsf original /etc/resolvconf/resolv.conf.d/tail
		else
			: > /etc/resolvconf/resolv.conf.d/tail
		fi
	fi
fi

### Linkify /etc/resolv.conf if appropriate ###
TIME_TO_CHANGE_LINKIFICATION=no
case "$1" in
configure|reconfigure)
	TIME_TO_CHANGE_LINKIFICATION=yes
	;;
# *)
	# In other modes we don't need to do anything
	# because we didn't take any action in the prerm
esac

if [ "$TIME_TO_CHANGE_LINKIFICATION" = yes ] ; then
	db_get resolvconf/linkify-resolvconf
	if [ "$RET" = "true" ] ; then
		# Back up
		if \
			[ -f /etc/resolv.conf ] \
			&& { \
				[ ! -L /etc/resolv.conf ] \
				|| [ ! "$(readlink /etc/resolv.conf)" = "/etc/resolvconf/run/resolv.conf" ] ; \
			}
		then
			if [ ! -e /etc/resolvconf/resolv.conf.d/original ] ; then
				cp -a /etc/resolv.conf /etc/resolvconf/resolv.conf.d/original
			else
				cp -a /etc/resolv.conf /etc/resolv.conf.dpkg-old
			fi
			# Before creating the link, make sure that the original file is
			# at the target of the link.  /sbin/resolvconf will overwrite
			# this when it runs, of course.
			if [ ! -e /etc/resolvconf/run/resolv.conf ] ; then
				cp -a /etc/resolv.conf /etc/resolvconf/run/resolv.conf
			fi
		fi

		# Create the link
		ln -nsf /etc/resolvconf/run/resolv.conf /etc/resolv.conf

	fi
fi

### Enable updates ###
/etc/init.d/resolvconf enable-updates

db_stop

