#!/bin/bash

mountpoint -q /etc/iris
if [ $? -ne 0 ]; then
    echo "[SKLK] The iris config is not mounted!"
    exit 1
fi

#setup u-boot-tools for this board
echo "/dev/mtd1 0x0 0x80000 0x80000" > /etc/fw_env.config

#extract the mac from the mounted partition
ETH0=$(sklk_config_parse network:eth0)

#extract the mac from the current fw env
FW_ETHADDR=$(fw_printenv -n ethaddr)

if [ "${ETH0}" = "${FW_ETHADDR}" ]; then
    echo "[SKLK] Firmware mac address matches iris config."
    exit 0
fi

echo "[SKLK] Loading ${ETH0} into firmware env..."

#also apply the ether with ifconfig
#this could be busy depending upon when the script is run
ifconfig eth0 hw ether ${ETH0}

#create file to import into the fw env
FW_IMPORTS=/tmp/fw_imports.txt
echo "ethaddr ${ETH0}" > ${FW_IMPORTS}
echo "bootcmd fatload mmc 0 0x10000000 image.ub && bootm 0x10000000" >> ${FW_IMPORTS}
echo "bootdelay 0" >> ${FW_IMPORTS}

#perform import
fw_setenv -s ${FW_IMPORTS}
