#!/bin/sh /etc/rc.common 

dhcp6s_auto_config() {
	local tmp
	local script
	local iface
	local ip6addr
	local prelen
	local presec
	local uci_ipauto_s
	local uci_ipauto_e
	local new_prefix
	local uci_prefix_s
	local uci_prefix_e
	local preparts
	local lastpart
	local l
	local new_ip_s
	local new_ip_e
	local proto6
	local sla_wan
	local new_dns
	local mode

	#input parameters
	script=$0 #/sbin/hotplug-call, caller script
	iface=$1  #lan1

	#check is LAN
	tmp=`json get network.$iface.physical`
	[ "$tmp" == "lan" ] || return
	tmp=`uci get network.$iface.status`
	[ "$tmp" == "enable" ] || return

	#For debug: basic infomation
	#echo "----------------------------------------" > /dev/console
	#echo "!!->[dhcp6s_auto_cfg] script: $script, iface: $iface" > /dev/console

	#Get ipv6 address and prefix length
	tmp=`/usr/sbin/ip -6 addr show lan-$iface |grep global |sed -e "s/^.*inet6 //" -e "s/ scope.*\$//"`
	[ -z "$tmp" ] && return
	ip6addr=`echo ${tmp%%/*}`
	prelen=`echo ${tmp##*/}`
	presec=4
	[ "$prelen" ] && presec=`expr $prelen / 16`
	#echo "ip6addr: $ip6addr, prelen: $prelen, presec: $presec" > /dev/console

	#Get dhcp6s uci config
	uci_ipauto_s=`uci get dhcp6s.$iface.start_ip_auto`
	uci_ipauto_e=`uci get dhcp6s.$iface.end_ip_auto`
	#echo "uci_ipauto_s: $uci_ipauto_s, uci_ipauto_e: $uci_ipauto_e" > /dev/console

	#check if need to modify uci config
	new_prefix=`echo $ip6addr |cut -d: -f1-$presec`
	[ "$uci_ipauto_s" ] && uci_prefix_s=`echo $uci_ipauto_s |cut -d: -f1-$presec`
	[ "$uci_ipauto_e" ] && uci_prefix_e=`echo $uci_ipauto_e |cut -d: -f1-$presec`
	#echo "new_prefix: $new_prefix, uci_prefix: $uci_prefix_s & $uci_prefix_e" > /dev/console
	[ "$new_prefix" == "$uci_prefix_s" -a "$new_prefix" == "$uci_prefix_e" ] && return

	#modify uci.$iface start_ip_auto, end_ip_auto
	tmp=8
	while [ -z "$lastpart" ]; do
		lastpart=`echo $ip6addr |cut -d: -f$tmp`
		tmp=`expr $tmp - 1`
	done
	preparts=`echo $ip6addr |cut -d: -f1-$tmp`
	#echo "preparts: $preparts, lastpart: $lastpart" > /dev/console
	tmp=`${#lastpart}`
	l=`echo $lastpart |cut -c 1`
	if [ $tmp -ne 4 -o "$l" == "1" -o "$l" == "2" -o "$l" == "3" ]; then
		new_ip_s=${preparts}:9000
		new_ip_e=${preparts}:9f00
	elif [ "$l" == "4" -o "$l" == "5" -o "$l" == "6" -o "$l" == "7" ]; then
		new_ip_s=${preparts}:b000
		new_ip_e=${preparts}:bf00
	elif [ "$l" == "8" -o "$l" == "9" -o "$l" == "a" -o "$1" == "b" ]; then
		new_ip_s=${preparts}:2000
		new_ip_e=${preparts}:2f00
	else
		new_ip_s=${preparts}:7000
		new_ip_e=${preparts}:7f00
	fi
	#echo "new_ip_s: $new_ip_s, new_ip_e: $new_ip_e" > /dev/console
	uci set dhcp6s.$iface.start_ip_auto=$new_ip_s
	uci set dhcp6s.$iface.end_ip_auto=$new_ip_e

	#modify uci.$iface.dns_auto
	proto6=`uci get network.$iface.proto6`
	if [ "$proto6" == "dhcp-sla" ]; then
		sla_wan=`uci get network.$iface.dhcp6_sla_wan`
		[ "$sla_wan" ] && new_dns=`json get network.$sla_wan.dns6`
	elif [ "$proto6" == "static" ]; then
		new_dns=`json get network.$iface.dns6`
	fi
	#echo "$iface proto6: $proto6, new dns: $new_dns" > /dev/console
	uci set dhcp6s.$iface.dns_auto=""
	for l in $new_dns; do
		uci add_list dhcp6s.$iface.dns_auto=$l
	done
	uci commit dhcp6s

	#stop dhcp6s and restart in file: 20-ipv6_autoconf
	mode=`uci get dhcp6s.$iface.mode`
	[ -z "$mode" ] && mode="manual" #not set this option, must be old version: manual
	#echo "$iface dhcp6s mode: $mode" > /dev/console
	[ "$mode" == "auto" -a -e /var/run/dhcp6s-$iface.pid ] && {
		#echo "restart $iface dhcp6s service" > /dev/console
		pid=`cat /var/run/dhcp6s-$iface.pid`
		kill $pid
	}

	#For debug: check function end
	#echo "!!->[dhcp6s_auto_cfg] function end check" > /dev/console
	#echo "----------------------------------------" > /dev/console
}
