#!/bin/sh

set -e

case $reason in
    BOUND6|RENEW6|REBIND6)
	;;
    *)
	exit 0
	;;
esac

# update /etc/resolv.conf based on received values
make_resolv_conf() {
	local new_resolv_conf

	if [ -n "$new_dhcp6_domain_search" ] || [ -n "$new_dhcp6_name_servers" ]; then
		new_resolv_conf=/etc/resolv.conf.dhclient-new
		rm -f $new_resolv_conf

		if [ -n "$new_dhcp6_domain_search" ]; then
			echo "search $new_dhcp6_domain_search" >> $new_resolv_conf
		fi

		if [ -n "$new_dhcp6_name_servers" ]; then
			for nameserver in $new_dhcp6_name_servers; do
				echo "nameserver $nameserver" >> $new_resolv_conf
			done
		else # keep 'old' nameservers
			grep -i '^nameserver' /etc/resolv.conf >> $new_resolv_conf
		fi

		mv $new_resolv_conf /etc/resolv.conf
	fi
}

if [ "$new_ip6_address" ] && [ "$new_ip6_prefixlen" ]; then
	ip -6 addr add $new_ip6_address/$new_ip6_prefixlen \
		dev $interface scope global
fi

make_resolv_conf

i=0
for srv in $new_dhcp6_name_servers; do
	echo "nameserver[$i] $srv"
	i=$(($i+1))
done

i=0
for srv in $new_dhcp6_domain_search; do
	echo "Domain search list[$i] $srv"
	i=$(($i+1))
done

i=0
for srv in $new_dhcp6_sntp_servers; do
	echo "NTP server[$i] $srv"
	i=$(($i+1))
done

echo "end"
