#!/bin/bash
# Set SSID for PiWifiAP wireless access point

current_user=`whoami`
if [ "$current_user" != "root" ]
then
	echo "You must have root privileges to run this setup."
	echo "Try 'sudo $0'"
	exit 1
fi

source /etc/piwifiap/piwifiap.conf

wapssidok="no"
    while [[ "$wapssidok" == "no" ]]
    do
        echo "This access point will need an SSID. This is what will appear"
        echo "as the name of this terminal when other devices connect."
        echo -n "Please enter the desired SSID for this access point:  "
        read wapssid
        if [[ "$wapssid" = "" ]]
        then
            echo "SSID cannot be blank. Try again."
        else
            wapssidok="yes"
        fi
    done

case "$network_system" in
	"systemd-networkd")
		sed -i "/ssid=.*/c ssid=$wapssid" /etc/hostapd/hostapd.conf
    	systemctl restart hostapd
		sed -i 's|'wapssid=.*'|'wapssid="\'$wapssid\'"'|' /etc/piwifiap/piwifiap.conf
		;;
	"NetworkManager")
		nmcli con down piwifi-hotspot
		nmcli con mod piwifi-hotspot ssid "$wapssid"
		nmcli con up piwifi-hotspot passwd-file /etc/piwifiap/piwifiap.secret
		sed -i 's|'wapssid=.*'|'wapssid="\'$wapssid\'"'|' /etc/piwifiap/piwifiap.conf
		;;
	*)
		echo "Unable to determine what type of network configuration (systemd-networkd"
		echo "or NetworkManager) your system uses."
		echo "Cannot modify wireless access point parameters."
		exit 2
		;;
esac
