#
# Script fragment to make dhclient3 work with resolvconf
#
# Licensed under the GNU GPL.  See /usr/share/common-licenses/GPL.
#

# Tips:
# * Be careful about changing the environment since this is sourced
# * This script fragment uses bash features

if [ -x /sbin/resolvconf ] ; then
	# Do stuff now
	case "$reason" in
		EXPIRE|FAIL|RELEASE|STOP)   # and TIMEOUT too ??
			# Delete resolv.conf info 
			[ ! "$interface" ] || /sbin/resolvconf -d "$interface"
			;;
	esac
	# For safety, undefine the nasty default make_resolv_conf()
	make_resolv_conf() {
		true
	}
	# Define a resolvconf-compatible m_r_c() function which gets run later
	# (or, in the TIMEOUT case, MAY get run later)
	case "$reason" in
		BOUND|RENEW|REBIND|REBOOT|TIMEOUT)
			make_resolv_conf() {
				R=""
				if [ "$new_domain_name_servers" ] && [ "$new_domain_name" ] ; then
					R="${R}domain $new_domain_name
"
				fi
				if [ "$new_domain_name_servers" ] && [ "$new_domain_search" ] ; then
					# The replacement below works around a bug with dhclient 3.1.0 which
					# separates items with the four characters backslash-zero-three-two
					# instead of with a space
					R="${R}search ${new_domain_search//\\032/ }
"
				fi
				for nameserver in $new_domain_name_servers ; do
					R="${R}nameserver $nameserver
"
				done
				[ ! "$interface" ] || echo -n "$R" | /sbin/resolvconf -a "$interface"
			}
			;;
	esac
fi
