#!/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_vlan_device() {
	local sec_name="$1"
	local id=""
	local plc_support=""
	local eth_num
	id="$(uci get network.vlan.id)"
	config_get name "$1" name
	#echo "delete_vlan_device:start to delete eth0.$id" > /dev/console
	real_dev_name=$(get_sgmac_eth_name)

	plc_support=$(get_product_plc_support)
	local switch_chip=$(uci -c /etc/profile.d get profile.switch.switch_chip)
	if [ -n "$plc_support" ] && [ "yes" == "$plc_support" ]; then
		if [ "$switch_chip" = "rtk_gsw" ]; then
			if [ "$name" == "eth1.""$id" ] && [ "$id" != "2" ] && [ "$id" != "3" ];then
				eth_num=$(uci -c /etc/profile.d get profile.@DEVINFO[0].eth_port_num) 
				if [ -n "$eth_num" ] && [ $eth_num -gt 2 ];then
					if [ "$id" != "4" ];then
						echo "delete_vlan_device:deleted eth1.$id" > /dev/console
						uci delete network."$sec_name"
					fi
				else
					echo "delete_vlan_device:deleted eth1.$id" > /dev/console
					uci delete network."$sec_name"
				fi
			fi

		else
			if [ "$name" == "$real_dev_name.""$id" ] && [ "$id" != "3" ] && [ "$id" != 4 ];then
				echo "delete_vlan_device:deleted $real_dev_name.$id" > /dev/console
				uci delete network."$sec_name"
			fi
		fi

	else
		if [ "$name" == "$real_dev_name.""$id" ]; then
			all_vlan_ids=$(get_all_vlan_ids)
			vid_match="false"
			for vlan_id in $all_vlan_ids
			do
				if [ "$id" == "$vlan_id" ]; then
					vid_match="true"
					break
				fi
			done

			if [ "$vid_match" == "false" ];then
				echo "delete_vlan_device:deleted $real_dev_name.$id" > /dev/console
				uci delete network."$sec_name"
			fi
		fi
	fi
}

ifname_sync()
{

	echo "br-wan"
	return
}

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
}

backup_protocol()
{
	local uci_r="uci -c /tmp/state"
	cp /etc/config/protocol /tmp/state
	$uci_r set protocol.dhcp.ifname="br-wan"
	$uci_r set protocol.pppoe.ifname="br-wan"
	$uci_r set protocol.static.ifname="br-wan"
	$uci_r set protocol.passthrough.ifname="br-wan"
	$uci_r set protocol.pppoev6.ifname="br-wan"
	$uci_r set protocol.dhcpv6.ifname="br-wan"
	$uci_r set protocol.6to4.ifname="br-wan"
	$uci_r commit protocol
}

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
}

update_device_mtu(){
	[ -z "$1" ] && return 0
	local sec_name="$1"
	local data=""
	local name=""
	
	data="$(uci get network_sync.wan.ifname)"
	data="$(ifname_sync "$data")"  #Note:multi-gmac device may not function ifname_sync,so no need this line.
	
	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
}

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

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
}

load_ewan_config() {
#copy some configs from network_sync to network
	local wan_type=""
	local network_wan_type=""
	local data=""
	local mode=""
	local role=""
	local dev_id=""
	local macaddr=""
	local if_name1=""
	local if_name2=""
	local plc_support=""
	local vpn_enabled="off"
	local vpntype="none"
	wan_type="$(uci get network_sync.wan.wan_type)"
	network_wan_type="$(uci get network.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)"
	}

	# backup protocol to /tmp/state
	backup_protocol

