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

mediatek_setup_interfaces()
{
	local board="$1"

	case $board in
	abt,asr3000|\
	buffalo,wsr-6000ax8|\
	cmcc,rax3000m|\
	h3c,magic-nx30-pro|\
	imou,hx21|\
	konka,komi-a31|\
	netis,nx30v2|\
	netis,nx31|\
	nokia,ea0326gmp|\
	mercusys,mr80x-v3|\
	routerich,ax3000-v1|\
	zbtlink,zbt-z8103ax|\
	zbtlink,zbt-z8103ax-c)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3" eth1
		;;
	acelink,ew-7886cax|\
	tplink,eap683-lr)
		ucidef_set_interface_lan "eth0" "dhcp"
		;;
	acer,predator-w6|\
	acer,predator-w6d)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3 game" eth1
		;;
	acer,predator-w6x-stock|\
	acer,predator-w6x-ubootmod)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3 lan4" eth1
		;;
	acer,vero-w6m)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3" internet
		;;
	arcadyan,mozart)
		ucidef_set_interfaces_lan_wan "lan0 eth1" eth2
		;;
	asus,rt-ax52|\
	asus,rt-ax59u|\
	asus,zenwifi-bt8|\
	asus,zenwifi-bt8-ubootmod|\
	cetron,ct3003|\
	cmcc,a10-stock|\
	cmcc,a10-ubootmod|\
	confiabits,mt7981|\
	creatlentem,clt-r30b1|\
	creatlentem,clt-r30b1-112m|\
	cudy,wr3000-v1|\
	jcg,q30-pro|\
	keenetic,kn-3711|\
	keenetic,kn-3811|\
	netis,nx32u|\
	qihoo,360t7|\
	routerich,ax3000|\
	routerich,ax3000-ubootmod|\
	routerich,be7200|\
	tenbay,wr3000k)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3" wan
		;;
	asus,tuf-ax4200|\
	iptime,ax3000sm|\
	iptime,ax7800m-6e|\
	jdcloud,re-cp-03|\
	mediatek,mt7981-rfb|\
	netcore,n60|\
	netcore,n60-pro|\
	nradio,c8-668gl|\
	ruijie,rg-x60-pro|\
	unielec,u7981-01*|\
	wavlink,wl-wn536ax6-a|\
	zbtlink,zbt-z8102ax|\
	zbtlink,zbt-z8102ax-v2)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3 lan4" eth1
		;;
	asiarf,ap7986-003|\
	asus,tuf-ax4200q|\
	asus,tuf-ax6000|\
	glinet,gl-mt6000|\
	tplink,tl-xdr4288|\
	tplink,tl-xdr6088|\
	tplink,tl-xtr8488)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3 lan4 lan5" eth1
		;;
	bananapi,bpi-r3)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3 lan4 sfp2" "sfp1 wan"
		;;
	bananapi,bpi-r4)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3 sfp-lan" "wan sfp-wan"
		;;
	bananapi,bpi-r4-lite)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3 lan4 sfp-lan" wan
		;;
	bananapi,bpi-r4-2g5|\
	bananapi,bpi-r4-poe)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3 lan4" "wan sfp-wan"
		;;
	comfast,cf-e393ax)
		ucidef_set_interfaces_lan_wan "lan1" eth1
		;;
	cudy,ap3000wall-v1)
		ucidef_set_interface_lan "lan1 lan2 lan3 lan4 lan5"
		;;
	cudy,ap3000outdoor-v1|\
	cudy,ap3000-v1|\
	cudy,re3000-v1|\
	kebidumei,ax3000-u22|\
	netgear,eax17|\
	netgear,wax220|\
	openfi,6c|\
	ubnt,unifi-6-plus|\
	wavlink,wl-wn573hx3|\
	widelantech,wap430x|\
	zyxel,nwa50ax-pro)
		ucidef_set_interface_lan "eth0"
		;;
	airpi,ap3000m|\
	bananapi,bpi-r3-mini|\
	edgecore,eap111|\
	huasifei,wh3000|\
	huasifei,wh3000-pro)
		ucidef_set_interfaces_lan_wan eth0 eth1
		;;
	bazis,ax3000wm|\
	cudy,m3000-v1|\
	cudy,tr3000-256mb-v1|\
	cudy,tr3000-v1|\
	cudy,tr3000-v1-ubootmod|\
	glinet,gl-mt2500|\
	glinet,gl-mt2500-airoha|\
	glinet,gl-mt3000|\
	glinet,gl-x3000|\
	glinet,gl-xe3000|\
	openembed,som7981|\
	openwrt,one)
		ucidef_set_interfaces_lan_wan eth1 eth0
		;;
	dlink,aquila-pro-ai-m30-a1|\
	dlink,aquila-pro-ai-m60-a1)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3 lan4" internet
		;;
	comfast,cf-wr632ax|\
	comfast,cf-wr632ax-ubootmod|\
	keenetic,kn-3911|\
	smartrg,sdg-8622|\
	smartrg,sdg-8632|\
	smartrg,sdg-8733a|\
	yuncore,ax835)
		ucidef_set_interfaces_lan_wan lan wan
		;;
	keenetic,kn-1812|\
	netcraze,nc-1812)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3 lan4 lan5" "wan"
		;;
	mediatek,mt7986a-rfb)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3 lan4 lan6" "eth1 wan"
		;;
	mediatek,mt7986b-rfb)
		ucidef_set_interfaces_lan_wan "lan0 lan1 lan2 lan3 lan4" eth1
		;;
	mediatek,mt7988a-rfb)
		ucidef_set_interfaces_lan_wan "lan0 lan1 lan2 lan3 eth2" eth1
		;;
	mercusys,mr85x)
		ucidef_add_switch "switch0" "0:lan0" "1:lan1" "2:lan2" "6@eth0"
		ucidef_set_interfaces_lan_wan "eth0.1 eth0.2 eth0.3" eth1
		;;
	mercusys,mr90x-v1|\
	mercusys,mr90x-v1-ubi)
		ucidef_set_interfaces_lan_wan "lan0 lan1 lan2" eth1
		;;
	tplink,fr365-v1)
		ucidef_set_interfaces_lan_wan "port1 port3 port4 port5 port6" "port2"
		;;
	tplink,tl-xdr6086|\
	wavlink,wl-wn551x3|\
	wavlink,wl-wn586x3|\
	wavlink,wl-wn586x3b)
		ucidef_set_interfaces_lan_wan "lan1 lan2" eth1
		;;
	tplink,archer-ax80-v1|\
	tplink,archer-ax80-v1-eu)
		ucidef_set_interfaces_lan_wan "lan0 lan1 lan2 lan3" eth1
		;;
	tplink,be450)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3 eth2" eth1
		;;
	tplink,re6000xd)
		ucidef_set_interface_lan "lan1 lan2 eth1"
		;;
	xiaomi,mi-router-ax3000t|\
	xiaomi,mi-router-ax3000t-ubootmod|\
	xiaomi,mi-router-wr30u-stock|\
	xiaomi,mi-router-wr30u-ubootmod|\
	xiaomi,redmi-router-ax6000-stock|\
	xiaomi,redmi-router-ax6000-ubootmod)
		ucidef_set_interfaces_lan_wan "lan2 lan3 lan4" wan
		;;
	*)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3 lan4" wan
		;;
	esac
}

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

	case $board in
	airpi,ap3000m)
		base_mac=$(macaddr_generate_from_mmc_cid mmcblk0)
		lan_mac="$base_mac"
		wan_mac="$(macaddr_add "$base_mac" 1)"
		;;
	acer,predator-w6|\
	acer,predator-w6d|\
	acer,vero-w6m)
		wan_mac=$(mmc_get_mac_ascii u-boot-env WANMAC)
		lan_mac=$(mmc_get_mac_ascii u-boot-env LANMAC)
		;;
	acer,predator-w6x-stock|\
	acer,predator-w6x-ubootmod)
		wan_mac=$(mtd_get_mac_ascii u-boot-env ethaddr)
		lan_mac=$(macaddr_add "$wan_mac" 1)
		label_mac=$wan_mac
		;;
	bananapi,bpi-r3|\
	bananapi,bpi-r3-mini|\
	bananapi,bpi-r4|\
	bananapi,bpi-r4-lite)
		wan_mac=$(macaddr_add $(cat /sys/class/net/eth0/address) 1)
		;;
	h3c,magic-nx30-pro)
		wan_mac=$(mtd_get_mac_ascii pdt_data_1 ethaddr)
		lan_mac=$(macaddr_add "$wan_mac" 1)
		label_mac=$wan_mac
		;;
	mercusys,mr80x-v3|\
	mercusys,mr85x)
		mac_dirty=$(cat "/tmp/tp_data/default-mac" | sed -n 's/^'"MAC"'://p')
		label_mac=$(macaddr_canonicalize "$mac_dirty")
		lan_mac=$label_mac
		wan_mac=$(macaddr_add "$lan_mac" 1)
		;;
	mercusys,mr90x-v1|\
	tplink,archer-ax80-v1|\
	tplink,archer-ax80-v1-eu|\
	tplink,re6000xd)
		label_mac=$(get_mac_binary "/tmp/tp_data/default-mac" 0)
		lan_mac=$label_mac
		;;
	netgear,wax220)
		lan_mac=$(mtd_get_mac_ascii u-boot-env mac)
		label_mac=$lan_mac
		;;
	nradio,c8-668gl)
		lan_mac=$(mmc_get_mac_ascii bdinfo "fac_mac ")
		wan_mac=$(macaddr_add "$lan_mac" 2)
		label_mac=$lan_mac
		;;
	qihoo,360t7)
		lan_mac=$(mtd_get_mac_ascii factory lanMac)
		wan_mac=$(macaddr_add "$lan_mac" 1)
		label_mac=$wan_mac
		;;
	ruijie,rg-x60-pro)
		label_mac=$(mtd_get_mac_ascii product_info ethaddr)
		wan_mac=$label_mac
		lan_mac=$(macaddr_add "$label_mac" 1)
		;;
	tplink,eap683-lr)
		label_mac=$(get_mac_binary "/tmp/factory/default-mac" 0)
		lan_mac=$label_mac
		;;
	tplink,fr365-v1)
		lan_mac=$(strings /dev/mtd11 | grep 'option macaddr' | awk -F"'" '{print $2}')
		wan_mac="$(macaddr_add $lan_mac 1)"
		;;
	xiaomi,mi-router-ax3000t|\
	xiaomi,mi-router-ax3000t-ubootmod|\
	xiaomi,mi-router-wr30u-stock|\
	xiaomi,mi-router-wr30u-ubootmod|\
	xiaomi,redmi-router-ax6000-stock|\
	xiaomi,redmi-router-ax6000-ubootmod)
		wan_mac=$(mtd_get_mac_ascii Bdata ethaddr_wan)
		label_mac=$wan_mac
		;;
	yuncore,ax835)
		label_mac=$(mtd_get_mac_binary "Factory" 0x4)
		;;
	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
board_config_flush

exit 0
