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

rockchip_setup_interfaces()
{
	local board="$1"

	case "$board" in
	cyber,cyber3588-aib|\
	armsom,sige7|\
	easepi,ars4|\
	lunzn,fastrhino-r66s|\
	friendlyarm,nanopi-r2c|\
	friendlyarm,nanopi-r2c-plus|\
	friendlyarm,nanopi-r2s|\
	friendlyarm,nanopi-r3s|\
	friendlyarm,nanopi-r4s|\
	friendlyarm,nanopi-r4se|\
	friendlyarm,nanopi-r4s-enterprise|\
	friendlyarm,nanopi-r6c|\
	friendlyarm,nanopi-r76s|\
	hinlink,opc-h66k|\
	hinlink,h88k-v2|\
	hinlink,h88k|\
	hinlink,hnas|\
	radxa,e52c|\
	radxa,rockpi-e|\
	radxa,rock-5t|\
	xunlong,orangepi-5-plus|\
	xunlong,orangepi-r1-plus|\
	xunlong,orangepi-r1-plus-lts)
		ucidef_set_interfaces_lan_wan 'eth1' 'eth0'
		;;
	friendlyarm,nanopc-t6|\
	friendlyarm,nanopi-r5c|\
	friendlyelec,nanopi-r5s-c1|\
	hlink,h28k|\
	radxa,e20c|\
	radxa,e25|\
	radxa,rock-3b)
		ucidef_set_interfaces_lan_wan 'eth0' 'eth1'
		;;
	hinlink,h88k-v3|\
	hinlink,opc-h69k|\
	friendlyarm,nanopi-r5s)
		ucidef_set_interfaces_lan_wan 'eth1 eth2' 'eth0'
		;;
	friendlyarm,nanopi-r6s)
		ucidef_set_interfaces_lan_wan 'eth0 eth2' 'eth1'
		;;
	hinlink,opc-h68k|\
	lunzn,fastrhino-r68s|\
	lyt,t68m|\
	lyt,t88m)
		ucidef_set_interfaces_lan_wan 'eth1 eth2 eth3' 'eth0'
		;;
	roceos,k60pro)
		ucidef_set_interfaces_lan_wan 'eth1 eth2 eth3 eth4 eth5' 'eth0'
		;;
	easepi,r1|easepi,r1-lite|\
	linkease,easepi-r1)
		ucidef_set_network_device_path eth2 'platform/3c0400000.pcie/pci0001:10/0001:10:00.0/0001:11:00.0'
		ucidef_set_network_device_path eth3 'platform/3c0000000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0'
		ucidef_set_interfaces_lan_wan 'eth1 eth2 eth3' 'eth0'
		;;
	sinovoip,rk3568-bpi-r2pro)
		ucidef_set_interfaces_lan_wan 'lan0 lan1 lan2 lan3' 'eth0'
		;;
	radxa,e24c)
		ucidef_add_switch "switch0" \
			"0:wan" "1:lan" "2:lan" "3:lan" "6t@eth0"
		;;
	radxa,e54c)
		ucidef_add_switch "switch0" \
			"3:wan" "2:lan" "1:lan" "0:lan" "6t@eth0"
		;;
	*)
		ucidef_set_interface_lan 'eth0' 'dhcp'
		;;
	esac
}

generate_mac_from_boot_mmc()
{
	local sd_hash
	local bootdisk=$(
		. /lib/upgrade/common.sh
		export_bootdevice && export_partdevice bootdisk 0 && echo $bootdisk
	)
	if echo "$bootdisk" | grep -q '^mmcblk' && [ -f "/sys/class/block/$bootdisk/device/cid" ]; then
		macaddr_generate_from_mmc_cid $bootdisk
	else
		macaddr_generate_from_mmc_cid mmcblk*
	fi
}

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

	case "$board" in
	armsom,sige7|\
	friendlyarm,nanopc-t6|\
	friendlyarm,nanopi-r2c|\
	friendlyarm,nanopi-r2s|\
	linkease,easepi-r1|\
	lunzn,fastrhino-r66s|\
	lunzn,fastrhino-r68s)
		wan_mac=$(macaddr_generate_from_mmc_cid mmcblk0)
		lan_mac=$(macaddr_add "$wan_mac" 1)
		;;
	friendlyarm,nanopi-r2c-plus|\
	friendlyarm,nanopi-r4s|\
	sinovoip,rk3568-bpi-r2pro)
		wan_mac=$(macaddr_generate_from_mmc_cid mmcblk1)
		lan_mac=$(macaddr_add "$wan_mac" 1)
		;;
	friendlyarm,nanopi-r4s-enterprise)
		wan_mac=$(get_mac_binary "/sys/bus/i2c/devices/2-0051/eeprom" 0xfa)
		lan_mac=$(macaddr_setbit_la "$wan_mac")
		;;
	xunlong,orangepi-r1-plus|\
	xunlong,orangepi-r1-plus-lts)
		wan_mac=$(macaddr_add "$(cat /sys/class/net/eth1/address)" -1)
		;;
	*)
		if json_is_a network object; then
			json_select_object network
			if json_is_a wan object; then
				wan_mac=$(generate_mac_from_boot_mmc)
			fi
			if json_is_a lan object; then
				if [ -n "$wan_mac" ]; then
					lan_mac=$(macaddr_add "$wan_mac" 1)
				else
					lan_mac=$(generate_mac_from_boot_mmc)
				fi
			fi
			json_select ..
		fi
		;;
	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)
rockchip_setup_interfaces $board
rockchip_setup_macs $board
board_config_flush

exit 0
