#!/bin/sh

set -e

MYNAME=resolvconf.preinst
report() { echo "${MYNAME}: $*" ; }
report_warn() { report "Warning: $*" >&2 ; }
report_info() { report "$*" >&2 ; }

OLD_RUN_DIR=/lib/init/rw

standard_run_tmpfs_is_available() {
	[ -d /run ] \
	&& [ -w /run ] \
	&& [ -r /proc/mounts ] \
	&& grep -qs "^tmpfs[[:space:]]\+/run[[:space:]]\+tmpfs[[:space:]]\+\([^[:space:]]\+,\)\?rw" /proc/mounts
}

standard_run_subdirs_created() {
	{ [ -d /run/resolvconf ] || mkdir /run/resolvconf ; } \
	&& { [ -d /run/resolvconf/interface ] || mkdir /run/resolvconf/interface ; }
}

standard_old_run_tmpfs_is_available() {
	[ -d "$OLD_RUN_DIR" ] \
	&& [ -w "$OLD_RUN_DIR" ] \
	&& [ -r /proc/mounts ] \
	&& grep -qs "^tmpfs[[:space:]]\+${OLD_RUN_DIR}[[:space:]]\+tmpfs[[:space:]]\+\([^[:space:]]\+,\)\?rw" /proc/mounts
}

standard_old_run_subdirs_created() {
	{ [ -d "${OLD_RUN_DIR}/resolvconf" ] || mkdir "${OLD_RUN_DIR}/resolvconf" ; } \
	&& { [ -d "${OLD_RUN_DIR}/resolvconf/interface" ] || mkdir "${OLD_RUN_DIR}/resolvconf/interface" ; }
}

is_installed() {
	# Same function in preinst, postinst, postrm
	[ "$1" ] || return 1
	dpkg-query -W -f='${Status}\n' "$1" 2>/dev/null | grep -siq '^[[:alpha:]]\+ [[:alpha:]]\+ installed$' >/dev/null 2>&1
}


### Create run-time directories ###
#
# We create the run-time directories here, in the preinst, so that even if
# resolvconf is run before the postinst runs there is nevertheless a place
# for resolvconf to store data.  The latter can occur if resolvconf
# is installed simultaneously with a caching nameserver package whose
# postinst runs resolvconf to add its IP address.
#
case "$1" in
  install|upgrade)
	# Ensure that /etc/resolvconf exists.
	mkdir -p /etc/resolvconf

	if [ -L /etc/resolvconf/run ] ; then
		# Make sure that the symlink is canonicalizable.
		RUN_CANONICALPATH="$(readlink -f /etc/resolvconf/run || :)"
		if [ -z "$RUN_CANONICALPATH" ] ; then
			# It's not canonicalizable
			report_warn "Deleting old symlink /etc/resolvconf/run, the canonical path of whose target could not be determined"
			rm -f /etc/resolvconf/run
		fi
	fi

	# /etc/resolvconf/run is not a non-canonicalizable symlink.
	# Attempt migration to new standard location
	if [ -L /etc/resolvconf/run ] ; then
		# It's a canonicalizable symlink
		# If it's standard then try to migrate from old to new standard location.
		# Note that the new standard location may not yet be available.
		if
			[ "$RUN_CANONICALPATH" = "${OLD_RUN_DIR}/resolvconf" ] \
			&& standard_run_tmpfs_is_available \
			&& standard_run_subdirs_created
		then
			# /etc/resolvconf/run points to the old-standard location
			# and new-standard run directories are ready for use.
			# Switch from the old to the new standard location.
			F="$(echo "${OLD_RUN_DIR}/resolvconf/"*)"
			if [ "$F" ] && [ "$F" != "${OLD_RUN_DIR}/resolvconf/*" ] ; then
				if cp -a "${OLD_RUN_DIR}/resolvconf/"* /run/resolvconf ; then
					report_info "Migrated resolvconf run-time data from ${OLD_RUN_DIR}/resolvconf to /run/resolvconf"
				fi
			fi
			ln -nsf /run/resolvconf /etc/resolvconf/run
		fi
	fi

	# Delete /etc/resolvconf/run if it is neither a directory nor a link to one
	if [ -e /etc/resolvconf/run ] && [ ! -d /etc/resolvconf/run ] ; then
		report_warn "Deleting /etc/resolvconf/run which isn't a directory"
		rm -f /etc/resolvconf/run
	fi

	# OK, now /etc/resolvconf/run is either:
	# * nonexistent, or
	# * a dangling but canonicalizable symlink, or
	# * a symlink to a directory, or
	# * a directory.

	# Create subdirectory
	if [ -d /etc/resolvconf/run ] ; then
		# It's a directory or a symlink to one
		[ -d /etc/resolvconf/run/interface ] || mkdir /etc/resolvconf/run/interface
	elif [ -L /etc/resolvconf/run ] ; then
		# It's a dangling but canonicalizable symlink
		mkdir "$RUN_CANONICALPATH" "${RUN_CANONICALPATH}/interface"
	else
		# It's nonexistent.
		# Make directory at one of the standard locations if possible,
		# otherwise directly under /etc/resolvconf, and link to it
		if standard_run_tmpfs_is_available && standard_run_subdirs_created ; then
			ln -s /run/resolvconf /etc/resolvconf/run
		elif standard_old_run_tmpfs_is_available && standard_old_run_subdirs_created ; then
			ln -s "${OLD_RUN_DIR}/resolvconf" /etc/resolvconf/run
		else
			[ -d /etc/resolvconf/run/interface ] || mkdir -p /etc/resolvconf/run/interface
		fi
	fi
	;;
  # abort-upgrade)
	# Don't do anything because we don't anything in the postrm on upgrade or on failed-upgrade
	# ;;
esac


### Prepare to notify already configured packages of our installation ###

case "$1" in
  install)
	# Create list of packages that might need to be notified of the installation of resolvconf
	if [ -d /usr/lib/resolvconf/dpkg-event.d ] ; then
		NOTIFICATION_HOOK_SCRIPTS="$(cd /usr/lib/resolvconf/dpkg-event.d >/dev/null ; run-parts --test .)"
		PACKAGES_TO_NOTIFY=""
		for SCRPT in $NOTIFICATION_HOOK_SCRIPTS ; do
			PKG="${SCRPT#./}"
			if is_installed "$PKG" ; then
				PACKAGES_TO_NOTIFY="${PACKAGES_TO_NOTIFY:+$PACKAGES_TO_NOTIFY }$PKG"
			fi
		done
		rm -f /etc/resolvconf/run/packages-to-notify
		if [ "$PACKAGES_TO_NOTIFY" ] ; then
			echo "$PACKAGES_TO_NOTIFY" > /etc/resolvconf/run/packages-to-notify
		fi
	fi
	;;
  # upgrade)
	# Don't do anything
	# ;;
  # abort-upgrade)
	# Don't do anything because we don't anything in the postrm on upgrade or on failed-upgrade
	# ;;
esac

#DEBHELPER#

exit 0
