#!/bin/sh
#
# Regenerate UniFi PXE override for netboot.xyz - with BIOS and UEFI support
# Run at boot using on-boot-script-2.x
# (see https://github.com/unifi-utilities/unifios-utilities/tree/main/on-boot-script-2.x)
# for Ubiquiti UCG Fiber
#

OUT="/run/dnsmasq.dhcp.conf.d/99-pxe-netbootxyz.conf"

# Wait until UniFi has generated DHCP configs
i=0
while [ ! -d /run/dnsmasq.dhcp.conf.d ] && [ $i -lt 30 ]; do
  sleep 1
  i=$((i+1))
done

cat > "$OUT" << 'EOF'
# ==========================================================
# 99-pxe-netbootxyz.conf
# UniFi-safe PXE override for MY_LAN (br20)
#
# Explicitly overrides UniFi-generated dhcp-boot
# Works with BIOS, UEFI, and UEFI64 clients
# ==========================================================

log-dhcp

# ----------------------------------------------------------
# Architecture detection (RFC 4578)
# ----------------------------------------------------------
# 0  = Legacy BIOS
# 7  = UEFI (IA32)
# 9  = UEFI64 (x86_64)

dhcp-match=set:BIOS,option:client-arch,0
dhcp-match=set:UEFI,option:client-arch,7
dhcp-match=set:UEFI64,option:client-arch,9

# ----------------------------------------------------------
# Optional: detect iPXE (future use)
# ----------------------------------------------------------
dhcp-userclass=set:ipxe,iPXE

# ----------------------------------------------------------
# VLAN tag (UniFi autogenerated)
# ----------------------------------------------------------
# net_MY_LAN_br20_10-20-28-0-22
# ----------------------------------------------------------
# VLAN tag notes (UniFi autogenerated)
#
# UniFi creates one dnsmasq tag per network in the form:
#   net_<NAME>_<bridge>_<subnet>
#
# To discover available tags, run on the gateway:
#   grep -R "dhcp-range=set:net_" /run/dnsmasq.dhcp.conf.d/
#
# Example output:
#   dhcp-range=set:net_MY_LAN_br20_10-20-28-0-22,...
#   dhcp-range=set:net_GuestNet_br4001_192-168-28-0-24,...
#
# Use the FULL tag string (exact match) in dhcp-boot lines
# to scope PXE behavior to that specific network only.
# ----------------------------------------------------------

# ----------------------------------------------------------
# BIOS clients -> chainload iPXE (kpxe)
# ----------------------------------------------------------
dhcp-boot=tag:net_MY_LAN_br20_10-20-28-0-22,tag:BIOS,netboot.xyz.kpxe,,10.20.28.219

# ----------------------------------------------------------
# UEFI / UEFI64 clients -> native EFI loader
# ----------------------------------------------------------
dhcp-boot=tag:net_MY_LAN_br20_10-20-28-0-22,tag:UEFI,netboot.xyz.efi,,10.20.28.219
dhcp-boot=tag:net_MY_LAN_br20_10-20-28-0-22,tag:UEFI64,netboot.xyz.efi,,10.20.28.219

# ----------------------------------------------------------
# Safety net: explicit 66/67 for noisy firmware
# ----------------------------------------------------------
dhcp-option=tag:net_MY_LAN_br20_10-20-28-0-22,tag:BIOS,66,10.20.28.219
dhcp-option=tag:net_MY_LAN_br20_10-20-28-0-22,tag:BIOS,67,"netboot.xyz.kpxe"

dhcp-option=tag:net_MY_LAN_br20_10-20-28-0-22,tag:UEFI,66,10.20.28.219
dhcp-option=tag:net_MY_LAN_br20_10-20-28-0-22,tag:UEFI,67,"netboot.xyz.efi"

dhcp-option=tag:net_MY_LAN_br20_10-20-28-0-22,tag:UEFI64,66,10.20.28.219
dhcp-option=tag:net_MY_LAN_br20_10-20-28-0-22,tag:UEFI64,67,"netboot.xyz.efi"
EOF

# Reload dnsmasq cleanly
if [ -f /run/dnsmasq-main.pid ]; then
  kill -HUP "$(cat /run/dnsmasq-main.pid)"
fi

exit 0