#
# Copyright (C) 2011-2015 OpenWrt.org
#

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

lantiq_setup_interfaces()
{
	local board="$1"

	case "$board" in
	arcadyan,arv4510pw)
		ucidef_add_switch "switch0" \
			"0:lan:4" "2:lan:2" "1:lan:3" "3:lan:1" "5t@eth0"
		;;
	arcadyan,arv4519pw|\
	arcadyan,arv7510pw22|\
	arcadyan,arv7518pw|\
	arcadyan,arv752dpw22|\
	arcadyan,arv8539pw22|\
	buffalo,wbmr-hp-g300h)
		ucidef_add_switch "switch0" \
			"0t@eth0" "2:lan" "3:lan" "4:lan" "5:lan"
		;;
	arcadyan,arv7506pw11|\
	audiocodes,mp-252|\
	siemens,gigaset-sx76x)
		ucidef_add_switch "switch0" \
			"1:lan:4" "2:lan:3" "3:lan:2" "4:lan:1" "5t@eth0"
		;;
	arcadyan,arv7519pw|\
	zte,h201l)
		ucidef_add_switch "switch0" \
			"0:lan" "1:lan" "2:lan" "3:lan" "4t@eth0"
		;;
	arcadyan,arv752dpw|\
	bt,homehub-v2b)
		ucidef_add_switch "switch0" \
			"1:lan" "2:lan" "3:lan" "4:lan" "5t@eth0"
		;;
	netgear,dgn3500|\
	netgear,dgn3500b)
		ucidef_add_switch "switch0" \
			"0:lan:4" "1:lan:3" "2:lan:2" "3:lan:1" "5t@eth0"
		;;
	zyxel,p-2601hn)
		ucidef_add_switch "switch0" \
			"0:lan" "1:lan" "2:lan" "3:lan" "5t@eth0"
		;;
	*)
		ucidef_set_interface_lan 'eth0'
		;;
	esac
}

lantiq_setup_dsl()
{
	local board="$1"
	local annex="a"

	case "$board" in
	arcadyan,arv7506pw11|\
	arcadyan,arv7525pw|\
	arcadyan,arv752dpw|\
	arcadyan,arv752dpw22|\
	arcadyan,arv8539pw22|\
	avm,fritz7312|\
	avm,fritz7320|\
	siemens,gigaset-sx76x|\
	zte,h201l)
		annex="b"
		;;
	esac

	lantiq_setup_dsl_helper "$annex"
}

lantiq_setup_macs()
{
	local board="$1"
	local lan_mac=""
	local wan_mac=""

	case "$board" in
	arcadyan,arv4510pw|\
	bt,homehub-v2b|\
	bt,homehub-v3a|\
	netgear,dgn3500|\
	netgear,dgn3500b)
		lan_mac=$(mtd_get_mac_ascii u-boot-env ethaddr)
		wan_mac=$(macaddr_add "$lan_mac" 1)
		;;
	arcadyan,arv7506pw11)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary board_config 0x16)" 2)
		;;
	arcadyan,arv7519pw)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary board_config 0x16)" 1)
		;;
	avm,fritz7312|\
	avm,fritz7320)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary urlader 0xa91)" 1)
		;;
	esac

	[ -n "$lan_mac" ] && ucidef_set_interface_macaddr "lan" "$lan_mac"
	[ -n "$wan_mac" ] && ucidef_set_interface_macaddr "wan" "$wan_mac"
}

board_config_update
board=$(board_name)
lantiq_setup_interfaces $board
lantiq_setup_dsl $board
lantiq_setup_macs $board
board_config_flush

exit 0
