#!/bin/sh /etc/rc.common
#add procd support by wanghao
#sync configs from network_sync when procd reload service.

START=25
STOP=90

USE_PROCD=1

NETWORK_MOD_ID=290
LOGX_ID_NETIFD_START=200
LOGX_ID_NETIFD_STOP=201

. /lib/functions.sh
. /usr/lib/eth-encap/eth_encap.sh

delete_br_wan_device() {
	local sec_name="$1"
	local id=""
	id="$(uci get network.vlan.id)"
	config_get name "$1" name
	if [ "$name" == "br-wan.""$id" ];then
		uci delete network."$sec_name"
	fi
}

delete_options() {
    local section="$1"

    local options=`uci show "$section" | grep -v "$section=" | awk -F = '{print $1}'`
    for op_r in $options ; do 
        uci set "$op_r"=""
    done
}

parent_sync()
{
	local wan_type=$1
	local parent=
	if [ -n "$wan_type" ]; then
		case "$wan_type" in
		dhcp|static)
			parent="wan"
			;;

		pppoe|l2tp|pptp|pppoeshare)
			parent="internet"
			;;

		dslite|v6plus)
			parent="wanv6"
			;;

		*)
			;;
		esac
	fi

	echo "$parent"
	return
}

delete_static_routing(){
	[ -z "$1" ] && return 0
	local sec_name="$1"
	uci delete network."$sec_name"
}

update_device_mtu(){
	[ -z "$1" ] && return 0
	local sec_name="$1"
	local data=""
	local name=""

	data="$(uci get network_sync.wan.ifname)"
	config_get name  "$sec_name" name ""

	if [ -n "$name" -a "$name" == "$data" ] ;then
		data="$(uci get network_sync.wan.mtu)"
		uci set network."$sec_name".mtu="$data"
	fi
}

set_static_routing(){
	[ -z "$1" ] && return 0
	local sec_name="$1"
	local route
	local role=$(uci get bind_device_list.$(getfirm DEV_ID).role)

	local enable
	local name
	local key
	local netmask
	local interface
	local target
	local gateway

	config_get enable    "$sec_name" enable "1"
	config_get name      "$sec_name" name ""
	config_get key       "$sec_name" key ""
	config_get netmask   "$sec_name" netmask ""
	config_get interface "$sec_name" interface ""
	config_get target    "$sec_name" target ""
	config_get gateway   "$sec_name" gateway ""

	if [ "$role" = "RE" ];then
		if [ "$interface" = "lan" ];then
			route=$(uci add network route)
			if [ -n "$route" ] ;then
				uci set network."$route".enable="$enable"
				uci set network."$route".name="$name"
				uci set network."$route".key="$key"
				uci set network."$route".netmask="$netmask"
				uci set network."$route".interface="$interface"
				uci set network."$route".target="$target"
				uci set network."$route".gateway="$gateway"
				uci set network."$route".route_pre="1"
			fi
		fi
	else
		route=$(uci add network route)
		if [ -n "$route" ] ;then
			uci set network."$route".enable="$enable"
			uci set network."$route".name="$name"
			uci set network."$route".key="$key"
			uci set network."$route".netmask="$netmask"
			uci set network."$route".interface="$interface"
			uci set network."$route".target="$target"
			uci set network."$route".gateway="$gateway"
			uci set network."$route".route_pre="1"
		fi
	fi
}

get_hostname(){
	local data=$(uci get isp_info.info.use_isp_hostname -c /etc/profile.d -q)
	if [ $data = 1 ]; then
		data=$(uci get isp_info.info.ips_hostname_suffix -c /etc/profile.d)
		echo "deco_$(getfirm MODEL)"${data}
	else
		echo "deco_$(getfirm MODEL)"
	fi
}

