#!/bin/bash
# Regenerates the netboot configuration (dnsmasq.conf, pxelinux.cfg/default and grub-custom.efi)
# when ifupdown brings an interface up. Installed as /etc/network/if-up.d/filewave-ivs.
#
# The NetworkManager dispatcher hook (10-dhclient-up-down.sh) does not cover eth0: the base image's
# installer and "imaging-control networksetup" configure it in /etc/network/interfaces, and
# NetworkManager leaves those interfaces to ifupdown ([ifupdown] managed=false).

# Overridable so the unit test does not run the real renderer
RENDERER="${RENDERER:-/imaging/scripts/bin/dhclient-up-down-hooks-debian}"
BANNER="${BANNER:-/imaging/scripts/bin/getty-script.sh}"

# Once per interface: not for lo, the IPv6 stanzas (the renderer reads the IPv4 address) or the
# extra pass "ifup -a" makes at the end (IFACE=--all, ADDRFAM=meta)
if [ "$MODE" != start ] || [ "$ADDRFAM" != inet ] || [ "$IFACE" = lo ]; then
    exit 0
fi

# Not inline: the renderer restarts dnsmasq, which is ordered after network-online.target, so
# waiting for it from networking.service at boot would hang until networking times out
systemd-run --no-block --collect --setenv=reason=BOUND "$RENDERER" ||
    echo "filewave-ivs: could not start $RENDERER for $IFACE" >&2

# The console banner shows the address. With DHCP the dhcpcd exit hook refreshes it too, but a
# static address from "imaging-control networksetup" only goes through here (FWRD-18749)
"$BANNER" --reload

# A failing hook would make ifup, and networking.service, report a failure
exit 0
