#!/bin/sh
#
# resolvconf        Update resolver configuration
#
# Licensed under the GNU GPL.  See /usr/share/common-licenses/GPL.
#
# History
# June-Sept 2003: Written by Thomas Hood <jdthood@yahoo.co.uk>

set -e

MYNAME="${0##*/}"
PATH=/bin:/sbin
RUNDIR=/etc/resolvconf/run
IFACEDIR="${RUNDIR}/interface"
RESOLVCONFFILE="${RUNDIR}/resolv.conf"
DISABLE_UPDATES_FLAGFILE="${RUNDIR}/disable-updates"

report_err()
{
	echo "${MYNAME}: Error: $*" >&2
}

update()
{
	[ -e $DISABLE_UPDATES_FLAGFILE ] && return
	cd "$IFACEDIR"
	# "update" scripts must assume that interface files are in PWD
	run-parts ${1:+--arg="$1"} ${2:+--arg="$2"} /etc/resolvconf/update.d
}

check_dirs()
{
	if [ ! -d "$RUNDIR" ] ; then
		report_err "$RUNDIR is not a directory, as it should be.  Exiting."
		exit 1
	fi
	if [ ! -d "$IFACEDIR" ] ; then
		report_err "$IFACEDIR is not a directory, as it should be.  Exiting."
		exit 1
	fi
}

case "$1" in
start)
	# The "start" argument is used only at boot time.
	# On package upgrade, don't run this.
	# If you want to update resolv.conf, use "reload".
	check_dirs
	echo -n "Starting resolvconf..."
	rm -f "$IFACEDIR"/*
	rm -f "$DISABLE_UPDATES_FLAGFILE"
	update -i
	echo "done."
	;;
stop)
	# The "stop" argument is used only at shutdown time.
	echo -n "Stopping resolvconf..."
	: >| "$DISABLE_UPDATES_FLAGFILE"
	echo "done."
	;;
reload|force-reload)
	# Silently
	check_dirs
	update
	;;
restart)
	echo -n "Restarting resolvconf..."
	$0 reload
	echo "done."
	;;
*)
	echo "Usage: /etc/init.d/resolvconf {start|stop|reload|restart|force-reload}" >&2
	exit 3
	;;
esac