#if device eth0.x existed in network_sync, then update it in network
	config_load network
	data="$(uci get network_sync.vlan.enable)"
	id="$(uci get network_sync.vlan.id)"
	#echo "load_config:start to add eth0.$id" > /dev/console
	config_foreach delete_vlan_device device
	config_foreach delete_static_routing route

	real_dev_name=$(get_sgmac_eth_name)
	plc_support=$(uci -c /etc/profile.d get profile.switch.plc_support)

	local vlan_device="$real_dev_name.$id"
	local vlan_device_exist=""
	local idx=-1
	local device_name=""
	while true
	do
		idx=$((idx + 1))
		device_name="$(uci get network.@device[$idx].name 2>/dev/null)"
		if [ -z "$device_name" ]; then
			break
		else
			if [ "$device_name" = "$vlan_device" ]; then
				vlan_device_exist="$vlan_device"
				break
			fi
		fi
	done

	switch_rtl8367s=$(lsmod|grep -o "rtl8367s_gsw")
	if [ -n "$plc_support" ] && [ "yes" == "$plc_support" ] && [ -z "$switch_rtl8367s" ]; then
		if [ "$data" == "1" ] && [ "$id" != 3 ] && [ "$id" != 4 ];then
			if [ -z "$vlan_device_exist" ]; then
				local sec=""
				sec="$(uci add network device)"
				uci set network."$sec".enabled="1"
				uci set network."$sec".name="eth0.${id}"
				echo "load_config:added eth0.$id" > /dev/console
	#                       uci set network."$sec".macaddr="$macaddr"
			fi
        fi
	else

		if [ "$data" == "1" ];then

			all_vlan_ids=$(get_all_vlan_ids)
			vid_match="false"
			for vlan_id in $all_vlan_ids
			do
				if [ "$id" == "$vlan_id" ]; then
					vid_match="true"
					break
				fi
			done

			if [ "$vid_match" == "false" ];then
				if [ -z "$vlan_device_exist" ]; then
					local sec=""
					sec="$(uci add network device)"
					uci set network."$sec".enabled="1"
					uci set network."$sec".name="$real_dev_name.${id}"
					echo "load_config:added $real_dev_name.$id" > /dev/console
				fi
			fi


			[ -e "/proc/wandetect/isp_vid" ] && {
				echo "$id" > /proc/wandetect/isp_vid
			}
