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

# Ideally, we should use OpenWrt label-mac-device alias in device tree
# but for some platforms with legacy drivers this has not been implemented.
# For those platforms, use fw_printenv

set_label_macaddr() {
	local macaddr
	macaddr="$(macaddr_canonicalize "$(fw_printenv -n ethaddr)")"
	[ -n "$macaddr" ] || return
	ucidef_set_label_macaddr "$macaddr"
}

mac_addr="$(get_mac_label)"
if [ -n "$mac_addr" ]; then
	# do not write MAC to board.json if OpenWrt get_mac_label already has one
	exit 0
fi
board_config_update
set_label_macaddr || exit 0
board_config_flush