summaryrefslogtreecommitdiff
path: root/network_bt_ops.sh
blob: 2cd1b7eb5da086234d098fa6d1b2e80f9735feb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/sh
choices=$(mktemp --suffix=network_ops)
# This assumes only 1 BT controller on the system
ctrl_pow=$(bluetoothctl show|awk '/^\s+Powered:\s/ {print $2}')
[ "$ctrl_pow" = "yes" ] && {
  bdevs=$(bluetoothctl devices|awk '{print $2}')
  for dev in $bdevs; do
   dev_name=$(bluetoothctl info $dev|awk -F ':' '/^\s+Name:/ {print $2}')
   dev_connected=$(bluetoothctl info $dev|awk '/^\s+Connected:/ {print $2}')
   #printf "dev_name = %s\n" "$dev_name"
   #printf "dev_connected = %s\n" "$dev_connected"
   [ "$dev_connected" = "yes" ] && {
     printf "disconnect %s %s\n" "$dev" "$dev_name" >> $choices
     } || {
     printf "connect %s %s\n" "$dev" "$dev_name" >> $choices
     }
  done
  printf "\npower off BT ctrl\n" >> $choices
} || {
 # BT controller is powered off, this is the only sensible BT option
 printf "\npower on BT ctrl\n" >> $choices
}
apstat=$(systemctl show hostapd.service|awk -F '=' '/^ActiveS/ {print $2}')
printf "apstat = %s\n" $apstat
case $apstat in
  inactive|failed)
    printf "\nStart WiFi AP\n" >> $choices
  ;;
  active)
    printf "\nStop WiFi AP\n" >> $choices
  ;;
esac
isp_iface_state=$(ip -j addr show 'isp' |jq -cr '.[]|.operstate')
case "$isp_iface_state" in
  "DOWN")
    printf "\nInternet ON\n" >> $choices
    ;;
  "UP")
    printf "\nInternet OFF\n" >> $choices
    ;;
esac
[ -e $HOME/vpn_commands.sh ] && {
  $HOME/vpn_commands.sh >> $choices
}
if $(systemctl --quiet is-active transmission.service)
then printf "doas /usr/bin/systemctl stop transmission.service\n" >> $choices
else printf "doas /usr/bin/systemctl start transmission.service\n" >> $choices
fi
if $(systemctl --quiet is-active tor.service)
then printf "doas /usr/bin/systemctl stop tor.service\n" >> $choices
else printf "doas /usr/bin/systemctl start tor.service\n" >> $choices
fi
choice=$(grep -Ev '^\s*$' $choices|$PICKER -p 'Network ops:')
[ -z "$choice" ] && notify-send "no choice" && rm $choices && exit 1
printf "choice = %s\n" "$choice"
case "$choice" in
  "Internet OFF")
    doas /usr/bin/ip l set isp down
    msg=$?
    ;;
  "Internet ON")
    doas /usr/bin/ip l set isp up
    msg=$?
    ;;
  "Start WiFi AP")
    doas /usr/bin/ip l set wlan0 down
    doas /usr/bin/systemctl start hostapd.service
    sleep 1
    msg="$(systemctl status hostapd.service)"
    ;;
  "Stop WiFi AP")
    doas /usr/bin/systemctl stop hostapd.service
    doas /usr/bin/ip l set wlan0 down
    sleep 1
    msg="$(systemctl status hostapd.service)"
    ;;
	"power on BT ctrl")
    msg="$(bluetoothctl power on)"
		;;
	"power off BT ctrl")
    msg="$(bluetoothctl power off)"
		;;
	connect*|disconnect*)
		bcmd="${choice%% *}"
		bdev="${choice#* }"
		bdev="${bdev%% *}"
		printf "bcmd = %s\n" $bcmd
		printf "bdev = %s\n" $bdev
    msg="$(bluetoothctl $bcmd $bdev)"
		;;
  *)
    $choice
    msg=$?
    ;;
esac
notify-send "$msg"
rm $choices