#!/bin/sh
# Alkane network bring-up (all boards): eth0 via DHCP, brought up early (S10) so
# SSH, the liveness surface, and afw verify-live are reachable. eth0 is the
# single wired NIC on every supported control board (TI CPSW, Meson dwmac,
# Cadence GEM, Allwinner EMAC). udhcpc runs backgrounded (-b) and renews the
# lease, so the address persists for the life of the boot.
INTERFACE=eth0

case "$1" in
  start)
    printf 'Starting network %s: ' "$INTERFACE"
    ip link set "$INTERFACE" up 2>/dev/null || { echo "FAIL (no $INTERFACE)"; exit 0; }
    if command -v udhcpc >/dev/null 2>&1; then
      udhcpc -i "$INTERFACE" -b -R -p /var/run/udhcpc.$INTERFACE.pid \
        -s /etc/udhcpc.script >/dev/null 2>&1
    fi
    echo "OK"
    ;;
  stop)
    printf 'Stopping network %s: ' "$INTERFACE"
    if [ -f /var/run/udhcpc.$INTERFACE.pid ]; then
      kill "$(cat /var/run/udhcpc.$INTERFACE.pid)" 2>/dev/null
      rm -f /var/run/udhcpc.$INTERFACE.pid
    fi
    ip link set "$INTERFACE" down 2>/dev/null
    echo "OK"
    ;;
  restart)
    "$0" stop
    sleep 1
    "$0" start
    ;;
  *)
    echo "usage: $0 {start|stop|restart}"
    exit 1
    ;;
esac
