#!/bin/bash
# Set passphrase 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.conf

newppok=0
while [ "$newppok" -ne 1 ]
do
    echo -n "New passphrase: "
    read newpp
    newpplen=`echo -n $newpp | wc -m`
    if [ "$newpp" = "" -o "$newpplen" -lt 8 ]
    then
        echo "Passphrase didn't satisfy requirements. Try again or hit <ctrl>-c to abort."
    else
        newppok=1
    fi
done

if [ "$network_system" = "systemd-networkd" ]
then
    sed -i "/wpa_passphrase=.*/c wpa_passphrase=$newpp" /etc/hostapd/hostapd.conf
    systemctl restart hostapd
elif [ "$network_system" = "NetworkManager" ]
then
    nmcli con down piwifi-ap
    nmcli con mod piwifi-ap wifi-sec.psk "$newpp"
    nmcli con up piwifi-ap
else
    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
fi

sed -i 's,'wappass=.*','wappass="\"$newpp\""',' /etc/piwifiap.conf


    
