
. /lib/functions/system.sh
. /lib/functions/uci-defaults.sh

ath79_setup_interfaces()
{
	local board="$1"

	case "$board" in
	buffalo,whr-g301n)
		ucidef_set_interface_wan "eth0"
		ucidef_add_switch "switch0" \
			"0@eth1" "1:lan:4" "2:lan:3" "3:lan:2" "4:lan:1"
		;;
	dlink,dir-615-e4|\
	netgear,wnr1000-v2|\
	netgear,wnr2000-v3|\
	netgear,wnr612-v2|\
	on,n150r|\
	tplink,tl-wr740n-v1|\
	tplink,tl-wr740n-v3|\
	tplink,tl-wr741-v1|\
	tplink,tl-wr743nd-v1|\
	tplink,tl-wr841-v5|\
	tplink,tl-wr941-v4)
		ucidef_set_interface_wan "eth0"
		ucidef_add_switch "switch0" \
			"0@eth1" "1:lan" "2:lan" "3:lan" "4:lan"
		;;
	engenius,eap350-v1|\
	engenius,ecb350-v1|\
	pqi,air-pen|\
	tplink,tl-mr10u|\
	tplink,tl-mr3020-v1|\
	tplink,tl-mr3040-v2|\
	tplink,tl-wa701nd-v1|\
	tplink,tl-wa730re-v1|\
	tplink,tl-wa801nd-v1|\
	tplink,tl-wa801nd-v3|\
	tplink,tl-wa801nd-v4|\
	tplink,tl-wa830re-v1|\
	tplink,tl-wa850re-v1|\
	tplink,tl-wa850re-v2|\
	tplink,tl-wa860re-v1|\
	tplink,tl-wa901nd-v1|\
	tplink,tl-wa901nd-v2|\
	tplink,tl-wa901nd-v3|\
	tplink,tl-wa901nd-v4|\
	tplink,tl-wa901nd-v5|\
	tplink,tl-wr703n|\
	tplink,tl-wr802n-v1|\
	tplink,tl-wr802n-v2|\
	ubnt,bullet-m-ar7240|\
	ubnt,bullet-m-ar7241|\
	ubnt,nanobridge-m|\
	ubnt,picostation-m|\
	ubnt,nanostation-loco-m)
		ucidef_set_interface_lan "eth0"
		;;
	engenius,enh202-v1)
		ucidef_set_interface_lan "eth0"
		ucidef_add_switch "switch0" \
			"0@eth1" "4:lan:1"
		;;
	tplink,tl-mr3220-v1|\
	tplink,tl-mr3420-v1|\
	tplink,tl-mr3420-v3|\
	tplink,tl-wr841-v7|\
	tplink,tl-wr841-v9|\
	tplink,tl-wr841-v10|\
	tplink,tl-wr841-v11|\
	tplink,tl-wr841-v12|\
	tplink,tl-wr940n-v3|\
	tplink,tl-wr940n-v4|\
	tplink,tl-wr940n-v6|\
	tplink,tl-wr941nd-v6)
		ucidef_set_interface_wan "eth1"
		ucidef_add_switch "switch0" \
			"0@eth0" "1:lan:4" "2:lan:3" "3:lan:2" "4:lan:1"
		;;
	tplink,tl-mr3420-v2|\
	tplink,tl-wr740n-v4|\
	tplink,tl-wr740n-v5|\
	tplink,tl-wr741nd-v4|\
	tplink,tl-wr841-v8|\
	tplink,tl-wr941nd-v5)
		ucidef_set_interface_wan "eth1"
		ucidef_add_switch "switch0" \
			"0@eth0" "1:lan:4" "2:lan:1" "3:lan:2" "4:lan:3"
		;;
	tplink,tl-wpa8630p-v2-int|\
	tplink,tl-wpa8630p-v2.0-eu|\
	tplink,tl-wpa8630p-v2.1-eu)
		# port 5 (internal) is the power-line port
		ucidef_add_switch "switch0" \
			"0@eth0" "2:lan:3" "3:lan:2" "4:lan:1" "5:lan:4"
		;;
	tplink,tl-wr941-v2)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3 lan4" "wan"
		;;
	tplink,tl-wr941n-v7-cn)
		ucidef_add_switch "switch0" \
			"0@eth0" "2:lan:1" "3:lan:2" "4:lan:3" "5:lan:4" "1:wan"
		;;
	ubnt,airrouter)
		ucidef_set_interface_wan "eth1"
		ucidef_add_switch "switch0" \
			"0@eth0" "1:lan:4" "2:lan:3" "3:lan:2" "4:lan:1"
		;;
	ubnt,nanostation-m)
		ucidef_set_interfaces_lan_wan "eth1" "eth0"
		;;
	*)
		ucidef_set_interfaces_lan_wan "eth0" "eth1"
		;;
	esac
}

ath79_setup_macs()
{
	local board="$1"

	case "$board" in
	dlink,dir-615-e4)
		lan_mac=$(mtd_get_mac_ascii "nvram" "lan_mac")
		wan_mac=$(mtd_get_mac_ascii "nvram" "wan_mac")
		label_mac=$wan_mac
		;;
	engenius,enh202-v1|\
	ubnt,airrouter|\
	ubnt,bullet-m-ar7240|\
	ubnt,bullet-m-ar7241|\
	ubnt,nanobridge-m|\
	ubnt,nanostation-loco-m|\
	ubnt,nanostation-m|\
	ubnt,picostation-m)
		label_mac=$(cat /sys/class/ieee80211/phy0/macaddress)
		;;
	tplink,tl-wr941-v2|\
	tplink,tl-wr941n-v7-cn)
		base_mac=$(mtd_get_mac_binary u-boot 0x1fc00)
		wan_mac=$(macaddr_add "$base_mac" 1)
		;;
	esac

	[ -n "$lan_mac" ] && ucidef_set_interface_macaddr "lan" $lan_mac
	[ -n "$wan_mac" ] && ucidef_set_interface_macaddr "wan" $wan_mac
	[ -n "$label_mac" ] && ucidef_set_label_macaddr $label_mac
}

board_config_update
board=$(board_name)
ath79_setup_interfaces $board
ath79_setup_macs $board
board_config_flush

exit 0