#			uci set network."$sec".macaddr="$macaddr"
		fi
	fi

	uci commit network
	config_clear
	config_load network
	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

	all_ifnames=$(get_all_eth_names)

	[ "$sysmode" == "AP" ] && {
		[ "$vlan_enable" == "1" ] && {
			uci set network.vlan.enable=0
			uci set network_sync.vlan.enable=0
			lanif=$(uci get network.lan.ifname)

			[ "$lanif" != "$all_ifnames" ] && {
				uci set network.lan.ifname="$all_ifnames"
			}
			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="${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

	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="$(ifname_sync "$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="$(ifname_sync "$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="$(ifname_sync "$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"
	dev_id="$(getfirm DEV_ID)"
	role="$(uci get bind_device_list."${dev_id}".role)"
	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="$(ifname_sync "$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="deco_$(getfirm MODEL)"

			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="$(ifname_sync "$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="$(ifname_sync "$data")"

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

			para_enable="$(uci get network_sync.internet.ac_enable)"
			data="$(uci get network_sync.internet.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="$(ifname_sync "$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="$(ifname_sync "$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="$(ifname_sync "$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="$(ifname_sync "$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="$(ifname_sync "$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="$(ifname_sync "$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="$(ifname_sync "$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="$(ifname_sync "$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="$(ifname_sync "$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="$(ifname_sync "$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="$(ifname_sync "$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="$(ifname_sync "$data")"

					data="$(uci get network_sync.wanv6.dns)"
					uci set network.wanv6.dns="$data"
					;;
				*)
					echo "unsupported wanv6 type..." > /dev/console
					uci delete network.wanv6
					;;
			esac
		}

		
	fi

	if [ "$sysmode" == "Router" ]; then
		config_clear
		config_load network_sync
		config_foreach set_static_routing route
		config_clear
	fi
	#FIXME
	#uci commit network
	# lua -e 'require("luci.sys.config").saveconfig("device-config")'
}

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

disable_section() {
	local sec_name="$1"
	uci set network."$sec_name".enable=0
}

delete_other_wan_interfaces() {
	local sec_name="$1"
	# Any new interface should be added here to avoid being deleted (this is absurb and must be fixed!)
	local interfaces="wan internet wanv6 internetv6 loopback lan guest lanv6 hgw wanb wanbv6"

	for interface in $interfaces; do
		[ "$interface" = "$sec_name" ] && return 0
	done

	uci delete network."$sec_name"
	#if [ "$sec_name" != "wan" ] && [ "$sec_name" != "internet" ] && [ "$sec_name" != "wanv6" ] && [ "$sec_name" != "internetv6" ];then
	#	uci delete network."$sec_name"
	#fi
}

delete_ewan_mode_interfaces() {
	local sec_name="$1"
	local interfaces="wan internet wanv6 internetv6 lanv6"

	for interface in $interfaces; do
		[ "$interface" = "$sec_name" ] && uci delete network."$sec_name"
	done
}

update_network_option() {
	local section="$1"; shift

	while [ "$#" -ge 1 ]; do
		local option="$1"; shift
		local data="$(uci get network_sync.${section}.${option})"
		uci set network.${section}.${option}="$data"
	done
}

update_lanv6_option() {
	local section="$1"; shift

	while [ "$#" -ge 1 ]; do
		local option="$1"; shift
		local data="$(uci get protocol.${section}.${option})"
		uci set network.lanv6.${option}="$data"
	done
}

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

	update_lanv6_option ${lanv6_type} auto ifname proto prefixlen prefix

	case "$lanv6_type" in
		dhcp6s)
			update_lanv6_option ${lanv6_type} endip startip leasetime
			;;
		slaac)
			echo "slaac no need extra params..." > /dev/console
			;;
		rdnss)
			update_lanv6_option ${lanv6_type} rdnss_prefix_type
			;;
		ndp)
			echo "ndp no need extra params..." > /dev/console
			;;
		*)
			echo "unsupported lanv6 type..." > /dev/console
			;;
	esac
}

load_ewan_lan_config() {
	local enabled=$(uci get mobile_lanv6.basic.enabled)
	[ "$enabled" = "1" ] && return 0

	local data="$(uci get network_sync.lanv6)"
	[ -n "$data" ] && load_ewan_lanv6_config
}

# AP mode: Only one EWAN connection, and wan_type should be "dhcp", and no VLAN.
update_ap_mode() {
	wan_type="$(uci get network.wan.wan_type)"
	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="deco_$(getfirm MODEL)"
		uci set network.wan.conn_mode="auto"
		uci set network.wan.broadcast="1"
	fi
}

update_defaultroute() {
	( [ -z $1 ] || [ -z $2 ] ) && exit 0
	local conn_id="$1"
	local defaultroute="$2"
	local wan_name="wan"
	local internet_name="internet"
	local wanv6_name="wanv6"
	local data=""

	if [ ${conn_id} -eq 0 ]; then
		wan_name="wan"
		internet_name="internet"
		wanv6_name="wanv6"
	else
		wan_name="wan_"${conn_id}
		internet_name="internet_"${conn_id}
		wanv6_name="wanv6_"${conn_id}
	fi

	data="$(uci get network.${wan_name})"
	[ -n "$data" ] && uci set network.${wan_name}.defaultroute=$defaultroute

	data="$(uci get network.${internet_name})"
	[ -n "$data" ] && uci set network.${internet_name}.defaultroute=$defaultroute

	data="$(uci get network.${wanv6_name})"
	[ -n "$data" ] && uci set network.${wanv6_name}.defaultroute=$defaultroute
}

load_mobile_lanv6_config() {
	local enabled=$(uci get mobile_lanv6.basic.enabled)
	local interface=$(uci get mobile_lanv6.basic.interface)

	[ -z "$interface" ] && interface=lanv6

	[ "$enabled" = "1" ] && {
		uci delete network.$interface
		uci set network.$interface=interface
		uci set network.$interface.auto=1
		uci set network.$interface.ifname=$(uci get mobile_lanv6.basic.ifname)
		uci set network.$interface.proto=$(uci get mobile_lanv6.basic.proto)
		uci set network.$interface.prefix=$(uci get mobile_lanv6.basic.prefix)
		uci set network.$interface.prefixlen=$(uci get mobile_lanv6.basic.prefixlen)
		uci set network.$interface.startip=$(uci get mobile_lanv6.basic.startip)
		uci set network.$interface.endip=$(uci get mobile_lanv6.basic.endip)
		uci set network.$interface.leasetime=$(uci get mobile_lanv6.basic.leasetime)
		uci set network.$interface.primary_dns=$(uci get mobile_lanv6.basic.primary_dns)
		uci set network.$interface.secondary_dns=$(uci get mobile_lanv6.basic.secondary_dns)
	} || {
		uci delete network.$interface.prefix
		uci delete network.$interface.prefixlen
		uci delete network.$interface.primary_dns
		uci delete network.$interface.secondary_dns
	}
}

load_mobile_wan_config() {
	local iface_name="unknown"
	#TODO: 10 is not the most suitable number
	for index in $(seq 0 1 10)
	do
		iface_type=$(uci -c /etc/profile.d get interfaces.@interface[$index].type)
		if [ "$iface_type" = "mobile" ]; then
			iface_name=$(uci -c /etc/profile.d get interfaces.@interface[$index].name)
			break;
		else
			continue
		fi
	done
	echo "!!!!!!!!!iface_name:$iface_name" > /dev/console
	uci set network.wanb=interface
	uci set network.wanb.wan_type="mobile"
	uci set network.wanb.proto="mobile"
	uci set network.wanb.ifname="$iface_name"
	uci set network.wanb.connectable="1"
	uci set network.wanb.keepup="1"
	uci set network.wanb.auto="0"
	uci set network.wanb.mtu="1500"
	uci set network.wanb.peerdns="1"
	uci set network.wanb.conn_mode="auto"
	uci set network.wanb.broadcast="0"
	uci commit network
	uci set network.wanbv6=interface
	uci set network.wanbv6.wan_type="mobile"
	uci set network.wanbv6.proto="mobilev6"
	uci set network.wanbv6.ifname="$iface_name"
	uci set network.wanbv6.connectable="1"
	uci set network.wanbv6.keepup="1"
	uci set network.wanbv6.auto="0"
	uci set network.wanbv6.mtu="1500"
	uci set network.wanbv6.peerdns="1"
	uci set network.wanbv6.conn_mode="auto"
	uci set network.wanbv6.broadcast="0"
	uci commit network

	local isMobile=$(isMobile 2>/dev/null)
	local sysmode=""
	[ "$isMobile" = "yes" ] && {
		sysmode=$(mobile_cmd mode)
	}

	if [ "$sysmode" = "LTE" ] || [ "$sysmode" = "Mobile_5G" ]; then
		# interface vpn
		config_load vpn
		config_get vpn_enabled "client" "enabled"
		config_get vpntype "client" "vpntype"

		if [ "$vpn_enabled" = "off" -o "$vpntype" = "none" ]; then
			echo "vpn client is off" > /dev/console
		else
			uci set network_sync.vpn.parent="wanb"
			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"
				;;

			*)
				echo "unsupported vpn type..." > /dev/console
				;;
			esac
		fi
	fi
	uci commit network
}

disable_mobile_connectable() {
	local tmp

	tmp=$(uci get network.wanb)
	[ -n "$tmp" ] && uci set network.wanb.connectable=0

	tmp=$(uci get network.wanbv6)
	[ -n "$tmp" ] && uci set network.wanbv6.connectable=0
}

update_mobile_backup_config() {
	[ ! -e /etc/config/mobile_backup ] && return 0

	local tmp
	local connectable=$(uci get mobile_backup.mobile.connectable)
	[ -z "$connectable" ] && connectable=0

	tmp=$(uci get network.wanb)
	[ -n "$tmp" ] && {
		uci set network.wanb.connectable=$connectable
	}

	tmp=$(uci get network.wanbv6)
	[ -n "$tmp" ] && {
		uci set network.wanbv6.connectable=$connectable
	}
}

update_ewan_backup_config() {
	[ ! -e /etc/config/mobile_backup ] && return 0

	local tmp
	local defaultroute=$(uci get mobile_backup.ewan.defaultroute)
	local peerdns=$(uci get mobile_backup.ewan.peerdns)

	tmp=$(uci get network.wan)
	[ -n "$tmp" ] && {
		[ -n "$defaultroute" ] && uci set network.wan.defaultroute=$defaultroute
		[ -n "$peerdns" ] && uci set network.wan.peerdns=$peerdns
	}

	tmp=$(uci get network.internet)
	[ -n "$tmp" ] && {
		[ -n "$defaultroute" ] && uci set network.internet.defaultroute=$defaultroute
		[ -n "$peerdns" ] && uci set network.internet.peerdns=$peerdns
	}

	tmp=$(uci get network.wanv6)
	[ -n "$tmp" ] && {
		[ -n "$defaultroute" ] && uci set network.wanv6.defaultroute=$defaultroute
		[ -n "$peerdns" ] && uci set network.wanv6.peerdns=$peerdns
	}
}

load_mobile_config() {
	local role=$(uci get bind_device_list.$(getfirm DEV_ID).role)
	[ "$role" = "RE" ] && echo "RE mode don't need to mobile dial" > /dev/console && return 0

	load_mobile_wan_config
	load_mobile_lanv6_config
}

ewan_disable_defaultroute() {
	local sec_name="$1"
	if [ "$sec_name" = "wan" ] || [ "$sec_name" = "internet" ] || [ "$sec_name" = "wanv6" ] || [ "$sec_name" = "internetv6" ];then
		uci set network.$sec_name.defaultroute='0'
	fi
}

ewan_reconnect_config() {
	local ewan_reconnect=0
	[ -e /tmp/ewan_reconnect ] && ewan_reconnect=$(cat /tmp/ewan_reconnect)
	[ "$ewan_reconnect" = "1" ] && {
		config_foreach ewan_disable_defaultroute interface
	}
}

update_ewan_config_onboarding() {
	local tmp

	tmp=$(uci get network.wan)
	[ -n "$tmp" ] && {
		uci set network.wan.defaultroute=0
		uci set network.wan.peerdns=0
	}

	tmp=$(uci get network.internet)
	[ -n "$tmp" ] && {
		uci set network.internet.defaultroute=0
		uci set network.internet.peerdns=0
	}
}

load_config() {
	local sysmode=""
	local vid=""
	local data=""
	local ewan_conns_num=0
	local isMobile=$(isMobile 2>/dev/null)

	config_load network

	if [ "$isMobile" = "yes" ]; then
		sysmode=$(mobile_cmd mode)
	else
		sysmode=$(uci get sysmode.sysmode.mode)
	fi

	config_foreach delete_other_wan_interfaces interface
	# config_foreach disable_section vlan

	if [ "$sysmode" = "Router" ]; then
		load_ewan_config 0 $ewan_conns_num

	fi

	if [ "$sysmode" = "LTE" ] || [ "$sysmode" = "Mobile_5G" ] ; then
		config_foreach delete_ewan_mode_interfaces interface
		load_mobile_config

		local binded=$(cat /tmp/is_binded)
		[ "$binded" != "1" ] && {
			load_ewan_config 0 $ewan_conns_num
			update_ewan_config_onboarding
		}
	fi

	if [ "$sysmode" = "Router" ]; then
		local backup_type=$(uci get accessmode.ewan.backup_type)
		[ "$backup_type" = "mobile" ] && {
			load_mobile_config
			update_mobile_backup_config
			update_ewan_backup_config
			local backup_on=$(uci get accessmode.ewan.backup)
			[ "$backup_on" != "on" ] && disable_mobile_connectable
		}

		load_ewan_lan_config
	fi

    if [ "$sysmode" = "AP" ];then
		config_foreach delete_static_routing route
        update_ap_mode
    fi

	#[ "$sysmode" = "Router" ] && ewan_reconnect_config

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

}

start_service() {
	load_config

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

	[ -e /proc/sys/kernel/core_pattern ] && {
		ulimit -c unlimited
		echo '/tmp/%e.%p.%s.%t.core' > /proc/sys/kernel/core_pattern
	}
	
	# 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

	local isMobile=$(isMobile 2>/dev/null)
	[ -n "$1" ] && [ "$isMobile" = "yes" ] && {
		echo "update $1" > /dev/console
		if [ "$1" == "mobile_lanv6" ]; then
			load_mobile_lanv6_config
			uci commit network
		elif [ "$1" == "ewan_lanv6" ]; then
			load_ewan_lanv6_config
			uci commit network
		fi
		return 0
	}

	rm -f /tmp/pppConnectionError /tmp/pppv6ConnectionError
	load_config
	ubus call network reload

    local vlan_enable
    local vlan_id
    local vlan_prio

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

    real_dev_name=$(get_sgmac_eth_name)

    if [ "$vlan_enable" == "1" ]; then
	local tmp_str="$(cat /proc/net/vlan/eth0.$vlan_id |grep EGRESS |grep -o "0:.")"
	local old_prio="${tmp_str:2}"

    vlan_set_prio $real_dev_name $vlan_id $vlan_prio

	if [ -n "$old_prio" ] && [ $vlan_prio -ne $old_prio ]; then
		echo "old_prio[$old_prio] ---> new_vlan_prio[$vlan_prio], reload wanDetect" > /dev/console
		ubus call wanDetect reload &
	fi
    fi

}

service_triggers() {
	procd_add_reload_trigger "network_sync"
}
