#! /bin/sh

# Get the SSID and Password from arguments
SSID=$1
PASSWORD=$2

# Validate SSID and Password
if [ -z "$SSID" ]; then
    echo "SSID cannot be empty"
    exit 1
fi

if [ -z "$PASSWORD" ]; then
    wpa_passphrase "$SSID" > /etc/wpa_supplicant/wpa_supplicant.conf
else
    wpa_passphrase "$SSID" "$PASSWORD" > /etc/wpa_supplicant/wpa_supplicant.conf
fi

openrc default

rc-update del iptables wifi
rc-update del dnsmasq wifi
rc-update del hostapd wifi

rc-update add wpa_supplicant wifi

sed -i '/^interface wlan0/{s/^interface/#interface/;n;s/^static/#static/}' /etc/dhcpcd.conf

service dhcpcd restart
