#!/bin/bash

# script to connect to weird educational wifis

# make sure that script is run with root privileges
[ $EUID -ne 0 ] && { echo -e "\e[0;30;101m Error: Please run this script with root privileges \e[0m"; exit; }

# list network interfaces
echo -e "\e[0;30;42m List of Wifi interfaces (iwconfig): \e[0m\n"
iwconfig

# get user input
echo -e "\e[0;30;46m Select Wifi Interface: \e[0m"
read -rp " >>> " interface

echo -e "\n\e[0;30;46m Username: \e[0m"
read -rp " >>> " username

echo -e "\n\e[0;30;46m Password: \e[0m"
echo -n " >>> "
read -rs password
printf "\n"

# define unique uuid
uuid=$(uuidgen)
while grep -q "uuid=${uuid}" /etc/NetworkManager/system-connections/*
do
    uuid=$(uuidgen)
done

# generate connection file
printf "[connection]
id=GKGe
uuid=%s
type=wifi
interface-name=%s
permissions=

[wifi]
mac-address-blacklist=
mode=infrastructure
ssid=GKGe

[wifi-security]
auth-alg=open
key-mgmt=wpa-eap

[802-1x]
eap=peap;
identity=%s
password=%s
phase2-auth=mschapv2

[ipv4]
dns-search=
method=auto

[ipv6]
addr-gen-mode=stable-privacy
dns-search=
method=auto

[proxy]
" "$uuid" "$interface" "$username" "$password" > /etc/NetworkManager/system-connections/gkg-connect.nmconnection
chmod 600 /etc/NetworkManager/system-connections/gkg-connect.nmconnection
systemctl restart NetworkManager || rc-service NetworkManager restart
nmcli connection up GKGe
