#
# 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
	alphanetworks,asl56026|\
	arcadyan,vg3503j)
		ucidef_set_interface_lan "lan1 lan2"
		;;
	arcadyan,arv7519rw22)
		ucidef_set_interface_lan "lan1 lan2 lan3 lan4 lan5"
		;;
	avm,fritz5490|\
	avm,fritz5490-micron)
		ucidef_set_interfaces_lan_wan "lan1 lan2" "wan"
		;;
	arcadyan,vgv7510kw22-brn|\
	arcadyan,vgv7510kw22-nor|\
	arcadyan,vgv7519-brn|\
	arcadyan,vgv7519-nor|\
	arcadyan,vrv9510kwac23|\
	bt,homehub-v5a|\
	lantiq,easy80920-nand|\
	lantiq,easy80920-nor|\
	zyxel,p-2812hnu-f1|\
	zyxel,p-2812hnu-f3)
		ucidef_set_interfaces_lan_wan "lan1 lan2 lan3 lan4" "wan"
		;;
	avm,fritz3370-rev2-hynix|\
	avm,fritz3370-rev2-micron|\
	avm,fritz3390|\
	avm,fritz3490|\
	avm,fritz3490-micron|\
	avm,fritz7360sl|\
	avm,fritz7360-v2|\
	avm,fritz7362sl|\
	avm,fritz7430|\
	avm,fritz7490|\
	avm,fritz7490-micron|\
	buffalo,wbmr-300hpd|\
	tplink,tdw8970|\
	tplink,tdw8980|\
	tplink,vr200|\
	tplink,vr200v)
		ucidef_set_interface_lan "lan1 lan2 lan3 lan4"
		;;
	avm,fritz7412|\
	netgear,dm200)
		ucidef_set_interface_lan "lan"
		;;
	*)
		ucidef_set_interface_lan 'eth0'
		;;
	esac
}

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

	case "$board" in
	arcadyan,vgv7510kw22-brn|\
	arcadyan,vgv7510kw22-nor|\
	avm,fritz3370-rev2-hynix|\
	avm,fritz3370-rev2-micron|\
	avm,fritz3390|\
	avm,fritz3490|\
	avm,fritz3490-micron|\
	avm,fritz7360sl|\
	avm,fritz7362sl|\
	avm,fritz7412|\
	avm,fritz7430|\
	avm,fritz7490|\
	avm,fritz7490-micron)
		annex="b"
		;;
	esac

	lantiq_setup_dsl_helper "$annex"
}

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

	case "$board" in
	alphanetworks,asl56026)
		lan_mac=$(mtd_get_mac_ascii u-boot-env ethaddr)
		wan_mac=$(mtd_get_mac_ascii u-boot-env wanmac)
		;;
	arcadyan,arv7519rw22)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary boardconfig 0x16)" 1)
		;;
	arcadyan,vg3503j|\
	lantiq,easy80920-nand|\
	lantiq,easy80920-nor|\
	zyxel,p-2812hnu-f1|\
	zyxel,p-2812hnu-f3)
		lan_mac=$(mtd_get_mac_ascii u-boot-env ethaddr)
		wan_mac=$(macaddr_add "$lan_mac" 1)
		;;
	arcadyan,vgv7510kw22-brn|\
	arcadyan,vgv7510kw22-nor)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary board_config 0x16)" 2)
		;;
	arcadyan,vgv7519-brn|\
	arcadyan,vgv7519-nor|\
	arcadyan,vrv9510kwac23)
		wan_mac=$(mtd_get_mac_binary board_config 0x16)
		;;
	avm,fritz3370-rev2-hynix|\
	avm,fritz3370-rev2-micron)
		lan_mac=$(fritz_tffs -n maca -i $(find_mtd_part "tffs (1)"))
		wan_mac=$(macaddr_add "$lan_mac" 3)
		;;
	avm,fritz7360sl)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary urlader 0xa91)" 1)
		;;
	avm,fritz3390|\
	avm,fritz3490|\
	avm,fritz3490-micron|\
	avm,fritz5490|\
	avm,fritz5490-micron|\
	avm,fritz7362sl|\
	avm,fritz7490|\
	avm,fritz7490-micron)
		lan_mac=$(fritz_tffs -n maca -i $(find_mtd_part "tffs (1)"))
		wan_mac=$(fritz_tffs -n macdsl -i $(find_mtd_part "tffs (1)"))
		;;
	avm,fritz7412|\
	avm,fritz7430)
		tffsdev=$(find_mtd_chardev "nand-tffs")
		lan_mac=$(/usr/bin/fritz_tffs_nand -d $tffsdev -n maca -o)
		wan_mac=$(/usr/bin/fritz_tffs_nand -d $tffsdev -n macdsl -o)
		;;
	bt,homehub-v5a)
		lan_mac=$(mtd_get_mac_binary_ubi caldata 0x110c)
		wan_mac=$(macaddr_add "$lan_mac" 1)
		;;
	buffalo,wbmr-300hpd)
		lan_mac=$(mtd_get_mac_ascii u-boot-env ethaddr)
		wan_mac="$lan_mac"
		;;
	netgear,dm200)
		lan_mac=$(mtd_get_mac_binary ART 0x0)
		wan_mac=$(macaddr_add "$lan_mac" 1)
		;;
	tplink,tdw8970|\
	tplink,tdw8980)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary boardconfig 0xf100)" 1)
		;;
	tplink,vr200|\
	tplink,vr200v)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary romfile 0xf100)" 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
