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

ath79_setup_interfaces()
{
	local board="$1"

	case "$board" in
	aerohive,hiveap-121|\
	meraki,mr18)
		ucidef_set_interface_lan "eth0"
		;;
	domywifi,dw33d)
		ucidef_add_switch "switch0" \
			"0@eth0" "1:wan" "2:lan" "3:lan" "4:lan" "5:lan" "6@eth1"
		;;
	dongwon,dw02-412h-64m|\
	dongwon,dw02-412h-128m)
		ucidef_add_switch "switch0" \
			"0@eth0" "2:lan:4" "3:lan:3" "4:lan:2" "5:lan:1" "1:wan"
		;;
	glinet,gl-ar750s-nor|\
	glinet,gl-ar750s-nor-nand)
		ucidef_add_switch "switch0" \
			"0@eth0" "2:lan:2" "3:lan:1" "1:wan"
		;;
	glinet,gl-e750|\
	zte,mf282)
		ucidef_set_interface_lan "eth0"
		ucidef_set_interface "wan" device "/dev/cdc-wdm0" protocol "qmi"
		;;
	glinet,gl-xe300)
		ucidef_set_interface_wan "eth1"
		ucidef_add_switch "switch0" \
			"0@eth0" "4:lan"
		;;
	glinet,gl-x1200-nor|\
	glinet,gl-x1200-nor-nand)
		ucidef_add_switch "switch0" \
			"0@eth0" "1:lan:4" "2:lan:3" "3:lan:2" "4:lan:1" "5:wan"
		;;
	linksys,ea4500-v3)
		ucidef_add_switch "switch0" \
			"6@eth1" "1:lan" "2:lan" "3:lan" "4:lan" "5:wan" "0@eth0"
		;;
	netgear,pgzng1)
		ucidef_set_interfaces_lan_wan "eth1" "eth0"
		;;
	netgear,r6100)
		ucidef_set_interface_wan "eth1"
		ucidef_add_switch "switch0" \
			"0@eth0" "1:lan:4" "2:lan:1" "3:lan:2" "4:lan:3"
		;;
	netgear,wndr3700-v4|\
	netgear,wndr4300|\
	netgear,wndr4300sw|\
	netgear,wndr4300-v2)
		ucidef_add_switch "switch0" \
			"0@eth0" "1:lan:4" "2:lan:3" "3:lan:2" "4:lan:1" "5:wan"
		;;
	netgear,wndr4500-v3)
		ucidef_add_switch "switch0" \
			"0@eth0" "1:lan" "2:lan" "3:lan" "4:lan" "5:wan"
		;;
	netgear,wndr4300tn)
		ucidef_add_switch "switch0" \
			"0@eth0" "1:lan:4" "2:lan:3" "3:lan:2" "4:lan:1"
		;;
	zte,mf281)
		ucidef_add_switch "switch0" \
			"0@eth0" "5:lan"
		;;
	zte,mf286|\
	zte,mf286a|\
	zte,mf286r)
		ucidef_add_switch "switch0" \
			"0@eth0" "1:lan:4" "2:lan:3" "3:lan:2" "5:lan:1"
		;;
	zyxel,emg2926-q10a|\
	zyxel,nbg6716)
		ucidef_add_switch "switch0" \
			"0@eth0" "1:lan" "2:lan" "3:lan" "4:lan" "5:wan" "6@eth1"
		;;
	*)
		ucidef_set_interfaces_lan_wan "eth0" "eth1"
		;;
	esac
}

ath79_setup_macs()
{
	local board="$1"

	case "$board" in
	dongwon,dw02-412h-64m|\
	dongwon,dw02-412h-128m)
		wan_mac=$(mtd_get_mac_binary art 0x0)
		label_mac=$wan_mac
		;;
	glinet,gl-x1200-nor|\
	glinet,gl-x1200-nor-nand)
		wan_mac=$(mtd_get_mac_binary art 0x0)
		lan_mac=$(macaddr_add "$wan_mac" 1)
		;;
	meraki,mr18)
		lan_mac=$(mtd_get_mac_binary_ubi board-config 102)
		;;
	netgear,wndr3700-v4|\
	netgear,wndr4300|\
	netgear,wndr4300sw|\
	netgear,wndr4300-v2|\
	netgear,wndr4500-v3)
		wan_mac=$(mtd_get_mac_binary caldata 0x6)
		;;
	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
