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

START=20
STOP=90

USE_PROCD=1

init_switch() {
	setup_switch() { return 0; }

	include /lib/network
	setup_switch
}

start_service() {
	init_switch

	procd_open_instance
	procd_set_param command /sbin/netifd
	procd_set_param respawn
	procd_set_param watch network.interface
	[ -e /proc/sys/kernel/core_pattern ] && {
		procd_set_param limits core="unlimited"
	}
	procd_close_instance
}

reload_service() {
	local rv=0

	init_switch
	conf_mod=$(uci get wireless.2g_5g.conf_mode)
	ethif=$(uci get network.lan.ifname)
	apcli2g=$(uci get wireless.apcli_2g.rootap_ssid)
	apcli5g=$(uci get wireless.apcli_5g.rootap_ssid)
	
	if [ "x$ethif" = "x" ] ; then
		ethif="lan0"
	fi
	if [ $conf_mod -eq 0 ] ; then
		killall dhcpfilter.sh 2>>/dev/null
		#ebtables -D OUTPUT -o apcli0 -p ipv4 --ip-protocol 17  --ip-sport 68 --ip-dport 67 -j DROP 2>>/dev/null
		#ebtables -D OUTPUT -o apclix0 -p ipv4 --ip-protocol 17  --ip-sport 68 --ip-dport 67 -j DROP 2>>/dev/null
		ebtables -D OUTPUT -o $ethif -p ipv4 --ip-protocol 17 --ip-sport 68  --ip-dport 67 -j DROP 2>>/dev/null
	else
		if [ "$apcli2g" != 12345678NETGEAR ] || [ "$apcli5g" != "12345678NETGEAR-5G" ] ; then
			ebtables  -A OUTPUT -o $ethif -p IPv4 --ip-proto 17 --ip-sport 68 --ip-dport 67 -j DROP
		fi
	fi
	ubus call network reload || rv=1
	#/sbin/wifi reload_legacy
	#monitor_wifi_switch &
	return $rv
}

stop_service() {
    #monitor_wifi_switch_exit
	/sbin/wifi down
	ifdown -a
	sleep 1
}

service_running() {
	local factory_mode=`envctl factory get factory_mode`
	if [ "$factory_mode" == "off" ]; then
		ubus -t 120 wait_for network.interface 
		mkdir -p /tmp/mtk/wifi/
		touch /tmp/mtk/wifi/normal_reload
		/sbin/wifi reload
		#monitor_wifi_switch &
	else
		ifconfig ra0 up
		ifconfig rax0 up 
		ifconfig ra0 down
		ifconfig rax0 down
		brctl addif br-lan  ra0
		brctl addif br-lan  rax0
	fi
	
}

validate_atm_bridge_section()
{
	uci_validate_section network "atm-bridge" "${1}" \
		'unit:uinteger:0' \
		'vci:range(32, 65535):35' \
		'vpi:range(0, 255):8' \
		'atmdev:uinteger:0' \
		'encaps:or("llc", "vc"):llc' \
		'payload:or("bridged", "routed"):bridged'
}

validate_route_section()
{
	uci_validate_section network route "${1}" \
		'interface:string' \
		'target:cidr4' \
		'netmask:netmask4' \
		'gateway:ip4addr' \
		'metric:uinteger' \
		'mtu:uinteger' \
		'table:or(range(0,65535),string)'
}

validate_route6_section()
{
	uci_validate_section network route6 "${1}" \
		'interface:string' \
		'target:cidr6' \
		'gateway:ip6addr' \
		'metric:uinteger' \
		'mtu:uinteger' \
		'table:or(range(0,65535),string)'
}

validate_rule_section()
{
	uci_validate_section network rule "${1}" \
		'in:string' \
		'out:string' \
		'src:cidr4' \
		'dest:cidr4' \
		'tos:range(0,31)' \
		'mark:string' \
		'invert:bool' \
		'lookup:or(range(0,65535),string)' \
		'goto:range(0,65535)' \
		'action:or("prohibit", "unreachable", "blackhole", "throw")'
}

validate_rule6_section()
{
	uci_validate_section network rule6 "${1}" \
		'in:string' \
		'out:string' \
		'src:cidr6' \
		'dest:cidr6' \
		'tos:range(0,31)' \
		'mark:string' \
		'invert:bool' \
		'lookup:or(range(0,65535),string)' \
		'goto:range(0,65535)' \
		'action:or("prohibit", "unreachable", "blackhole", "throw")'
}

validate_switch_section()
{
	uci_validate_section network switch "${1}" \
		'name:string' \
		'enable:bool' \
		'enable_vlan:bool' \
		'reset:bool' \
		'ar8xxx_mib_poll_interval:uinteger' \
		'ar8xxx_mib_type:range(0,1)'
}

validate_switch_vlan()
{
	uci_validate_section network switch_vlan "${1}" \
		'device:string' \
		'vlan:uinteger' \
		'ports:list(ports)'
}

service_triggers()
{
	procd_add_reload_trigger network wireless

	procd_open_validate
	validate_atm_bridge_section
	validate_route_section
	[ -e /proc/sys/net/ipv6 ] && validate_route6_section
	validate_rule_section
	[ -e /proc/sys/net/ipv6 ] && validate_rule6_section
	validate_switch_section
	validate_switch_vlan
	procd_close_validate
}

shutdown() {
    #monitor_wifi_switch_exit
	/sbin/wifi down
	ifdown -a
	sleep 1
}
