#!/bin/bash
# vim: noet
# Bash required because /usr/share/debconf/confmodule uses "local" (#252094)

set -e

. /usr/share/debconf/confmodule

MYNAME=resolvconf.postinst
DEFAULT_RUN_DIR=/var/run/resolvconf
report() { echo "${MYNAME}: $*" ; }
report_err() { report "Error: $*" >&2 ; }
report_warning() { report "Warning: $*" >&2 ; }
safe_mkdir() { [ -d "$1" ] || mkdir -v "$1"; }

### 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)
	# The Ubuntu Dapper boot sequence requires that resolvconf be
	# initialized before the root FS becomes writable and before
	# udev provides a writable /dev/shm.
	# Thus, we try to use /var/run as our run directory as hard
	# as possible.

	# 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
		# delete a symlink /etc/resolvconf/run -> /dev/shm which was
		# installed by previous versions of this package.
		elif [ "${RUN_CANONICALPATH}" != "${RUN_CANONICALPATH#/dev/shm}" ] ; then
			report_warning "Deleting obsolete /etc/resolvconf/run symlink to /dev/shm (inaccessible during bootup)"
			rm -f /etc/resolvconf/run
			# try to keep the resolver working until reboot.
			if [ ! -e "${DEFAULT_RUN_DIR}" ]; then
				ln -s "${RUN_CANONICALPATH}" "${DEFAULT_RUN_DIR}"
			fi
		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
		safe_mkdir /etc/resolvconf/run/interface
	else
		# It's a dangling but canonicalizable symlink
		# or it did not exist, or it was deleted by us.
		# Clean up.
		if [ ! -L /etc/resolvconf/run ]; then
			ln -s "${DEFAULT_RUN_DIR}" /etc/resolvconf/run \
			    || { report_err "Cannot create /etc/resolvconf/run symlink"; exit 1; }
			RUN_CANONICALPATH="${DEFAULT_RUN_DIR}"
		fi
		safe_mkdir "${RUN_CANONICALPATH}"
		safe_mkdir "${RUN_CANONICALPATH}/interface"
	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

