#!/bin/sh

#this script is use to setup ipv6 wan ip via dhcpv6

WAN_IF=$(uci get ipv6.wan.ifname)
WAN_LINK_ADDR=$(uci get ipv6.wan.link_addr)
WAN_MAC_ADDR=$(uci get ipv6.wan.mac_addr)
#DNS_FLAG=$(uci get ipv6.wan_dhcp.user_dhcp)
#WAN_PREFIX=$(uci get ipv6.wan_static.prefix)
#WAN_GATEWAY=$(uci get ipv6.wan_static.gateway)

WAN_DNS1=$(uci get ipv6.wan_dhcp.dns1)
WAN_DNS2=$(uci get ipv6.wan_dhcp.dns2)

killall dhcp6client

if [ ! "$WAN_IF" ]; then
	echo "Interface not set"
	exit 1
fi

rm -rf /etc/dhcp6/solicit.conf

echo "interface $WAN_IF                                        " > /etc/dhcp6/solicit.conf
echo "{                                                        " >> /etc/dhcp6/solicit.conf
echo "    duid_type 3                                          " >> /etc/dhcp6/solicit.conf
echo "    {                                                    " >> /etc/dhcp6/solicit.conf
echo "        hardware_type           6                        " >> /etc/dhcp6/solicit.conf
echo "        hardware_len            6                        " >> /etc/dhcp6/solicit.conf
echo "        hardware_addr           $WAN_MAC_ADDR            " >> /etc/dhcp6/solicit.conf
echo "    }                                                    " >> /etc/dhcp6/solicit.conf
echo "                                                         " >> /etc/dhcp6/solicit.conf
echo "    link_local_addr             $WAN_LINK_ADDR           " >> /etc/dhcp6/solicit.conf
echo "                                                         " >> /etc/dhcp6/solicit.conf
echo "    preferred_ipv6_address      2001::0323               " >> /etc/dhcp6/solicit.conf
echo "    preferred_lifetime          3600                     " >> /etc/dhcp6/solicit.conf
echo "    valid_lifetime              3600                     " >> /etc/dhcp6/solicit.conf
echo "}                                                        " >> /etc/dhcp6/solicit.conf

echo "dhcp6client $WAN_IF"
dhcp6client $WAN_IF &

echo "user set DNS"
if [ $WAN_DNS1 ]; then
	echo "nameserver $WAN_DNS1" >> /etc/resolv.conf
fi
if [ $WAN_DNS2 ]; then
	echo "nameserver $WAN_DNS2" >> /etc/resolv.conf
fi
