#!/bin/sh /etc/rc.common
# Hardware bring-up before netifd: vendor drivers, zram, Wi-Fi (SC2355), first-boot Wi-Fi config, Bluetooth off
START=12
export PATH="$PATH:/opt/mu300/busybox-bin"

# Everything here runs in the background on purpose. busybox init only spawns the consoles after sysinit has
# finished, so anything slow (or hanging) in here costs the device its serial console *and* its network - which
# is exactly what a hanging wifi-start did on the mainline kernel, leaving no way in at all.
boot() {
	/opt/mu300/bin/mu300-led power on
	/opt/mu300/bin/mu300-usb boot
	# the kernel's module helper once its table is made (~2 s; FINDINGS 163)
	/opt/mu300/bin/mu300-modprobe-guard --prepare > /tmp/mu300-modprobe-guard.log 2>&1 &
	hw_bringup > /tmp/mu300-hw.log 2>&1 &
}

hw_bringup() {
	# The rules first, then the chip: cfg80211 looked for regulatory.db before the rootfs was mounted, and with the
	# database and the country in place before the phy appears, netifd's own start of hostapd (it starts one as soon
	# as the phy is there) already runs under them. The Wi-Fi then comes before the vendor extras and zram, which held
	# it back ~2 s at every boot (10 modprobes of modules this kernel does not have, and the Mali GPU's).
	/opt/mu300/bin/regdb-load >/tmp/mu300-wifi-start.log 2>&1
	# a broken /etc/board.json made again before the wifi-scripts read it (the bring-up goes on either way)
	/opt/mu300/bin/openwrt-board-check >>/tmp/mu300-wifi-start.log 2>&1
	country=$(uci -q get wireless.radio0.country)
	[ -n "$country" ] && iw reg set "$country"
	/opt/mu300/bin/wifi-start >>/tmp/mu300-wifi-start.log 2>&1
	/opt/mu300/bin/extra-modules >/tmp/mu300-extra-modules.log 2>&1
	/opt/mu300/bin/zram-start >/dev/null 2>&1
	cfg=$(md5sum /etc/config/wireless 2>/dev/null)
	/opt/mu300/bin/openwrt-wifi-config >>/tmp/mu300-wifi-start.log 2>&1
	# a hotspot set to WPA3/SAE (the U30 Air's SAE offload, now disabled: docs/U30AIR-SAE.md, FINDINGS 80) becomes
	# WPA2-PSK with the same password, before the Wi-Fi restart below, which is the start that brings the hotspot up
	# (netifd's own, earlier one ends AP-DISABLED under the world regulatory domain; the wifi-scripts refuse SAE)
	/opt/mu300/bin/openwrt-wifi-wpa2-only migrate >>/tmp/mu300-wifi-start.log 2>&1
	# the NFC tag (U30 Air, V50): the hotspot's name and password, if that is what it holds (see mu300-nfc)
	/opt/mu300/bin/mu300-nfc supported 2>/dev/null && /opt/mu300/bin/mu300-nfc sync >/tmp/mu300-nfc.log 2>&1
	# apply the country before netifd starts hostapd, otherwise 5 GHz channels are still NO-IR under world rules
	country=$(uci -q get wireless.radio0.country)
	reg_ready=1
	if [ -n "$country" ]; then
		iw reg set "$country"
		reg_ready=0
		for n in $(seq 1 10); do iw reg get | grep -q "country $country:" && { reg_ready=1; break; }; sleep 1; done
	fi
	# Unchanged settings and an access point that netifd's own start brought up: nothing to redo. The down/up below
	# took a hotspot that was up (and the chip under it: a power cycle and a firmware load) down again for ~4 s at
	# every boot. Only once the country is live (kanoqwq's f0b5ad43): an access point that came up under the world
	# rules is restarted under the country's. A settings file that could not be read is never "unchanged".
	if [ "$reg_ready" = 1 ] && [ -n "$cfg" ] && [ "$(md5sum /etc/config/wireless 2>/dev/null)" = "$cfg" ] &&
	   ap_enabled_soon; then
		echo "access point up under the configured rules, no restart" >>/tmp/mu300-wifi-start.log
		return 0
	fi
	# a reload does not restart a hostapd interface that went AP-DISABLED while the world regulatory domain
	# was still active; one down/up right after the country is live does
	wifi down >>/tmp/mu300-wifi-start.log 2>&1
	wifi up >>/tmp/mu300-wifi-start.log 2>&1
}

# ap_enabled_soon: true once the Wi-Fi is up as configured (wifi-ready: every enabled radio and interface, each access
# point ENABLED; one switched off counts as up), 20 s at most; false when it fails (a radio netifd gave up on, an access
# point DISABLED) or never gets there. It asked hostapd.wlan0 alone before, which a hotspot switched off never has.
ap_enabled_soon() {
	for n in $(seq 1 100); do
		/opt/mu300/bin/wifi-ready
		case $? in
			0) return 0 ;;
			2) return 1 ;;
		esac
		/opt/mu300/bin/busybox sleep 0.2 2>/dev/null || sleep 1
	done
	return 1
}

start() {
	boot
}
