#!/bin/bash
set -ex

# Unblock WiFi and turn the radio on before initial-setup runs, so its NetworkSpoke
# (and the desktop afterwards) can see/scan networks. The Switch's BCM4356A3 comes up
# rfkill soft-blocked, which is why WiFi could not be configured in initial-setup.
# Best-effort: must never block firstboot, so guard and swallow failures under set -e.
command -v rfkill >/dev/null 2>&1 && rfkill unblock wifi 2>/dev/null || true
command -v nmcli  >/dev/null 2>&1 && nmcli radio wifi on 2>/dev/null || true

if [[ -e /boot/switchroot/firstboot ]]; then
	# Ensure variables are being source
	source /boot/switchroot/firstboot
	# Create user
	adduser -m -s /bin/bash -o "${USERNAME}"
	echo "${USERNAME}:${PASSWORD}" | chpasswd
	# Disable initial-setup
	systemctl disable initial-setup
	# Rewmove firstboot setup file
	rm /boot/switchroot/firstboot
fi

systemctl disable l4t-firstboot
rm -f "${0}"
