#!/bin/sh
# First-boot provisioning for the OMEGA Hub. OpenWrt runs /etc/uci-defaults/*
# once on first boot, then deletes the script. Idempotent: bails if already done.
#
# Creates the isolated AV network (10.20.0.0/24), a hidden "OMEGA-AV" Wi-Fi with a
# per-unit random key, DHCP for the units, and firewall rules that keep AV traffic
# off the main LAN while letting staff reach only the bridge (:8080).

# --- LAN 2-5 → OMEGA-AV device ports. Runs on EVERY provisioning, ABOVE the
#     "already provisioned" guard below, so a unit first set up by a build that
#     predates this feature (< 1.10.23) still gets the device ports on its next
#     update — the guard would otherwise skip it forever. Idempotent: only
#     commits/reloads when something actually changes. LAN 1 always stays on the
#     main LAN as the fixed management port. Find br-lan by name, not @device[0]. ---
uci -q get network.omega_dev.name >/dev/null 2>&1 || {
	uci set network.omega_dev='device'
	uci set network.omega_dev.name='br-omega'
	uci set network.omega_dev.type='bridge'
}
BRLAN=""; i=0
while name="$(uci -q get network.@device[$i].name 2>/dev/null)"; do
	[ "$name" = "br-lan" ] && { BRLAN="network.@device[$i]"; break; }
	i=$((i + 1))
done
PORTS_CHANGED=0
for p in lan2 lan3 lan4 lan5; do
	uci -q get network.omega_dev.ports | grep -qw "$p" && continue
	[ -n "$BRLAN" ] && uci del_list "$BRLAN.ports"="$p" 2>/dev/null
	uci add_list network.omega_dev.ports="$p"
	PORTS_CHANGED=1
done
if [ "$PORTS_CHANGED" = 1 ]; then
	uci commit network
	# On an already-provisioned unit (an update) apply it now — boot already ran;
	# a fresh unit picks it up when the network first comes up later in this boot.
	[ -n "$(uci -q get network.omega)" ] && /etc/init.d/network reload 2>/dev/null
fi

[ -n "$(uci -q get network.omega)" ] && exit 0   # the REST is already provisioned

# --- pick the 2.4GHz radio (LinkPlay/OMEGA Wi-Fi is 2.4GHz). Device names vary
#     by platform (radio0 on vanilla OpenWrt, mt798611 on GL.iNet mt7986), so
#     match any device with band=2g, then fall back to the first wifi-device. ---
RADIO="$(uci show wireless 2>/dev/null | sed -n "s/^wireless\.\([^.]*\)\.band='2g'.*/\1/p" | head -1)"
[ -z "$RADIO" ] && RADIO="$(uci show wireless 2>/dev/null | sed -n "s/^wireless\.\([^.]*\)=wifi-device/\1/p" | head -1)"

# --- per-unit random Wi-Fi key + readable venue defaults (changeable later) ---
KEY="$(tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 16)"
[ -n "$(uci -q get omega.@hub[0])" ] || uci add omega hub >/dev/null
# fill any missing/empty fields individually — the shipped /etc/config/omega
# carries the section with an empty key placeholder
[ -n "$(uci -q get omega.@hub[0].venue)" ] || uci set omega.@hub[0].venue='OMEGA Venue'
[ -n "$(uci -q get omega.@hub[0].ssid)" ]  || uci set omega.@hub[0].ssid='OMEGA-AV'
[ -n "$(uci -q get omega.@hub[0].key)" ]   || uci set omega.@hub[0].key="$KEY"
uci commit omega
SSID="$(uci get omega.@hub[0].ssid)"
KEY="$(uci get omega.@hub[0].key)"

# --- isolated AV network (br-omega + its LAN 2-5 ports were set up above) ---
uci set network.omega='interface'
uci set network.omega.proto='static'
uci set network.omega.device='br-omega'
uci set network.omega.force_link='1'
uci set network.omega.ipaddr='10.20.0.1'
uci set network.omega.netmask='255.255.255.0'

# --- DHCP for the units ---
uci set dhcp.omega='dhcp'
uci set dhcp.omega.interface='omega'
uci set dhcp.omega.start='50'
uci set dhcp.omega.limit='150'
uci set dhcp.omega.leasetime='12h'
# dnsmasq only serves ranges for interfaces that existed when it started —
# restart it after br-omega appears or clients associate but never get a lease
/etc/init.d/dnsmasq restart 2>/dev/null || true

# --- hidden OMEGA-AV Wi-Fi bound to the AV network ---
uci set wireless.omega_ap='wifi-iface'
uci set wireless.omega_ap.device="$RADIO"
uci set wireless.omega_ap.mode='ap'
uci set wireless.omega_ap.network='omega'
uci set wireless.omega_ap.ssid="$SSID"
uci set wireless.omega_ap.hidden='1'
uci set wireless.omega_ap.encryption='psk2'
uci set wireless.omega_ap.key="$KEY"
# GL.iNet's proprietary MTK driver needs an explicit ifname (ra0..raN on 2.4GHz);
# pick the first raN not claimed by another wifi-iface. Vanilla mac80211 ignores it.
if uci show wireless | grep -q "ifname='ra0'"; then
	for i in 1 2 3; do
		uci show wireless | grep -q "ifname='ra$i'" || { uci set wireless.omega_ap.ifname="ra$i"; break; }
	done
fi
uci set wireless."$RADIO".disabled='0'
# Pin the 2.4GHz channel: 'auto' tends to pick 13, which Apple devices score
# poorly (phones silently hop back to their previous Wi-Fi after ~3 min).
uci set wireless."$RADIO".channel='6'

# GL.iNet's repeater daemon site-scans every ~30s, pulling the radio off-channel.
# Speakers buffer through it; iPhones/Macs rate the network as flaky and leave.
# This box is a router, never a repeater — turn it off for good.
/etc/init.d/repeater stop 2>/dev/null
/etc/init.d/repeater disable 2>/dev/null

# --- firewall: isolate AV, allow internet, expose only the bridge to staff ---
uci add firewall zone >/dev/null
uci set firewall.@zone[-1].name='omega'
uci set firewall.@zone[-1].network='omega'
uci set firewall.@zone[-1].input='ACCEPT'
uci set firewall.@zone[-1].output='ACCEPT'
uci set firewall.@zone[-1].forward='REJECT'

uci add firewall forwarding >/dev/null
uci set firewall.@forwarding[-1].src='omega'
uci set firewall.@forwarding[-1].dest='wan'

uci add firewall rule >/dev/null
uci set firewall.@rule[-1].name='staff-to-bridge'
uci set firewall.@rule[-1].src='lan'
uci set firewall.@rule[-1].proto='tcp'
uci set firewall.@rule[-1].dest_port='8088'
uci set firewall.@rule[-1].target='ACCEPT'

uci commit
exit 0
