#!/bin/sh
# iwd - bring up wireless interface & manager srv

WLAN=$(ip link show | awk -F: '/^[0-9]+: wl/ {print $2; exit}' | tr -d ' ')

[ -n "$WLAN" ] || { echo "iwd: no wireless interface found"; exit 1; }

echo "iwd: using interface $WLAN"
mkdir -p /var/lib/iwd
rm -f /run/sv/ready/iwd

# poll for iwd having associated (interface up with no address yet is fine
# dhcp will handle the address; we just need iwd to have a link)
(
	for i in $(seq 30); do
		sleep 1
		ip link show "$WLAN" 2>/dev/null | grep -q 'state UP' && {
			touch /run/sv/ready/iwd
			exit 0
		}
	done
	echo "iwd: timed out waiting for $WLAN to come up"
) &

exec iwd
