#!/bin/sh

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

ramips_setup_interfaces()
{
	local board="$1"

	case $board in
	asus,rt-n56u)
		ucidef_add_switch "switch0" \
			"0:lan" "1:lan" "2:lan" "3:lan" "4:wan" "8@eth0"
		;;
	belkin,f9k1109v1)
		ucidef_add_switch "switch0" \
			"0:lan" "1:lan" "2:lan" "3:lan" "4:wan" "5@eth0"
		;;
	dlink,dir-645)
		ucidef_add_switch "switch0" \
			"1:lan" "2:lan" "3:lan" "4:lan" "0:wan" "6@eth0"
		;;
	edimax,br-6475nd)
		ucidef_add_switch "switch0" \
			"1:lan" "2:lan" "3:lan" "4:lan" "0:wan" "9@eth0"
		;;
	engenius,esr600h|\
	sitecom,wlr-6000|\
	trendnet,tew-691gr|\
	trendnet,tew-692gr)
		ucidef_add_switch "switch0" \
			"1:lan" "2:lan" "3:lan" "4:lan" "5:wan" "0@eth0"
		;;
	loewe,wmdr-143n|\
	omnima,hpm)
		ucidef_set_interface_lan "eth0"
		;;
	samsung,cy-swr1100)
		ucidef_add_switch "switch0" \
			"0:lan" "1:lan" "2:lan" "3:lan" "4:wan" "9@eth0"
		;;
	esac
}

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

	case $board in
	asus,rt-n56u)
		lan_mac=$(macaddr_setbit_la "$(cat /sys/class/net/eth0/address)")
		wan_mac=$(mtd_get_mac_binary factory 0x8004)
		;;
	belkin,f9k1109v1)
		wan_mac=$(mtd_get_mac_ascii uboot-env HW_WAN_MAC)
		lan_mac=$(mtd_get_mac_ascii uboot-env HW_LAN_MAC)
		label_mac=$wan_mac
		;;
	dlink,dir-645)
		lan_mac=$(mtd_get_mac_ascii nvram lanmac)
		wan_mac=$(mtd_get_mac_ascii nvram wanmac)
		;;
	edimax,br-6475nd)
		wan_mac=$(mtd_get_mac_binary devdata 0x7)
		;;
	engenius,esr600h)
		wan_mac=$(mtd_get_mac_ascii u-boot-env wanaddr)
		lan_mac=$(macaddr_add "$wan_mac" 1)
		label_mac=$wan_mac
		;;
	samsung,cy-swr1100)
		lan_mac=$(mtd_get_mac_ascii nvram lanmac)
		wan_mac=$(mtd_get_mac_ascii nvram wanmac)
		label_mac=$wan_mac
		;;
	sitecom,wlr-6000)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary factory 0x8004)" 2)
		;;
	trendnet,tew-691gr)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary factory 0x4)" 3)
		;;
	trendnet,tew-692gr)
		wan_mac=$(macaddr_add "$(mtd_get_mac_binary factory 0x4)" 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
