neodotfiles/config/fuzzel/scripts/rs_wifi
2024-08-19 00:36:33 -04:00

167 lines
4 KiB
Bash
Executable file

#!/usr/bin/env bash
# rs_wifi - A simple WiFi menu script for rofi/dmenu/wofi/fuzzel
# Author: Clay Gomera (Drake)
# Dependencies: {rofi || dmenu || wofi || fuzzel}, NetworkManager, libnotify (notify-send)
############################
# Configuration Parameters #
############################
# Main menu options
option1=" Turn on WiFi"
option2=" Turn off WiFi"
option3="󱛅 Disconnect WiFi"
option4="󱛃 Connect WiFi"
option5="󱛆 Setup captive portal"
option6=" Exit"
options="$option1\n$option2\n$option3\n$option4\n$option5\n$option6"
# WiFi interface information
wifi_info=$(nmcli dev | awk '/wifi/ {print $1,$3; exit}')
read -r wlan constate <<< "$wifi_info"
########################
# Function Definitions #
########################
# Check for missing dependencies
check_dependencies() {
local run_launcher_found=false
for launcher in rofi dmenu wofi fuzzel; do
if command -v "$launcher" &> /dev/null; then
run_launcher_found=true
break
fi
done
if [ "$run_launcher_found" = false ]; then
echo "Missing dependency: one of rofi, dmenu, wofi or fuzzel is required."
exit 1
fi
local missing_deps=()
for dep in nmcli notify-send; do
if ! command -v "$dep" &> /dev/null; then
missing_deps+=("$dep")
fi
done
if [ ${#missing_deps[@]} -ne 0 ]; then
echo "Missing dependencies: ${missing_deps[*]}"
exit 1
fi
}
# Turn off WiFi
turnoff() {
nmcli radio wifi off
if [ $? -eq 0 ]; then
notify-send "WiFi has been turned off"
else
notify-send "Failed to turn off WiFi"
fi
}
# Turn on WiFi
turnon() {
nmcli radio wifi on
if [ $? -eq 0 ]; then
notify-send "WiFi has been turned on"
else
notify-send "Failed to turn on WiFi"
fi
}
# Disconnect from WiFi
disconnect() {
if [ "$constate" = "disconnected" ]; then
notify-send "WiFi is already disconnected"
elif [ "$constate" = "connected" ]; then
nmcli device disconnect "$wlan"
if [ $? -eq 0 ]; then
notify-send "WiFi has been disconnected"
else
notify-send "Failed to disconnect WiFi"
fi
else
notify-send "Unknown WiFi state"
fi
}
# Connect to a WiFi network
connect() {
notify-send -t 5000 "Scanning networks..."
nmcli dev wifi rescan
wifinet=$(nmcli -f BSSID,SSID,BARS,SECURITY dev wifi list | sed -n '1!p' | $RUNNER -i -l 10 -p "[ Select a Wifi Network]  ");
if [ -z "$wifinet" ]; then
exit 0
fi
bssid=$(echo "$wifinet" | awk '{print $1}')
ssid=$(echo "$wifinet" | awk '{print $2}')
}
# Prompt for WiFi password
password() {
if nmcli connection show | awk -v ssid="$ssid" '$1 == ssid {found=1} END {exit !found}'; then
pass=""
elif nmcli -f BSSID,SECURITY dev wifi list | grep -w "$bssid" | awk '{print $2}' | grep -q -- "--"; then
pass=""
else
pass=$($RUNNER -l 0 --password -p "[Enter Password ]  " );
if [ -z "$pass" ]; then
exit 0
fi
fi
}
# Execute WiFi connection
action() {
if [ -n "$pass" ]; then
nmcli dev wifi connect "$bssid" password "$pass"
else
nmcli dev wifi connect "$bssid"
fi
if [ $? -eq 0 ]; then
notify-send "Connected to $ssid"
else
notify-send "Failed to connect to $ssid"
fi
}
####################
# Main Script Flow #
####################
# Check for dependencies
check_dependencies
cases=$(echo -e "$options" | $RUNNER -i -l 6 -p "[ Wifi Settings]  " ) # main menu prompt
if [ -z "$cases" ]; then
exit 0
fi
case "$cases" in
"$option1")
turnon
;;
"$option2")
turnoff
;;
"$option3")
disconnect
;;
"$option4")
if connect; then
if password; then
action
fi
fi
;;
"$option5")
${BROWSER:-xdg-open} http://networkcheck.kde.org
;;
"$option6")
exit 0
;;
esac