#!/bin/sh

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

ramips_setup_interfaces()
{
	local board="$1"

	case $board in
	7links,px-4885-4m|\
	7links,px-4885-8m|\
	allnet,all0256n-4m|\
	allnet,all0256n-8m|\
	allnet,all5002|\
	allnet,all5003|\
	belkin,f7c027|\
	dlink,dcs-930l-b1|\
	dlink,dcs-930|\
	edimax,3g-6200nl|\
	hame,mpr-a1|\
	hame,mpr-a2|\
	hauppauge,broadway|\
	hootoo,ht-tm02|\
	huawei,d105|\
	intenso,memory2move|\
	netgear,wnce2001|\
	tenda,3g150b|\
	tenda,3g300m|\
	tenda,w150m|\
	trendnet,tew-714tru|\
	unbranded,a5-v11|\
	wansview,ncs601w|\
	zorlik,zl5900v2)
		ucidef_add_switch "switch0"
		ucidef_add_switch_attr "switch0" "enable" "false"
		ucidef_set_interface_lan "eth0"
		;;
	8devices,carambola)
		ucidef_add_switch "switch0" \
			"0:lan" "1:lan" "6@eth0"
		;;
	accton,wr6202|\
	alfa-network,w502u|\
	argus,atp-52b|\
	asiarf,awm002-evb-4m|\
	asiarf,awm002-evb-8m|\
	asus,rt-n10-plus|\
	asus,wl-330n|\
	asus,wl-330n3g|\
	aztech,hw550-3g|\
	engenius,esr-9753|\
	jcg,jhr-n805r|\
	jcg,jhr-n825r|\
	jcg,jhr-n926r|\
	petatel,psr-680w|\
	planex,mzk-wdpr|\
	skyline,sl-r7205|\
	teltonika,rut5xx|\
	tenda,w306r-v2|\
	unbranded,xdx-rn502j|\
	upvel,ur-326n4g)
		ucidef_add_switch "switch0" \
			"1:lan" "2:lan" "3:lan" "4:lan" "0:wan" "6@eth0"
		;;
	airlive,air3gii|\
	asus,rt-g32-b1|\
	asus,rt-n13u|\
	aximcom,mr-102n|\
	buffalo,whr-g300n|\
	dlink,dap-1350|\
	dlink,dir-300-b1|\
	dlink,dir-300-b7|\
	dlink,dir-320-b1|\
	dlink,dir-600-b1|\
	dlink,dir-610-a1|\
	dlink,dir-615-d|\
	dlink,dir-620-a1|\
	dlink,dir-620-d1|\
	dlink,dwr-512-b|\
	easyacc,wizard-8800|\
	edimax,3g-6200n|\
	fon,fonera-20n|\
	hilink,hlk-rm04|\
	mofinetwork,mofi3500-3gn|\
	netcore,nw718|\
	nexaira,bc2|\
	nixcore,x1-16m|\
	nixcore,x1-8m|\
	olimex,rt5350f-olinuxino|\
	olimex,rt5350f-olinuxino-evb|\
	omnima,miniembplug|\
	omnima,miniembwifi|\
	planex,mzk-w300nh2|\
	poray,ip2202|\
	poray,m3|\
	poray,m4-4m|\
	poray,m4-8m|\
	poray,x5|\
	poray,x8|\
	prolink,pwh2004|\
	ralink,v22rw-2x2|\
	unbranded,wr512-3gn-4m|\
	unbranded,wr512-3gn-8m|\
	upvel,ur-336un|\
	zyxel,keenetic|\
	zyxel,nbg-419n|\
	zyxel,nbg-419n-v2)
		ucidef_add_switch "switch0" \
			"0:lan" "1:lan" "2:lan" "3:lan" "4:wan" "6@eth0"
		;;
	alphanetworks,asl26555-8m|\
	alphanetworks,asl26555-16m)
		ucidef_add_switch "switch0" \
			"1:lan" "2:lan" "3:lan" "4:lan" "6t@eth0"
		;;
	arcwireless,freestation5)
		# FIXME: Which is the actual wan port?
		ucidef_add_switch "switch0" \
			"0:lan" "1:wan" "2:wan" "3:wan" "4:wan" "6@eth0"
		;;
	asiarf,awapn2403)
		ucidef_add_switch "switch0" \
			"0:lan" "1:wan" "6@eth0"
		;;
	aximcom,mr-102n|\
	trendnet,tew-638apb-v2)
		ucidef_add_switch "switch0" \
			"4:lan" "6@eth0"
		;;
	belkin,f5d8235-v2)
		ucidef_add_switch "switch0" \
			"1:lan" "2:lan" "3:lan" "4:lan" "0:wan" "5@eth0"
		;;
	dlink,dir-615-h1)
		ucidef_add_switch "switch0" \
			"0:lan:4" "1:lan:3" "2:lan:2" "3:lan:1" "4:wan:5" "6@eth0"
		;;
	huawei,hg255d)
		ucidef_add_switch "switch0" \
			"1:lan:4" "2:lan:3" "3:lan:2" "4:lan:1" "0:wan" "6@eth0"
		;;
	nexx,wt1520-4m|\
	nexx,wt1520-8m)
		ucidef_add_switch "switch0" \
			"0:lan" "4:wan" "6@eth0"
		;;
	planex,mzk-dp150n|\
	vocore,vocore-8m|\
	vocore,vocore-16m)
		ucidef_add_switch "switch0" \
			"0:lan" "4:lan" "6t@eth0"
		;;
	sitecom,wl-351)
		ucidef_add_switch "switch0" \
			"0:lan" "1:lan" "2:lan" "3:lan" "4:wan" "5@eth0"
		;;
	sparklan,wcr-150gn)
		ucidef_add_switch "switch0" \
			"0:lan" "6t@eth0"
		;;
	wiznet,wizfi630a)
		ucidef_add_switch "switch0" \
			"0:lan" "1:lan" "2:wan" "6@eth0"
		;;
	zte,mf283plus)
		ucidef_add_switch "switch0" \
			"0:lan" "1:lan" "2:lan" "3:lan" "6@eth0"
		;;
	zyxel,keenetic-lite-b|\
	zyxel,keenetic-start)
		ucidef_add_switch "switch0" \
			"0:lan:3" "1:lan:2" "2:lan:1" "3:lan:0" "4:wan" "6@eth0"
		;;
	esac
}

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

	case $board in
	7links,px-4885-4m|\
	7links,px-4885-8m)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary devconf 0x28)" 1)
		;;
	8devices,carambola|\
	alfa-network,w502u|\
	arcwireless,freestation5|\
	netgear,wnce2001)
		wan_mac=$(mtd_get_mac_binary factory 0x2e)
		;;
	accton,wr6202|\
	asiarf,awm002-evb-4m|\
	asiarf,awm002-evb-8m|\
	asus,rt-n13u|\
	aztech,hw550-3g|\
	fon,fonera-20n|\
	huawei,hg255d|\
	omnima,miniembwifi|\
	planex,mzk-wdpr|\
	poray,ip2202|\
	teltonika,rut5xx|\
	unbranded,xdx-rn502j|\
	zyxel,keenetic|\
	zyxel,nbg-419n|\
	zyxel,nbg-419n-v2)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary factory 0x28)" 1)
		;;
	airlive,air3gii|\
	argus,atp-52b|\
	asus,wl-330n3g|\
	dlink,dir-620-d1|\
	edimax,3g-6200n|\
	edimax,3g-6200nl|\
	netcore,nw718|\
	nexx,wt1520-4m|\
	nexx,wt1520-8m|\
	nixcore,x1-16m|\
	nixcore,x1-8m|\
	omnima,miniembplug|\
	planex,mzk-w300nh2|\
	sitecom,wl-351|\
	trendnet,tew-714tru)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary factory 0x4)" 1)
		;;
	asus,rt-g32-b1|\
	asus,rt-n10-plus)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary devconf 0x4)" 1)
		;;
	belkin,f5d8235-v2)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary uboot 0x40004)" 1)
		;;
	dlink,dir-300-b7|\
	dlink,dir-320-b1|\
	dlink,dir-620-a1|\
	engenius,esr-9753|\
	hame,mpr-a1|\
	hauppauge,broadway|\
	huawei,d105|\
	hilink,hlk-rm04|\
	nexaira,bc2|\
	olimex,rt5350f-olinuxino|\
	olimex,rt5350f-olinuxino-evb|\
	petatel,psr-680w|\
	skyline,sl-r7205)
		lan_mac=$(macaddr_setbit_la "$(cat /sys/class/net/eth0/address)")
		wan_mac=$(macaddr_add "$lan_mac" 1)
		;;
	dlink,dap-1350)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary devdata 0x2e)" 1)
		;;
	dlink,dir-300-b1|\
	dlink,dir-600-b1|\
	dlink,dir-610-a1)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary devdata 0x4004)" 1)
		;;
	dlink,dir-615-d)
		label_mac=$(mtd_get_mac_binary devdata 0x4004)
		;;
	dlink,dir-615-h1)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary factory 0x28)" 1)
		label_mac=$(mtd_get_mac_binary factory 0x4)
		;;
	dlink,dwr-512-b)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary config 0xe07e)" 1)
		;;
	jcg,jhr-n805r|\
	jcg,jhr-n825r|\
	jcg,jhr-n926r)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary factory 0x2e)" 1)
		;;
	poray,m3|\
	poray,m4-4m|\
	poray,m4-8m|\
	poray,x5|\
	poray,x8)
		lan_mac=$(macaddr_add "$(mtd_get_mac_binary factory 0x4)" -2)
		;;
	sparklan,wcr-150gn|\
	wiznet,wizfi630a)
		wan_mac=$(mtd_get_mac_binary factory 0x28)
		;;
	tenda,w306r-v2)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary factory 0x28)" 5)
		;;
	upvel,ur-326n4g|\
	upvel,ur-336un)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary factory 0x4004)" 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)
ramips_setup_interfaces $board
ramips_setup_macs $board
board_config_flush

exit 0