load_config() {
#copy some configs from network_sync to network
	local wan_type=""
	local data=""
	local para_enable=""
	local mode=""
	local vlan_status=""
	local macaddr=""
	wan_type="$(uci get network_sync.wan.wan_type)"
	#store wan mac address
	macaddr="$(uci get network.wan.macaddr)"

	local carrier_network=""
	local carrier_sync=""
	[ "$wan_type" == "v6_plus" ] && {
		carrier_network="$(uci get network.wan.carrier)"
		carrier_sync="$(uci get network_sync.wan.carrier)"
	}

#if device br-wan.x existed in network_sync, then update it in network
	config_load network
	data="$(uci get network_sync.@device[0].enabled)"
	config_foreach delete_br_wan_device device
	if [ "$data" == "1" ];then
		local sec=""
		sec="$(uci add network device)"
		uci set network."$sec".enabled="1"
		data="$(uci get network_sync.@device[0].name)"
		uci set network."$sec".name="$data"
		uci set network."$sec".macaddr="$macaddr"
	fi

	config_foreach delete_static_routing route
	config_foreach update_device_mtu device
#if role == FAP and device br-wan.x not existed in network, then add it
	#mode=$(grep 'RE' /tmp/work_mode)
#	device_id=$(getfirm DEV_ID)
#	role=$(uci get bind_device_list."$device_id".role)
#	vlan_status=$(uci get network.vlan.enable)
#	if [ "$role" == "AP" ]; then
#		[ "$vlan_status" == "1" ] && {
#			config_load network
#			local id=""
#			id="$(uci get network_sync.vlan.id)"
#			config_foreach delete_br_wan_device device
#			local sec=""
#			sec="$(uci add network device)"
#			uci set network."$sec".enabled="1"
#			uci set network."$sec".name="br-wan.""$id"
#			uci set network."$sec".macaddr="$macaddr"
#		}
#	fi

#copy vlan info from network_sync to network  --add by linxijin
	data="$(uci get network_sync.vlan.enable)"
	uci set network.vlan.enable="$data"

	data="$(uci get network_sync.vlan.isp_name)"
	uci set network.vlan.isp_name="$data"

	data="$(uci get network_sync.vlan.id)"
	uci set network.vlan.id="$data"

	data="$(uci get network_sync.vlan.tag_802_1q)"
	uci set network.vlan.tag_802_1q="$data"

	data="$(uci get network_sync.vlan.priority)"
	uci set network.vlan.priority="$data"

#if vlan.enable=0 and wan.ifname != "br-wan", then reset it to "br-wan"! It happen in change sysmode.
#vlan_status=$(uci get network_sync.vlan.enable)
#wanif=$(uci get network_sync.wan.ifname)
#[ "$vlan_status" == "0" -a "$wanif" != "br-wan" ] && {
#	uci set network_sync.wan.ifname="br-wan"
#	uci commit
#}
	sysmode=$(uci get sysmode.sysmode.mode)
	vlan_enable=$(uci get network.vlan.enable)
	vid=$(uci get network.vlan.id)
	# if Router->AP, then reset vlan.enable=0 if enabled vlan
	[ "$sysmode" == "AP" ] && {
		[ "$vlan_enable" == "1" ] && {

			all_eth_ifname=$(get_all_eth_names)
			all_eth_ifname=$(echo $all_eth_ifname|sed 's/^[ \t]*$//g')

			uci set network.vlan.enable=0
			uci set network_sync.vlan.enable=0
			lanif=$(uci get network.lan.ifname)
			[ "$lanif" != "$all_eth_ifname" ] && {
				uci set network.lan.ifname="$all_eth_ifname"
			}
			uci commit network
			uci commit network_sync
		}
	}
	# if AP->Router, then set vlan.enable=1 if enabled vlan
	[ "$sysmode" == "Router" ] && {
		[ "$vlan_enable" == "0" -a "$vid" != "0" ] && {
			uci set network.vlan.enable=1
			uci set network_sync.vlan.enable=1
			uci commit network
			uci commit network_sync
		}
	}




#delete wan and internet section
	#uci delete network.wan
    delete_options "network.wan"

    if [ "$wan_type" = "pppoe" ] || [ "$wan_type" = "pptp" ] || [ "$wan_type" = "l2tp" ] || [ "$wan_type" = "pppoeshare" ]; then
        delete_options "network.internet"
    else
        uci delete network.internet
    fi

#add wan section
	uci set network.wan=interface

#copy mac_clone info from network_sync to network
	data="$(uci get network_sync.mac_clone.clone_enable)"
	if [ "$data" == '1' ]; then
		new_macaddr="$(uci get network_sync.mac_clone.macaddr)"
	else
		new_macaddr="$(network_get_firm wan)"
		#new_macaddr="$(echo "$new_macaddr" | tr 'a-z' 'A-Z')"
		new_macaddr="$(echo ${new_macaddr//-/:})"
	fi
	uci set network.wan.macaddr="$new_macaddr"
	if [ "$new_macaddr" != "$macaddr" ]; then
		data="$(uci get network_sync.wan.ifname)"
		uci set network.wan.ifname="$data"
		uci commit network

        if [ "$wan_type" = "static" ]; then
            ubus call network.interface.internet disconnect
            ubus call network.interface.wanv6 disconnect
            ubus call network.interface.wan remove
            sleep 1
            ifmac
        else
            ubus call network.interface.internet disconnect
            ubus call network.interface.wan disconnect
            ubus call network.interface.wanv6 disconnect
            sleep 1
            ifmac
        fi
        #ubus call network.interface.wan connect
        #ubus call network.interface.internet connect
	fi
	
	#v6plus and ocn use same wan_type 'v6_plus', but different 'carrier'
	#so when carrier changed, the dialing type has changed
	[ "$wan_type" == "v6_plus" ] && {
		[ -n "$carrier_network" ] && [ "$carrier_network" != "$carrier_sync" ] && {
			ubus call network.interface.wan disconnect
			ubus call network.interface.wanv6 disconnect
		}
	}

	# interface vpn
	config_load vpn
	config_get vpn_enabled "client" "enabled"
	config_get vpntype "client" "vpntype"

	if [ -z "$vpn_enabled" ] || [ -z "$vpntype" ]; then
		# vpn client is not supported
		:
	elif [ "$vpn_enabled" = "off" ] || [ "$vpntype" = "none" ]; then
		echo "vpn client is off" > /dev/console
	else
		data="$(parent_sync "$wan_type")"
		[ -n "$date" ] && {
			uci set network_sync.vpn.parent="$data"
		}
		uci delete network.vpn
		uci set network.vpn=interface
		case "$vpntype" in
		pptpvpn)
		#update pptpvpn section values
			data="$(uci get network_sync.vpn.proto)"
			uci set network.vpn.proto="$data"

			data="$(uci get network_sync.vpn.auto)"
			uci set network.vpn.auto="$data"

			data="$(uci get network_sync.vpn.encryption)"
			uci set network.vpn.encryption="$data"

			data="$(uci get network_sync.vpn.parent)"
			uci set network.vpn.parent="$data"

			data="$(uci get network_sync.vpn.ifname)"
			uci set network.vpn.ifname="$data"

			data="$(uci get network_sync.vpn.password)"
			uci set network.vpn.password="$data"

			data="$(uci get network_sync.vpn.server)"
			uci set network.vpn.server="$data"

			data="$(uci get network_sync.vpn.connectable)"
			uci set network.vpn.connectable="$data"

			data="$(uci get network_sync.vpn.mru)"
			uci set network.vpn.mru="$data"

			data="$(uci get network_sync.vpn.username)"
			uci set network.vpn.username="$data"

			data="$(uci get network_sync.vpn.wan_type)"
			uci set network.vpn.wan_type="$data"

			data="$(uci get network_sync.vpn.name)"
			uci set network.vpn.name="$data"
			;;

		l2tpvpn)
		#update l2tpvpn section values
			data="$(uci get network_sync.vpn.proto)"
			uci set network.vpn.proto="$data"

			data="$(uci get network_sync.vpn.auto)"
			uci set network.vpn.auto="$data"

			data="$(uci get network_sync.vpn.psk)"
			uci set network.vpn.psk="$data"

			data="$(uci get network_sync.vpn.parent)"
			uci set network.vpn.parent="$data"

			data="$(uci get network_sync.vpn.ifname)"
			uci set network.vpn.ifname="$data"

			data="$(uci get network_sync.vpn.password)"
			uci set network.vpn.password="$data"

			data="$(uci get network_sync.vpn.server)"
			uci set network.vpn.server="$data"

			data="$(uci get network_sync.vpn.connectable)"
			uci set network.vpn.connectable="$data"

			data="$(uci get network_sync.vpn.mru)"
			uci set network.vpn.mru="$data"

			data="$(uci get network_sync.vpn.username)"
			uci set network.vpn.username="$data"

			data="$(uci get network_sync.vpn.wan_type)"
			uci set network.vpn.wan_type="$data"

			data="$(uci get network_sync.vpn.name)"
			uci set network.vpn.name="$data"

			;;
		openvpn)
		#update openvpn section values
			data="$(uci get network_sync.vpn.proto)"
			uci set network.vpn.proto="$data"

			data="$(uci get network_sync.vpn.auto)"
			uci set network.vpn.auto="$data"

			data="$(uci get network_sync.vpn.psk)"
			uci set network.vpn.psk="$data"

			data="$(uci get network_sync.vpn.parent)"
			uci set network.vpn.parent="$data"

			data="$(uci get network_sync.vpn.ifname)"
			uci set network.vpn.ifname="$data"

			data="$(uci get network_sync.vpn.password)"
			uci set network.vpn.password="$data"

			data="$(uci get network_sync.vpn.connectable)"
			uci set network.vpn.connectable="$data"

			data="$(uci get network_sync.vpn.mtu)"
			uci set network.vpn.mru="$data"

			data="$(uci get network_sync.vpn.username)"
			uci set network.vpn.username="$data"

			data="$(uci get network_sync.vpn.name)"
			uci set network.vpn.name="$data"
			;;
		wireguardvpn)
		#update wireguardvpn section values
			data="$(uci get network_sync.vpn.proto)"
			uci set network.vpn.proto="$data"

			data="$(uci get network_sync.vpn.auto)"
			uci set network.vpn.auto="$data"

			data="$(uci get network_sync.vpn.preshared_key)"
			uci set network.vpn.preshared_key="$data"

			data="$(uci get network_sync.vpn.parent)"
			uci set network.vpn.parent="$data"

			data="$(uci get network_sync.vpn.ifname)"
			uci set network.vpn.ifname="$data"

			data="$(uci get network_sync.vpn.name)"
			uci set network.vpn.name="$data"

			data="$(uci get network_sync.vpn.allowed_ips)"
			uci set network.vpn.allowed_ips="$data"

			data="$(uci get network_sync.vpn.nat)"
			uci set network.vpn.nat="$data"

			data="$(uci get network_sync.vpn.endpoint_port)"
			uci set network.vpn.endpoint_port="$data"

			data="$(uci get network_sync.vpn.private_key)"
			uci set network.vpn.private_key="$data"

			data="$(uci get network_sync.vpn.endpoint_address)"
			uci set network.vpn.endpoint_address="$data"

			data="$(uci get network_sync.vpn.public_key)"
			uci set network.vpn.public_key="$data"

			data="$(uci get network_sync.vpn.persistent_keepalive)"
			uci set network.vpn.persistent_keepalive="$data"

			data="$(uci get network_sync.vpn.address)"
			uci set network.vpn.address="$data"

			data="$(uci get network_sync.vpn.wg_dns)"
			uci set network.vpn.wg_dns="$data"

			data="$(uci get network_sync.vpn.wg_dns_bk)"
			uci set network.vpn.wg_dns_bk="$data"

			data="$(uci get network_sync.vpn.connectable)"
			uci set network.vpn.connectable="$data"

			data="$(uci get network_sync.vpn.mtu)"
			uci set network.vpn.mtu="$data"

			;;
		*)
			echo "unsupported vpn type..." > /dev/console
			;;
		esac
	fi

	uci set network.wan.wan_type="$wan_type"
	local role=$(uci get bind_device_list.$(getfirm DEV_ID).role)
	local mode=$(uci get sysmode.sysmode.mode)

	case "$wan_type" in
		dhcp)
		#update values
			data="$(uci get network_sync.wan.proto)"
			uci set network.wan.proto="$data"

			data="$(uci get network_sync.wan.ifname)"
			uci set network.wan.ifname="$data"

			data="$(uci get network_sync.wan.connectable)"
			uci set network.wan.connectable="$data"

			data="$(uci get network_sync.wan.keepup)"
			uci set network.wan.keepup="$data"

			data="$(uci get network_sync.wan.auto)"
			uci set network.wan.auto="$data"

			data="$(uci get network_sync.wan.mtu)"
			uci set network.wan.mtu="$data"

			data="$(uci get network_sync.wan.peerdns)"
			uci set network.wan.peerdns="$data"

			data="$(uci get network_sync.wan.dns)"
			[ "0.0.0.0" != "$data" ] && {
				uci set network.wan.dns="$data"
			}

			# data="$(uci get network_sync.wan.hostname)"
			uci set network.wan.hostname=$(get_hostname)

			data="$(uci get network_sync.wan.conn_mode)"
			uci set network.wan.conn_mode="$data"

			data="$(uci get network_sync.wan.broadcast)"
			uci set network.wan.broadcast="$data"
			;;
		pppoe)
		#update wan section values
			data="$(uci get network_sync.wan.proto)"
			uci set network.wan.proto="$data"

			data="$(uci get network_sync.wan.ifname)"
			uci set network.wan.ifname="$data"

			data="$(uci get network_sync.wan.connectable)"
			uci set network.wan.connectable="$data"

			data="$(uci get network_sync.wan.keepup)"
			uci set network.wan.keepup="$data"

			data="$(uci get network_sync.wan.auto)"
			uci set network.wan.auto="$data"

			data="$(uci get network_sync.wan.mtu)"
			uci set network.wan.mtu="$data"

		#add internet section
			uci set network.internet=interface
		#update internet section values
			data="$(uci get network_sync.internet.keepalive)"
			uci set network.internet.keepalive="$data"

			data="$(uci get network_sync.internet.proto)"
			uci set network.internet.proto="$data"

			data="$(uci get network_sync.internet.parent)"
			uci set network.internet.parent="$data"

			data="$(uci get network_sync.internet.conn_mode)"
			uci set network.internet.conn_mode="$data"

			data="$(uci get network_sync.internet.dns_mode)"
			uci set network.internet.dns_mode="$data"

			data="$(uci get network_sync.internet.ip_mode)"
			uci set network.internet.ip_mode="$data"

			data="$(uci get network_sync.internet.username)"
			uci set network.internet.username="$data"

			data="$(uci get network_sync.internet.auto)"
			uci set network.internet.auto="$data"

			data="$(uci get network_sync.internet.dns)"
			uci set network.internet.dns="$data"

			data="$(uci get network_sync.internet.connectable)"
			uci set network.internet.connectable="$data"

			data="$(uci get network_sync.internet.mru)"
			uci set network.internet.mru="$data"

			data="$(uci get network_sync.internet.password)"
			uci set network.internet.password="$data"
 
			data="$(uci get network_sync.internet.ifname)"
			uci set network.internet.ifname="$data"	

			para_enable="$(uci get protocol.pppoe.svr_enable)"
			data="$(uci get protocol.pppoe.server)"
			if [ "$para_enable" = "1" ]; then
				uci set network.internet.service="$data"
			else
				uci set network.internet.service=""
			fi

			para_enable="$(uci get protocol.pppoe.ac_enable)"
			data="$(uci get protocol.pppoe.access)"
			if [ "$para_enable" = "1" ]; then
				uci set network.internet.ac="$data"
			else
				uci set network.internet.ac=""
			fi
			;;
		pptp)
		#update wan section values
			data="$(uci get network_sync.wan.proto)"
			uci set network.wan.proto="$data"
			
			[ "$data" == "dhcp" ] && {
					local broadcast="$(uci get network_sync.wan.broadcast)"
					uci set network.wan.broadcast="$broadcast"
				}

			[ "$data" == "static" ] && {
                                [ $role == "AP" -a "$mode" == "Router" ] && { 
					data="$(uci get network_sync.wan.ipaddr)"
					uci set network.wan.ipaddr="$data"
				}

				data="$(uci get network_sync.wan.netmask)"
				uci set network.wan.netmask="$data"

				data="$(uci get network_sync.wan.gateway)"
				uci set network.wan.gateway="$data"

				data="$(uci get network_sync.wan.dns)"
				uci set network.wan.dns="$data"
			}


			data="$(uci get network_sync.wan.ifname)"
			uci set network.wan.ifname="$data"

			data="$(uci get network_sync.wan.connectable)"
			uci set network.wan.connectable="$data"

			data="$(uci get network_sync.wan.keepup)"
			uci set network.wan.keepup="$data"

			data="$(uci get network_sync.wan.auto)"
			uci set network.wan.auto="$data"

			data="$(uci get network_sync.wan.mtu)"
			uci set network.wan.mtu="$data"

		#add internet section
			uci set network.internet=interface
		#update internet section values
			data="$(uci get network_sync.internet.keepalive)"
			uci set network.internet.keepalive="$data"

			data="$(uci get network_sync.internet.proto)"
			uci set network.internet.proto="$data"

			data="$(uci get network_sync.internet.parent)"
			uci set network.internet.parent="$data"

			data="$(uci get network_sync.internet.conn_mode)"
			uci set network.internet.conn_mode="$data"

			data="$(uci get network_sync.internet.dns_mode)"
			uci set network.internet.dns_mode="$data"

			data="$(uci get network_sync.internet.ip_mode)"
			uci set network.internet.ip_mode="$data"

			data="$(uci get network_sync.internet.username)"
			uci set network.internet.username="$data"

			data="$(uci get network_sync.internet.auto)"
			uci set network.internet.auto="$data"

			data="$(uci get network_sync.internet.dns)"
			uci set network.internet.dns="$data"

			data="$(uci get network_sync.internet.connectable)"
			uci set network.internet.connectable="$data"

			data="$(uci get network_sync.internet.mru)"
			uci set network.internet.mru="$data"

			data="$(uci get network_sync.internet.password)"
			uci set network.internet.password="$data"
 
			data="$(uci get network_sync.internet.ifname)"
			uci set network.internet.ifname="$data"	

			data="$(uci get network_sync.internet.server)"
			uci set network.internet.server="$data"	

			data="$(uci get network_sync.internet.wan_type)"
			uci set network.internet.wan_type="$data"	
			;;
		l2tp)
		#update wan section values
			data="$(uci get network_sync.wan.proto)"
			uci set network.wan.proto="$data"
			
			[ "$data" == "dhcp" ] && {
					local broadcast="$(uci get network_sync.wan.broadcast)"
					uci set network.wan.broadcast="$broadcast"
				}

			[ "$data" == "static" ] && {
				[ $role == "AP" -a "$mode" == "Router" ] && { 
					data="$(uci get network_sync.wan.ipaddr)"
					uci set network.wan.ipaddr="$data"
				}

				data="$(uci get network_sync.wan.netmask)"
				uci set network.wan.netmask="$data"

				data="$(uci get network_sync.wan.gateway)"
				uci set network.wan.gateway="$data"

				data="$(uci get network_sync.wan.dns)"
				uci set network.wan.dns="$data"
			}

			data="$(uci get network_sync.wan.ifname)"
			uci set network.wan.ifname="$data"

			data="$(uci get network_sync.wan.connectable)"
			uci set network.wan.connectable="$data"

			data="$(uci get network_sync.wan.keepup)"
			uci set network.wan.keepup="$data"

			data="$(uci get network_sync.wan.auto)"
			uci set network.wan.auto="$data"

			data="$(uci get network_sync.wan.mtu)"
			uci set network.wan.mtu="$data"

		#add internet section
			uci set network.internet=interface
		#update internet section values
			data="$(uci get network_sync.internet.keepalive)"
			uci set network.internet.keepalive="$data"

			data="$(uci get network_sync.internet.proto)"
			uci set network.internet.proto="$data"

			data="$(uci get network_sync.internet.parent)"
			uci set network.internet.parent="$data"

			data="$(uci get network_sync.internet.conn_mode)"
			uci set network.internet.conn_mode="$data"

			data="$(uci get network_sync.internet.dns_mode)"
			uci set network.internet.dns_mode="$data"

			data="$(uci get network_sync.internet.ip_mode)"
			uci set network.internet.ip_mode="$data"

			data="$(uci get network_sync.internet.username)"
			uci set network.internet.username="$data"

			data="$(uci get network_sync.internet.auto)"
			uci set network.internet.auto="$data"

			data="$(uci get network_sync.internet.dns)"
			uci set network.internet.dns="$data"

			data="$(uci get network_sync.internet.connectable)"
			uci set network.internet.connectable="$data"

			data="$(uci get network_sync.internet.mru)"
			uci set network.internet.mru="$data"

			data="$(uci get network_sync.internet.password)"
			uci set network.internet.password="$data"
 
			data="$(uci get network_sync.internet.ifname)"
			uci set network.internet.ifname="$data"	

			data="$(uci get network_sync.internet.server)"
			uci set network.internet.server="$data"	

			data="$(uci get network_sync.internet.wan_type)"
			uci set network.internet.wan_type="$data"	
			;;
		static)
		#update values
			data="$(uci get network_sync.wan.proto)"
			uci set network.wan.proto="$data"

			data="$(uci get network_sync.wan.auto)"
			uci set network.wan.auto="$data"

			data="$(uci get network_sync.wan.mtu)"
			uci set network.wan.mtu="$data"

			data="$(uci get network_sync.wan.netmask)"
			uci set network.wan.netmask="$data"

			data="$(uci get network_sync.wan.conn_mode)"
			uci set network.wan.conn_mode="$data"

			data="$(uci get network_sync.wan.connectable)"
			uci set network.wan.connectable="$data"

			data="$(uci get network_sync.wan.dns)"
			uci set network.wan.dns="$data"

			data="$(uci get network_sync.wan.gateway)"
			uci set network.wan.gateway="$data"

			[ $role == "AP" -a "$mode" == "Router" ] && { 
				data="$(uci get network_sync.wan.ipaddr)"
				[ $data ] && { 
						uci set network.wan.ipaddr="$data" 
				}
			}
 
			data="$(uci get network_sync.wan.ifname)"
			uci set network.wan.ifname="$data"
 
			data="$(uci get network_sync.wan.keepup)"
			uci set network.wan.keepup="$data"
			;;
		pppoeshare)
		#update wan section values
			data="$(uci get network_sync.wan.proto)"
			uci set network.wan.proto="$data"

			data="$(uci get network_sync.wan.ifname)"
			uci set network.wan.ifname="$data"

			data="$(uci get network_sync.wan.connectable)"
			uci set network.wan.connectable="$data"

			data="$(uci get network_sync.wan.keepup)"
			uci set network.wan.keepup="$data"

			data="$(uci get network_sync.wan.auto)"
			uci set network.wan.auto="$data"

			data="$(uci get network_sync.wan.mtu)"
			uci set network.wan.mtu="$data"

			data="$(uci get network_sync.wan.ip_config)"
			uci set network.wan.ip_config="$data"

			data="$(uci get network_sync.wan.lanif)"
			uci set network.wan.lanif="$data"

		#add internet section
			uci set network.internet=interface
		#update internet section values
			data="$(uci get network_sync.internet.keepalive)"
			uci set network.internet.keepalive="$data"

			data="$(uci get network_sync.internet.proto)"
			uci set network.internet.proto="$data"

			data="$(uci get network_sync.internet.parent)"
			uci set network.internet.parent="$data"

			data="$(uci get network_sync.internet.conn_mode)"
			uci set network.internet.conn_mode="$data"

			data="$(uci get network_sync.internet.dns_mode)"
			uci set network.internet.dns_mode="$data"

			data="$(uci get network_sync.internet.ip_mode)"
			uci set network.internet.ip_mode="$data"

			data="$(uci get network_sync.internet.username)"
			uci set network.internet.username="$data"

			data="$(uci get network_sync.internet.auto)"
			uci set network.internet.auto="$data"
			
			data="$(uci get network_sync.internet.dns)"
			uci set network.internet.dns="$data"

			data="$(uci get network_sync.internet.connectable)"
			uci set network.internet.connectable="$data"

			data="$(uci get network_sync.internet.mru)"
			uci set network.internet.mru="$data"

			data="$(uci get network_sync.internet.password)"
			uci set network.internet.password="$data"

			data="$(uci get network_sync.internet.service)"
			uci set network.internet.service="$data"

			data="$(uci get network_sync.internet.ac)"
			uci set network.internet.ac="$data"
			
			data="$(uci get network_sync.internet.ifname)"
			uci set network.internet.ifname="$data"
			data="$(uci get network_sync.internet.dnsv6_mode)"
			uci set network.internet.dnsv6_mode="$data"

			data="$(uci get network_sync.internet.ipv6_mode)"
			uci set network.internet.ipv6_mode="$data"
			;;
		dslite)
		#update values
			data="$(uci get network_sync.wan.proto)"
			uci set network.wan.proto="$data"

			data="$(uci get network_sync.wan.snd_conn)"
			uci set network.wan.snd_conn="$data"

			data="$(uci get network_sync.wan.connectable)"
			uci set network.wan.connectable="$data"

			data="$(uci get network_sync.wan.conn_mode)"
			uci set network.wan.conn_mode="$data"

			data="$(uci get network_sync.wan.parent)"
			uci set network.wan.parent="$data"

			data="$(uci get network_sync.wan.peeraddr)"
			uci set network.wan.peeraddr="$data"

			data="$(uci get network_sync.wan.AFTR_type)"
			uci set network.wan.AFTR_type="$data"
			
			data="$(uci get network_sync.wan.AFTR_name)"
			uci set network.wan.AFTR_name="$data"
			
			data="$(uci get network_sync.wan.ifname)"
			uci set network.wan.ifname="$data"

			data="$(uci get network_sync.wan.keepup)"
			uci set network.wan.keepup="$data"
			;;
		v6_plus)
		#update values
			data="$(uci get network_sync.wan.proto)"
			uci set network.wan.proto="$data"

			data="$(uci get network_sync.wan.snd_conn)"
			uci set network.wan.snd_conn="$data"

			data="$(uci get network_sync.wan.connectable)"
			uci set network.wan.connectable="$data"

			data="$(uci get network_sync.wan.conn_mode)"
			uci set network.wan.conn_mode="$data"

			data="$(uci get network_sync.wan.parent)"
			uci set network.wan.parent="$data"
			
			data="$(uci get network_sync.wan.ifname)"
			uci set network.wan.ifname="$data"

			data="$(uci get network_sync.wan.keepup)"
			uci set network.wan.keepup="$data"

			data="$(uci get network_sync.wan.carrier)"
			uci set network.wan.carrier="$data"
			;;
		*)
			echo "unsupported wan type..." > /dev/console
			;;
	esac
	local ipv6_enable=$(uci get protocol.wanv6.enable)
	if [ "$ipv6_enable" == "on" ]; then 
		#Update wanv6.ifname in needed.
		local ipv6_ifname=$(uci get network_sync.wanv6.ifname)
		local ipv4_ifname=$(uci get network_sync.wan.ifname)
		[ "$ipv6_enable" == "on" ] && {
			[ -n "$ipv4_ifname" ] && [ "$ipv6_ifname" != "$ipv4_ifname" ] && {
				uci set network_sync.wanv6.ifname="$ipv4_ifname"
				uci commit network_sync
			}
		}

		#delete wan and internet section
		#uci delete network.wanv6
		if [ "$wan_type" != "pppoeshare" ]; then
            delete_options "network.wanv6"
        else
            uci delete network.wanv6
        fi
		#uci delete network.internet
		[ "$wan_type" != "pppoeshare" ] && {
			#add wan section
			uci set network.wanv6=interface

			local wanv6_type="$(uci get network_sync.wanv6.wan_type)"
			case "$wanv6_type" in
				dhcp6c)
				#update values
					data="$(uci get network_sync.wanv6.proto)"
					uci set network.wanv6.proto="$data"

					data="$(uci get network_sync.wanv6.auto)"
					uci set network.wanv6.auto="$data"

					data="$(uci get network_sync.wanv6.lanif)"
					uci set network.wanv6.lanif="$data"

					data="$(uci get network_sync.wanv6.peerdns)"
					uci set network.wanv6.peerdns="$data"

					data="$(uci get network_sync.wanv6.ip_mode)"
					uci set network.wanv6.ip_mode="$data"

					data="$(uci get network_sync.wanv6.ip_config)"
					uci set network.wanv6.ip_config="$data"

					data="$(uci get network_sync.wanv6.wan_type)"
					uci set network.wanv6.wan_type="$data"

					data="$(uci get network_sync.wanv6.conn_mode)"
					uci set network.wanv6.conn_mode="$data"

					data="$(uci get network_sync.wanv6.connectable)"
					uci set network.wanv6.connectable="$data"

					data="$(uci get network_sync.wanv6.dns_mode)"
					uci set network.wanv6.dns_mode="$data"

					[ "$data" == "static" ] && {
						data="$(uci get network_sync.wanv6.dns)"
						uci set network.wanv6.dns="$data"
					}

					data="$(uci get network_sync.wanv6.ifname)"
					uci set network.wanv6.ifname="$data"
					;;
				pppoev6)
				#update wan section values
					data="$(uci get network_sync.wanv6.proto)"
					uci set network.wanv6.proto="$data"

					data="$(uci get network_sync.wanv6.lanif)"
					uci set network.wanv6.lanif="$data"

					data="$(uci get network_sync.wanv6.conn_mode)"
					uci set network.wanv6.conn_mode="$data"

					data="$(uci get network_sync.wanv6.dns_mode)"
					uci set network.wanv6.dns_mode="$data"

					[ "$data" == "static" ] && {
						data="$(uci get network_sync.wanv6.dns)"
						uci set network.wanv6.dns="$data"
					}

					data="$(uci get network_sync.wanv6.mru)"
					uci set network.wanv6.mru="$data"

					data="$(uci get network_sync.wanv6.connectable)"
					uci set network.wanv6.connectable="$data"

					data="$(uci get network_sync.wanv6.auto)"
					uci set network.wanv6.auto="$data"

					data="$(uci get network_sync.wanv6.password)"
					uci set network.wanv6.password="$data"

					data="$(uci get network_sync.wanv6.ip_config)"
					uci set network.wanv6.ip_config="$data"

					data="$(uci get network_sync.wanv6.username)"
					uci set network.wanv6.username="$data"
					
					data="$(uci get network_sync.wanv6.keeplive)"
					uci set network.wanv6.keeplive="$data"

					data="$(uci get network_sync.wanv6.wan_type)"
					uci set network.wanv6.wan_type="$data"

					data="$(uci get network_sync.wanv6.ifname)"
					uci set network.wanv6.ifname="$data"

					data="$(uci get network_sync.wanv6.ip_mode)"
					uci set network.wanv6.ip_mode="$data"
					;;
				passthrough)
					#update values
					data="$(uci get network_sync.wanv6.proto)"
					uci set network.wanv6.proto="$data"

					data="$(uci get network_sync.wanv6.wan_type)"
					uci set network.wanv6.wan_type="$data"

					data="$(uci get network_sync.wanv6.conn_mode)"
					uci set network.wanv6.conn_mode="$data"

					data="$(uci get network_sync.wanv6.connectable)"
					uci set network.wanv6.connectable="$data"
					;;	
				6to4)
					#update values
					data="$(uci get network_sync.wanv6.proto)"
					uci set network.wanv6.proto="$data"

					data="$(uci get network_sync.wanv6.auto)"
					uci set network.wanv6.auto="$data"

					data="$(uci get network_sync.wanv6.wan_type)"
					uci set network.wanv6.wan_type="$data"

					data="$(uci get network_sync.wanv6.connectable)"
					uci set network.wanv6.connectable="$data"

					data="$(uci get network_sync.wanv6.conn_mode)"
					uci set network.wanv6.conn_mode="$data"

					data="$(uci get network_sync.wanv6.ttl)"
					uci set network.wanv6.ttl="$data"

					data="$(uci get network_sync.wanv6.ifname)"
					uci set network.wanv6.ifname="$data"

					data="$(uci get network_sync.wanv6.dns)"
					uci set network.wanv6.dns="$data"
					;;
				*)
                    uci delete network.wanv6

					echo "unsupported wanv6 type..." > /dev/console
					;;
			esac
		}

        delete_options "network.lanv6"
        local lanv6_type=$(uci get network_sync.lanv6.proto)
        [ -z "$lanv6_type" ] && lanv6_type="dhcp6s"

        data="$(uci get protocol.${lanv6_type}.auto)"
        uci set network.lanv6.auto="$data"

        data="$(uci get protocol.${lanv6_type}.ifname)"
        uci set network.lanv6.ifname="$data"

        data="$(uci get protocol.${lanv6_type}.proto)"
        uci set network.lanv6.proto="$data"

        data="$(uci get protocol.${lanv6_type}.prefixlen)"
        uci set network.lanv6.prefixlen="$data"

        data="$(uci get protocol.${lanv6_type}.prefix)"
		if [ -n "$data" ]; then
			uci set network.lanv6.prefix="$data"
		else
			uci set network.lanv6.prefix="::"
		fi
		 
        

        case "$lanv6_type" in
            dhcp6s)
                data="$(uci get protocol.dhcp6s.endip)"
                uci set network.lanv6.endip="$data"

                data="$(uci get protocol.dhcp6s.startip)"
                uci set network.lanv6.startip="$data"

                data="$(uci get protocol.dhcp6s.leasetime)"
                uci set network.lanv6.leasetime="$data"
                ;;
            slaac)
                echo "slaac no need extra params..." > /dev/console
                ;;
            rdnss)
                data="$(uci get protocol.dhcp6s.rdnss_prefix_type)"
                uci set network.lanv6.rdnss_prefix_type="$data"
                ;;
            ndp)
                echo "ndp no need extra params..." > /dev/console
                ;;				
            *)
                echo "unsupported lanv6 type..." > /dev/console
                ;;
        esac
		
	fi
    
    if [ "$sysmode" = "AP" ];then
        if [ "$wan_type" != "dhcp" ];then
            uci set network.wan.proto="dhcp"
            uci set network.wan.ifname="br-wan"
            uci set network.wan.keepup="1"
            uci set network.wan.connectable="1"
            uci set network.wan.peerdns="1"
            uci set network.wan.hostname=$(get_hostname)
            uci set network.wan.conn_mode="auto"
            uci set network.wan.broadcast="1"
        fi
    fi

	if [ "$sysmode" == "Router" ]; then
		config_clear
		config_load network_sync
		config_foreach set_static_routing route
		config_clear
	fi

	uci commit network
	# lua -e 'require("luci.sys.config").saveconfig("device-config")'
}

