#!/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
}

load_ewan_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

#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" -a "$carrier_network" != "$carrier_sync" ]] && {
			ubus call network.interface.wan disconnect
			ubus call network.interface.wanv6 disconnect
		}
	}

	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="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="$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" == "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" == "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
		}
	fi
	#FIXME
	#uci commit network
	# lua -e 'require("luci.sys.config").saveconfig("device-config")'
}

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"

        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)"
        uci set network.lanv6.prefix="$data"

        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
}

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
}

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

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

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 ifname=$(uci -c /etc/profile.d get mobile.interface.name)
	[ "ifname" = "none" ] && ifname="wwan0_1"
	echo $ifname > /tmp/mobile_ifname

	uci set network.wanb=interface
	uci set network.wanb.wan_type="mobile"
	uci set network.wanb.proto="mobile"
	uci set network.wanb.ifname=$ifname
	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 set network.wanbv6=interface
	uci set network.wanbv6.wan_type="mobile"
	uci set network.wanbv6.proto="mobilev6"
	uci set network.wanbv6.ifname=$ifname
	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"
}

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
}

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 access_mode=""
	local vid=""
	local data=""
	local ewan_conns_num=0

	config_load network

	sysmode=$(uci get sysmode.sysmode.mode)
	if [ -z "$sysmode" ]; then
		sysmode="Router"
	fi

	if [ "$sysmode" == "Router" ] && [ -f "/etc/config/accessmode" ]; then
		echo "Router, file accessmode exits." > /dev/console
		access_mode=$(uci get accessmode.accessmode.mode)
		[ "$access_mode" == "mobile" ] && {
			#TODO: Adapter Mobile_5G and LTE
			sysmode="LTE"
		}
	fi

	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
        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
    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)"

	[ -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
	echo "===================== network reload =========================" > /dev/console
	
	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 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"
}
