speedwm-personal/scripts/speedwm-netctrl
speediegq 3086171d8f More reliable way of setting binary directory. Other method worked fine,
but Portage rejects it and I want a distro-friendly way.
2022-10-09 15:01:14 +02:00

130 lines
4.3 KiB
Bash
Executable file

#!/bin/sh
# speedwm-netctrl
# dmenu/other run launcher GUI for iwd
# Licensed under the GNU GPLv3 free software license.
case "$RUNLAUNCHER" in
"") RUNLAUNCHER=dmenu ;;
esac
BINDIR="$(dirname $(command -v status))/"
command -v iwctl > /dev/null || exit 1
HAVE_GRID="true"
HAVE_PASSWORD="false"
if [ -e "$HOME/.config/speedwm-de/global/config" ]; then
. $HOME/.config/speedwm-de/global/config
grep -q HAVE_PASSWORD $HOME/.config/speedwm-de/global/config || printf "\nHAVE_PASSWORD=$HAVE_PASSWORD # Whether or not to use the Password argument if a script requests it. If you do not have the dmenu password patch, set this to false. Doing so will disable password prompts." >> $HOME/.config/speedwm-de/global/config
else
mkdir -p $HOME/.config/speedwm-de/global
printf "HAVE_GRID=$HAVE_GRID # Whether or not to use the Grid argument. If you do not have the dmenu grid patch, set this to false. Doing so will disable grid." > $HOME/.config/speedwm-de/global/config
printf "\nHAVE_PASSWORD=$HAVE_PASSWORD # Whether or not to use the Password argument if a script requests it. If you do not have the dmenu password patch, set this to false. Doing so will disable password prompts." >> $HOME/.config/speedwm-de/global/config
fi
if [ "$HAVE_GRID" = "true" ]; then
GRIDNUM="1"
fi
if [ "$HAVE_GRID" = "true" ]; then
GRIDARG="-g"
fi
SHOW_PASSWORD="true"
test ${BINDIR}iwctl || exit 0
if [ -e "$HOME/.config/speedwm-de/netctrl/config" ]; then
. $HOME/.config/speedwm-de/netctrl/config
else
mkdir -p $HOME/.config/speedwm-de/netctrl
printf "SHOW_PASSWORD=$SHOW_PASSWORD # Show password when entering it or not? Requires the 'password' dmenu patch. If you do not have it, you can get my build. You also need to enable it in $HOME/.config/speedwm-de/global/config. (true/false)" > $HOME/.config/speedwm-de/netctrl/config
fi
if [ "$SHOW_PASSWORD" = "false" ]; then
if [ "$HAVE_PASSWORD" = "true" ]; then
PASS_ARG="-P"
fi
fi
# Get the device used to connect.
getdevice() {
seldevice=$1
if [ -e "$HOME/.local/share/seldevice" ]; then
seldevice=$(cat $HOME/.local/share/seldevice)
else
case "$1" in
"") devices="$(printf "\n$(ip link)" | awk '{ print $2 }' | awk 'NR%2==0' | sed "s/://g")"
seldevice=$(echo $devices | sed 's/ /\n/g' | $RUNLAUNCHER -p "What device do you want to connect with?" -l 10 $GRIDARG $GRIDNUM) && echo $seldevice > $HOME/.local/share/seldevice ;;
esac
fi
case "$seldevice" in
"") exit 0 ;;
esac
}
# connect to wifi
connectwifi() {
# if args aren't disconnect, scan for networks
# if it is disconnect, disconnect
if [ "$INARGS" != "disconnect" ]; then
iwctl station $seldevice scan
network="$(printf "$(iwctl station $seldevice get-networks | sed -n 6,20p | sed "s|>||g" | awk '{ print $1 }')\nDisconnect\n------\nExit" | sed "s| |No networks found|g" | $RUNLAUNCHER -l 10 $GRIDARG $GRIDNUM -p "Select a network to connect to" | sed "s|------||g" | sed "s|No networks found| |g")"
case "$network" in
"") exit 0 ;;
"Disconnect") disconnect=true ;;
"Exit") exit 0 ;;
esac
else
disconnect=true
fi
if [ "$disconnect" != "true" ]; then
iwctl station $seldevice get-networks | grep "$network" > /dev/null || exit 1
connect_pass() {
PASSPHRASE=$(printf "\n" | $RUNLAUNCHER -p "This network is protected. Enter the passphrase." -l 1 $GRIDARG $GRIDNUM $PASS_ARG)
case "$PASSPHRASE" in
"") exit 0 ;;
esac
printf "$network\n" >> $HOME/.local/share/networks
iwctl station $seldevice connect $network -P $PASSPHRASE || connect_pass
if [ -e "${BINDIR}notify-send" ]; then
notify-send " Connected to $network!"
fi
exit 0
}
connect_no_pass() {
iwctl station $seldevice connect $network || exit 0
if [ -e "${BINDIR}notify-send" ]; then
notify-send " Connected to $network!"
fi
exit 0
}
fi
disconnect() {
iwctl station $seldevice disconnect
if [ -e "${BINDIR}notify-send" ]; then
notify-send " Disconnected from the network!"
fi
exit 0
}
if [ "$disconnect" != "true" ]; then
grep $network $HOME/.local/share/networks && connect_no_pass && exit 0
iwctl station $seldevice get-networks | grep psk && connect_pass && exit 0
else
disconnect
fi
}
export INARGS="$1"
getdevice && connectwifi
pgrep -x status > /dev/null && pkill status && status &