#!/bin/bash
set -e

[ $(id -u) -eq 0 ] || {
	echo need root
	exit 1
}

apk add networkmanager network-manager-applet wpa_supplicant chrony macchanger wireless-tools iputils


cat >/etc/init.d/nm_wpa <<'EOF'
#!/sbin/openrc-run
command=/sbin/wpa_supplicant
command_args="-u -B"
name="WPA Supplicant Daemon (NetworkManager)"

depend()
{
	need localmount
	use logger dbus
	after bootmisc modules
	before dns dhcpcd net
	keyword -shutdown
}
EOF


cat >/etc/NetworkManager/NetworkManager.conf <<'EOF'
[main]
# dhcp=dhcpcd
dhcp=internal
plugins=keyfile,ifupdown,ofono

[ifupdown]
managed=true

[device-mac-randomization]
wifi.scan-rand-mac-address=yes

[logging]
level=debug
EOF


mv -nv /etc/network/interfaces{,.bak}
cat >/etc/network/interfaces <<'EOF'
auto lo
iface lo inet loopback
EOF


chmod 755 /etc/init.d/nm_wpa
rc-update add nm_wpa
rc-update add networkmanager
service nm_wpa start
service networkmanager start