start_service() {
	load_config

	local pid=""
	local wan_ifname="$(uci get network.wan.ifname)"

	# BCM product bug : pptp vpn connect fail problem when gre is on
	if [ -d /proc/fcache/ -a -e /lib/vpn/vpn_fc.sh ] ; then
		. /lib/vpn/vpn_fc.sh
		update_pptp_fc_gre_status
	fi

	[ -e /proc/sys/kernel/core_pattern ] && {
		ulimit -c unlimited
		echo '/tmp/%e.%p.%s.%t.core' > /proc/sys/kernel/core_pattern
	}
	

	# we insmod statistics in boot, so no need to do it here

	# netifd depends on "statistics" kernel module
        #local mode
        #mode=$(uci get sysmode.sysmode.mode)
        #if [ "$mode" == "Router" ]; then
                #insmod statistics sysmode=1
        #else
                #insmod statistics sysmode=2	
        #fi
	#service_start /sbin/netifd

	procd_open_instance
	procd_set_param command /sbin/netifd
	procd_set_param respawn 600 5 5
	procd_set_param watch network.interface

	pid=$(pidof netifd)
	if [ x"$pid" != x ]; then
		logx -p $pid $NETWORK_MOD_ID $LOGX_ID_NETIFD_START
	fi
	
	procd_close_instance
}

