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

ramips_setup_interfaces()
{
	local board="$1"

	case $board in
	alfa-network,awusfree1|\
	d-team,pbr-d1|\
	dlink,dap-1325-a1|\
	glinet,microuter-n300|\
	glinet,vixmini|\
	hak5,wifi-pineapple-mk7|\
	mediatek,linkit-smart-7688|\
	minew,g1-c|\
	onion,omega2p|\
	onion,omega2|\
	ravpower,rp-wd009|\
	tama,w06|\
	tplink,re200-v2|\
	tplink,re200-v3|\
	tplink,re200-v4|\
	tplink,re220-v2|\
	tplink,re305-v1|\
	tplink,tl-mr3020-v3|\
	tplink,tl-wr802n-v4|\
	tplink,tl-wa801nd-v5|\
	widora,neo-16m|\
	widora,neo-32m)
		ucidef_add_switch "switch0"
		ucidef_add_switch_attr "switch0" "enable" "false"
		ucidef_set_interface_lan "eth0"
		;;
	asus,rt-n10p-v3|\
	asus,rt-n11p-b1|\
	asus,rt-n12-vp-b1|\
	hiwifi,hc5661a|\
	mediatek,mt7628an-eval-board|\
	mercury,mac1200r-v2|\
	totolink,lr1200|\
	wavlink,wl-wn570ha1|\
	wavlink,wl-wn575a3)
		ucidef_add_switch "switch0" \
			"0:lan" "1:lan" "2:lan" "3:lan" "4:wan" "6@eth0"
		;;
	buffalo,wcr-1166ds|\
	elecom,wrc-1167fs|\
	wavlink,wl-wn577a2|\
	wavlink,wl-wn578a2)
		ucidef_add_switch "switch0" \
			"3:lan" "4:wan" "6@eth0"
		;;
	cudy,wr1000)
		ucidef_add_switch "switch0" \
			"2:lan:2" "3:lan:1" "4:wan" "6@eth0"
		;;
	duzun,dm06)
		ucidef_add_switch "switch0" \
			"1:lan" "0:wan" "6@eth0"
		;;
	glinet,gl-mt300n-v2)
		ucidef_add_switch "switch0" \
			"1:lan" "0:wan" "6@eth0"
		;;
	hilink,hlk-7628n|\
	hilink,hlk-7688a|\
	hiwifi,hc5861b|\
	skylab,skw92a|\
	tplink,archer-c20-v4|\
	tplink,archer-c20-v5|\
	tplink,archer-c50-v3|\
	tplink,archer-c50-v4|\
	tplink,tl-mr3420-v5|\
	tplink,tl-wr840n-v4|\
	tplink,tl-wr840n-v5|\
	tplink,tl-wr841n-v13|\
	tplink,tl-wr841n-v14|\
	tplink,tl-wr842n-v5|\
	tplink,tl-wr850n-v2|\
	unielec,u7628-01-16m|\
	wrtnode,wrtnode2p|\
	wrtnode,wrtnode2r|\
	zyxel,keenetic-extra-ii)
		ucidef_add_switch "switch0" \
			"1:lan" "2:lan" "3:lan" "4:lan" "0:wan" "6@eth0"
		;;
	hiwifi,hc5761a)
		ucidef_add_switch "switch0" \
			"0:lan" "1:lan" "4:wan" "6@eth0"
		;;
	iptime,a3|\
	totolink,a3)
		ucidef_add_switch "switch0" \
			"2:lan:2" "3:lan:1" "0:wan" "6@eth0"
		;;
	iptime,a604m)
		ucidef_add_switch "switch0" \
			"1:lan:4" "2:lan:3" "3:lan:2" "4:lan:1" "0:wan" "6@eth0"
		;;
	jotale,js76x8-8m|\
	jotale,js76x8-16m|\
	jotale,js76x8-32m)
		ucidef_add_switch "switch0" \
			"0:lan" "1:lan" "2:lan" "6@eth0"
		;;
	motorola,mwr03)
		ucidef_add_switch "switch0" \
			"1:lan" "2:lan" "3:lan" "0:wan" "6@eth0"
		;;
	netgear,r6020|\
	netgear,r6080|\
	netgear,r6120)
		ucidef_add_switch "switch0" \
			"0:lan:4" "1:lan:3" "2:lan:2" "3:lan:1" "4:wan" "6@eth0"
		;;
	rakwireless,rak633)
		ucidef_add_switch "switch0" \
			"0:wan" "1:lan" "2:lan" "3:lan" "4:lan" "6t@eth0"
		;;
	tplink,tl-mr6400-v4)
		ucidef_add_switch "switch0" \
			"1:lan" "2:lan" "3:lan" "4:wan" "6@eth0"
		;;
	tplink,tl-mr6400-v5)
		ucidef_add_switch "switch0" \
			"0:lan" "1:lan" "2:lan" "3:wan" "6@eth0"
		;;
	tplink,tl-wr902ac-v3)
		ucidef_add_switch "switch0" \
			"4:lan" "6@eth0"
		;;
	vocore,vocore2|\
	vocore,vocore2-lite)
		ucidef_add_switch "switch0" \
			"0:lan" "2:lan" "6t@eth0"
		;;
	wiznet,wizfi630s)
		ucidef_add_switch "switch0" \
			"0:wan" "3:lan" "4:lan" "6@eth0"
		;;
	xiaomi,mi-router-4a-100m)
		ucidef_add_switch "switch0" \
			"4:lan:1" "2:lan:2" "0:wan" "6@eth0"
		;;
	xiaomi,mi-router-4c)
		ucidef_add_switch "switch0" \
			"4:lan:1" "2:lan:2" "1:wan" "6@eth0"
		;;
	xiaomi,miwifi-nano)
		ucidef_add_switch "switch0" \
			"0:lan:2" "2:lan:1" "4:wan" "6@eth0"
		;;
	zbtlink,zbt-we1226)
		ucidef_add_switch "switch0" \
			"0:lan:2" "1:lan:1" "4:wan" "6@eth0"
		;;
	esac
}

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

	case $board in
	buffalo,wcr-1166ds)
		local index="$(find_mtd_index "board_data")"
		wan_mac="$(grep -m1 mac= "/dev/mtd${index}" | cut -d= -f2)"
		lan_mac=$wan_mac
		;;
	cudy,wr1000|\
	hilink,hlk-7628n|\
	hilink,hlk-7688a|\
	wavlink,wl-wn577a2|\
	wavlink,wl-wn578a2)
		wan_mac=$(mtd_get_mac_binary factory 0x2e)
		label_mac=$(mtd_get_mac_binary factory 0x4)
		;;
	duzun,dm06|\
	netgear,r6020|\
	netgear,r6080|\
	netgear,r6120|\
	wrtnode,wrtnode2p|\
	wrtnode,wrtnode2r|\
	zyxel,keenetic-extra-ii)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary factory 0x4)" 1)
		;;
	elecom,wrc-1167fs)
		wan_mac=$(mtd_get_mac_binary factory 0x22)
		label_mac=$wan_mac
		;;
	hiwifi,hc5661a|\
	hiwifi,hc5761a|\
	hiwifi,hc5861b)
		lan_mac=$(mtd_get_mac_ascii bdinfo "Vfac_mac ")
		label_mac=$lan_mac
		[ -n "$lan_mac" ] || lan_mac=$(cat /sys/class/net/eth0/address)
		wan_mac=$(macaddr_add "$lan_mac" 1)
		;;
	iptime,a3|\
	iptime,a604m|\
	totolink,a3)
		wan_mac=$(mtd_get_mac_binary u-boot 0x1fc40)
		;;
	jotale,js76x8-8m|\
	jotale,js76x8-16m|\
	jotale,js76x8-32m|\
	skylab,skw92a|\
	totolink,lr1200)
		wan_mac=$(mtd_get_mac_binary factory 0x2e)
		;;
	mercury,mac1200r-v2)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary factory_info 0xd)" 1)
		;;
	motorola,mwr03)
		label_mac=$(mtd_get_mac_binary factory 0x4)
		wan_mac=$(macaddr_add "$label_mac" 2)
		;;
	onion,omega2|\
	onion,omega2p|\
	vocore,vocore2|\
	vocore,vocore2-lite)
		label_mac=$(mtd_get_mac_binary factory 0x4)
		;;
	rakwireless,rak633|\
	unielec,u7628-01-16m|\
	wavlink,wl-wn575a3)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary factory 0x28)" 1)
		;;
	tplink,archer-c20-v4|\
	tplink,archer-c50-v3|\
	tplink,tl-mr3420-v5|\
	tplink,tl-wr840n-v4|\
	tplink,tl-wr840n-v5|\
	tplink,tl-wr841n-v13|\
	tplink,tl-wr841n-v14|\
	tplink,tl-wr842n-v5|\
	tplink,tl-wr850n-v2)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary factory 0xf100)" 1)
		;;
	tplink,archer-c20-v5|\
	tplink,archer-c50-v4)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary rom 0xf100)" 1)
		;;
	wavlink,wl-wn570ha1|\
	zbtlink,zbt-we1226)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary factory 0x2e)" 1)
		;;
	wiznet,wizfi630s)
		label_mac=$(mtd_get_mac_binary factory 0x4)
		wan_mac=$(mtd_get_mac_binary factory 0x28)
		;;
	xiaomi,mi-router-4a-100m|\
	xiaomi,mi-router-4c)
		wan_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)
ramips_setup_interfaces $board
ramips_setup_macs $board
board_config_flush

exit 0
