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

mediatek_setup_interfaces()
{
	local board="$1"

	case $board in
	bananapi,bpi-r64|\
	elecom,wrc-x3200gst3|\
	linksys,e8450|\
	linksys,e8450-ubi|\
	mediatek,mt7622-rfb1|\
	mediatek,mt7622-rfb1-ubi|\
	netgear,wax206|\
	reyee,ax3200-e5|\
	ruijie,rg-ew3200gx-pro)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3 lan4" wan
		;;
	buffalo,wsr-2533dhp2)
		ucidef_add_switch "switch0" \
			"0:lan" "1:lan" "2:lan" "3:lan" "4:wan" "6@eth0"
		;;
	ubnt,unifi-6-lr*)
		ucidef_set_interface_lan "eth0"
		;;
	xiaomi,redmi-router-ax6s)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3" wan
		;;
	*)
		ucidef_add_switch "switch0" \
				  "0:lan" "1:lan" "2:lan" "3:lan" "4:wan" "6u@eth0" "5u@eth1"
		;;
	esac
}

DEV_PATH_WMAC=platform/18000000.wmac
DEV_PATH_PCIE0=1a143000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0
DEV_PATH_PCIE1=1a145000.pcie/pci0001:00/0001:00:01.0/0001:01:00.0

mediatek_setup_wlan()
{
	dt_is_enabled wmac@18000000 && ucidef_add_wlan $DEV_PATH_WMAC
	dt_is_enabled pcie@1a143000 && ucidef_add_wlan $DEV_PATH_PCIE0
	dt_is_enabled pcie@1a145000 && ucidef_add_wlan $DEV_PATH_PCIE1
}

mediatek_setup_macs()
{
	local board="$1"
	local lan_mac=""
	local wan_mac=""
	local label_mac=""

	case $board in
	reyee,ax3200-e5|\
	ruijie,rg-ew3200gx-pro)
		lan_mac=$(macaddr_add $(get_mac_label) 1)
		;;
	xiaomi,redmi-router-ax6s)
		wan_mac=$(mtd_get_mac_ascii bdata ethaddr_wan)
		label_mac=$wan_mac
		;;
	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)
mediatek_setup_interfaces $board
mediatek_setup_macs $board
mediatek_setup_wlan $board
board_config_flush

exit 0
