#!/bin/sh
# resolvconf support by Mattias Guns, May 2006.

# Modified by chase 20090414, the dns work on resolv.conf
#if [ -n "$new_domain_name" -o -n "$new_domain_name_servers" ]; then
#    new_resolv_conf=/var/etc/resolv_ipv6.conf
#    rm -f $new_resolv_conf
#    if [ -n "$new_domain_name" ]; then
#        echo search $new_domain_name >> $new_resolv_conf
#    fi
#    if [ -n "$new_domain_name_servers" ]; then
#        for nameserver in $new_domain_name_servers; do
#            echo nameserver $nameserver >> $new_resolv_conf
#        done
#    fi
#fi

#exit 0

RESOLV_IPV4="/var/etc/resolv.conf"
RESOLV_IPV6="/var/etc/resolv_ipv6.conf"
RESOLV_CONF="/etc/resolv.conf"

if [ -n "$new_domain_name" -o -n "$new_domain_name_servers" ]; then
	rm -f $RESOLV_IPV6
	if [ -n "$new_domain_name" ]; then
		echo search $new_domain_name >> $RESOLV_IPV6
	fi
	if [ -n "$new_domain_name_servers" ]; then
		for nameserver in $new_domain_name_servers; do
			echo nameserver $nameserver >> $RESOLV_IPV6
		done
		cat $RESOLV_IPV4 $RESOLV_IPV6 > $RESOLV_CONF
	fi
fi

ipv6_autoconfig=$(nvram get ipv6_autoconfig)
if [ "$ipv6_autoconfig" = "ipv6_autoconfig = 1" ]; then

# waiting for rc is idle, to restart radvd and dhcp6s
	count=0
	while [ "$count" -lt 10 ]; do
		read rc_flag < /var/etc/rc_flag.conf
		if [ "$rc_flag" = "i" ]; then
			break
		elif [ "$rc_flag" = "b" ]; then
			count=$(($count+1))
			sleep 1
		else
			break
		fi
	done

	ipv6_dhcp_pd_enable=$(nvram get ipv6_dhcp_pd_enable)
	if [ "$ipv6_dhcp_pd_enable" = "ipv6_dhcp_pd_enable = 1" ]; then
# waiting for /var/tmp/dhcp_pd be generated, to restart radvd
		count=0
		while [ "$count" -lt 5 ]; do
			if [ -f /var/tmp/dhcp_pd ]; then
				break
			else
				count=$(($count+1))
				sleep 1
			fi
		done
	fi
# restart radvd
	killall -SIGQUIT timer

	sleep 1

	ipv6_autoconfig_type=$(nvram get ipv6_autoconfig_type)
	if [ "$ipv6_autoconfig_type" = "ipv6_autoconfig_type = stateful" ]; then
# restart dhcp6s
		killall -SIGABRT rc
	fi
fi
exit 0