restart() {
	start
}

shutdown() {
	stop
}

stop_service() {
	local pid=$(pidof netifd)

	ifdown -a
	if [ x"$pid" != x ]; then
		sleep 1
	fi

	#service_stop /sbin/netifd
	#rmmod statistics

	if [ x"$pid" != x ]; then
		logx -p $pid $NETWORK_MOD_ID $LOGX_ID_NETIFD_STOP
	fi
}

reload_service() {
	#local action="$(uci get network.wan.action)"
	
	#if [ "$action" = "set" ]; then
	#	echo do not reload the QuickSetup configuration > /dev/console
	#	return
	#fi
	echo "===================== network reload =========================" > /dev/console
	rm -f /tmp/pppConnectionError /tmp/pppv6ConnectionError
	load_config
	ubus call network reload

    local wanifname
    local vlan_enable
    local vlan_id
    local vlan_prio

    config_load network
    config_get wanifname wan ifname
    config_get vlan_enable vlan enable "0"
    config_get vlan_id vlan id "0"
    config_get vlan_prio vlan priority "0"
    config_clear

    if [ "$vlan_enable" == "1" ]; then
        if [ "$wanifname" == "br-wan.""$vlan_id" ]; then
            vlan_set_prio br-wan $vlan_id $vlan_prio
        fi
    fi

    return 0
}

service_triggers() {
	procd_add_reload_trigger "network_sync" "iptv_v2"
}
