#!/bin/bash
# Point the EN8811H port LEDs at their own interface. The PHY offloads both
# rules in hardware, so the yellow one blinks on traffic and the green one
# stays lit at gigabit and above without any CPU involvement.
set -u

configure_leds() {
local ifpath ifname leds led found=1
for ifpath in /sys/class/net/*; do
ifname="${ifpath##*/}"
leds="${ifpath}/phydev/leds"
[[ -d ${leds} ]] || continue
found=0
for led in "${leds}"/*; do
[[ -d ${led} ]] || continue
echo netdev > "${led}/trigger" 2>/dev/null || continue
echo "${ifname}" > "${led}/device_name"
case "${led##*/}" in
*:yellow:*)
echo 1 > "${led}/link"
echo 1 > "${led}/tx"
echo 1 > "${led}/rx"
;;
*:green:*)
echo 1 > "${led}/link_1000"
echo 1 > "${led}/link_2500"
;;
esac
done
done
return ${found}
}

# the phy only attaches when the interface is opened, so give it a moment
for _ in $(seq 30); do
configure_leds && exit 0
sleep 1
done
exit 0
