flake-update-20260505
 1#!/usr/bin/env bash
 2# Disconnect from RedHat VPN
 3# This will list active VPN connections and allow you to disconnect from them
 4set -e
 5
 6# Detect desktop environment and available tools
 7DESKTOP="${XDG_CURRENT_DESKTOP:-}"
 8GRAPHICS=1
 9
10# Check if running in a graphical environment
11if [[ "$DESKTOP" != "sway" && "$DESKTOP" != "niri" ]]; then
12	if ! command -v xset &>/dev/null; then
13		GRAPHICS=0
14	elif ! timeout 1s xset q &>/dev/null; then
15		GRAPHICS=0
16	fi
17fi
18
19# Get active VPN connections
20active_vpns=$(nmcli connection show --active | grep vpn)
21
22if [[ -z "$active_vpns" ]]; then
23	NOTIFY_CMD="notify-send"
24	if [[ GRAPHICS -eq 0 ]]; then
25		NOTIFY_CMD="echo"
26	fi
27	$NOTIFY_CMD "No active VPN" "No VPN connection is currently active."
28	exit 0
29fi
30
31# Select VPN connection to disconnect based on environment
32if [[ GRAPHICS -eq 0 ]]; then
33	# Terminal: use fzf
34	connection="$(echo "$active_vpns" | fzf)"
35elif [[ "$DESKTOP" == "sway" || "$DESKTOP" == "niri" ]]; then
36	# Wayland compositors (Sway/Niri): use fuzzel
37	connection="$(echo "$active_vpns" | awk '{print $1, $2, $3, $4}' | fuzzel --dmenu --prompt "Disconnect VPN: ")"
38else
39	# X11: use zenity
40	connection="$(echo "$active_vpns" | zenity --list --title "Active Red Hat VPNs" --text "Choose VPN to disconnect.." --column "Name" --width=600 --height=450)"
41fi
42
43NOTIFY_CMD="notify-send"
44if [[ GRAPHICS -eq 0 ]]; then
45	NOTIFY_CMD="echo"
46fi
47
48uuid=$(echo "${connection}" | awk '{print $4}')
49name=$(echo "${connection}" | awk '{print $1 $2 $3}')
50
51nmcli connection down "${uuid}"
52$NOTIFY_CMD "VPN ${name} disconnected." "You have been disconnected from the Red Hat VPN."