#!/bin/bash

#script started by the user via the web interface
#$1 and $2 are SSID and Password (arguments passed via the php script launcher.php)

echo "-------"
echo "Start connexion shell script at $(date)"


#disable hotspot
echo "Disable Hotspot:"
sudo nmcli connection down Hotspot
sleep 10

echo "Start connection with:"
echo "SSID: $1"
echo "Password: $2"
sudo nmcli device wifi connect "$1" password "$2"

#check if connection is successfull
if [ $? -eq 0 ]; then
    echo "Connection to $1 is successfull"
else
    echo "Connection to $1 failed"
    echo "Restarting hotspot..."
    #enable hotspot
    sudo nmcli connection up Hotspot
fi
echo "End connexion shell script"
echo "-------"


