#!/bin/sh

if [ -f "/sys/class/net/eth0/address" ]; then
    ETH0MAC=$(cat /sys/class/net/eth0/address)
fi
if [ -z "$ETH0MAC" ]; then
    echo "Could not find mac address for eth0" >&2
    exit 1
fi

if [ -f "/sys/class/net/eth1/address" ]; then
    ETH1MAC=$(cat /sys/class/net/eth1/address)
fi
if [ -z "$ETH1MAC" ]; then
    echo "Could not find mac address for eth1" >&2
fi

if [ ! -f "/etc/systemd/network/10-green0.link" ]; then
    echo "Missing /etc/systemd/network/10-green0.link" >&2
    exit 1
fi

if [ ! -f "/etc/systemd/network/20-red0.link" ]; then
    echo "Missing /etc/systemd/network/20-red0.link" >&2
    exit 1
fi

sed -i "s/MACAddress=.*/MACAddress=${ETH0MAC}/" \
    /etc/systemd/network/10-green0.link

sed -i "s/MACAddress=.*/MACAddress=${ETH1MAC}/" \
    /etc/systemd/network/20-red0.link
