#!/bin/bash
###
if [ $UID != 0 ]; then echo "Run this by root"; exit 1; fi
if [ -z "$(command -v zenity)" ]; then echo "zenity not installed."; exit 8; fi
if [ "$(systemctl is-active NetworkManager)" == active ]; then
zenity --warning --title="Information" --text="Network Manager is active. This can cause some problems" --no-wrap
fi
if ! [ "$(command -v rfkill)" ]; then true; else
if [ -n "$(rfkill -r | grep -Eo "wlan * blocked *")" ]; then
zenity --error --text="It seems like your WLAN interface(s) blocked by something. Use rfkill unblock INTERFACE" \
	--width=300 --height=100
exit 1
fi; fi
if [ -z "$(command -v wifiman)" ]; then
zenity --error --text="WifiMan not found. Please install it first." \
	--width=300 --height=100
exit
fi
wfile=/etc/wifiman0/.wmgui
if [ -z "$(command -v wpa_supplicant)" ]; then zenity --error --text="WPA_Supplicant not found."; exit 8; fi
if [ -z "$(command -v iwconfig)" ]; then zenity --error --text="wireless-tools not found."; exit 8; fi
if [ -z "$(command -v sed)" ]; then zenity --error --text="sed not found."; exit 8; fi
if [ -z "$(command -v dhcpcd; command -v dhclient)" ]; then zenity --error --text="dhcp client not found."; exit 8; fi
if [ ! -d /etc/wifiman0 ]; then mkdir /etc/wifiman0; else true; fi
if [ "$(ls -ld /etc/wifiman0 | cut -d' ' -f 1)" != "drwxr-----" ]; then chmod 740 /etc/wifiman0; fi
if [ ! -f /etc/wifiman0/.dhcp ]; then
if [ "$(command -v dhcpcd dhclient | wc -l)" == 2 ]; then
        zenity --list --title="Choose dhcp client" --text="If you do not see the difference, choose dhclient" --width=300 --height=250 --radiolist \
	--column="Pick" --column="Client" \
	FALSE dhcpcd \
	FALSE dhclient > "$wfile"
        case $(cat $wfile) in
        dhcpcd) future_dhcp=dhcpcd ;; dhclient) future_dhcp=dhclient ;; *) echo "Error."; exit 6 ;; esac
	zenity --info --title="Information" --text="$(cat $wfile) selected."; echo $future_dhcp > /etc/wifiman0/.dhcp
else
        if [ -n "$(command -v dhcpcd)" ]; then future_dhcp=dhcpcd; echo $future_dhcp > /etc/wifiman0/.dhcp
        else
                if [ -n "$(command -v dhclient)" ]; then future_dhcp=dhclient; echo $future_dhcp > /etc/wifiman0/.dhcp; else zenity --error --text "DHCP client not found" --width=300 --height=100; exit 8; fi
        fi; fi; fi
bruh() {
zenity --list --title="WifiMan GUI Manager" \
	--width=350 --height=400 \
	--radiolist \
	--text="Choose option:" \
	--column="Pick" --column="Option" \
	FALSE "List of available networks" \
	FALSE "Create profile" \
	FALSE "List created profiles" \
	FALSE "Delete profile" \
	FALSE "Connect using profile" \
	FALSE "Show profile password" \
	FALSE "List available interfaces" \
	FALSE "Change DHCP client" \
	FALSE "Disconnect" > $wfile
bruh2
}
bruh2() {
case "$(cat $wfile)" in
"List of available networks")
zenity --info --title="Found networks:" \
	 --text "$(wifiman -l | sed 's/\x1b\[[0-9;]*m//g')" --width=200 --no-wrap
bruh
;;
"Create profile")
zenity --forms --title="Creating configuration" --text="fill in the fields below" \
	--add-entry="Network name(ESSID)" \
	--add-entry="Interface name" \
	--add-password="Password" | tr "|" "\n" > $wfile
	essid_thing="$(sed -n '1p' $wfile)"
	interface_thing="$(sed -n '2p' $wfile)"
	password_thing="$(sed -n '3p' $wfile)"
zenity --info --title="Information" --width=200 --text="$(wifiman -cp "$essid_thing" $interface_thing "$password_thing" | sed 's/\x1b\[[0-9;]*m//g')"
bruh
;;
"List created profiles")
zenity --info --title="Created profiles:" \
	--text "$(wifiman -lp)" --width=200
bruh
;;
"Delete profile")
if [ -z "$(ls /etc/wifiman0)" ]; then zenity --error --text "No profiles found" --no-wrap; bruh; else
sudo wifiman -lp | zenity --list --title="Deleting profile" --text="choose profile" --column=Pick 2>/dev/null > $wfile
zenity --info --title="Information" --width=300 --text="$(wifiman -dp $(cat $wfile) | sed 's/\x1b\[[0-9;]*m//g')" --no-wrap
fi
bruh
;;
"Connect using profile")
if [ -z "$(ls /etc/wifiman0)" ]; then zenity --error --text "No profiles found" --no-wrap; bruh; else
sudo wifiman -lp | zenity --list --title="Connecting" --text="choose profile" --column=Pick 2>/dev/null > $wfile
zenity --info --title="Information" --width=300 --text="$(wifiman -c $(cat $wfile) | sed 's/\x1b\[[0-9;]*m//g')" --no-wrap
fi
;;
"Disconnect")
zenity --info --title="Information" --width=300 --text="$(wifiman -d | sed 's/\x1b\[[0-9;]*m//g')" --no-wrap
bruh
;;
"Show profile password")
if [ -z "$(ls /etc/wifiman0)" ]; then zenity --error --text "No profiles found" --no-wrap; bruh; else
sudo wifiman -lp | zenity --list --title="Showing password" --text="choose profile" --column=Pick 2>/dev/null > $wfile
zenity --warning --title="Information" --width=300 --text="$(wifiman -sp $(cat $wfile) | sed 's/\x1b\[[0-9;]*m//g')" --no-wrap
fi
bruh
;;
"Change DHCP client")
if [ -z "$(ls /etc/wifiman0)" ]; then zenity --error --text "No profiles found" --no-wrap; bruh; else
zenity --list --title="Changing DHCP client" --text="choose client" --radiolist --column=Pick --column=Profile FALSE dhcpcd FALSE dhclient 2>/dev/null > $wfile
zenity --info --title="Information" --width=300 --text="$(wifiman -u $(cat ${wfile}) | sed 's/\x1b\[[0-9;]*m//g')" --no-wrap
fi
bruh
;;
"List available interfaces")
zenity --info --title="Interfaces:" --width=200 --height=150 \
	--text="$(ls /sys/class/net)"
bruh
;;
esac
}
bruh
