summary refs log tree commit diff
path: root/bin/rcmenu
blob: 06e940c0723c9e9a7ae09c9b472cf89225febf92 (plain) (blame)
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
#!/bin/bash

[ -n "$DEBUG" ] && set -x
set -u

export MENU_PROMPT="󰑮 rc"
svdir=/run/s6-rc/servicedirs
ico=/usr/share/icons/mdi/scalable/run-fast.svg
menu_exitcode=-1

__list() {
	for sv in "$svdir"/*; do
		printf "%s " "$(basename "$sv")"
		doas -n s6-svstat "$sv"
	done | column -t
}

__handle() {
	while read -r line; do
		sv=$(printf "%s" "${line}" | awk '{print $1}')
		state=$(printf "%s" "${line}" | awk '{print $2}')

		case "$state" in
		up)
			msg=$(doas -n s6-rc -v2 -d change "$sv")
			if [ $menu_exitcode -eq 18 ]; then
				action=stopp
			else
				action=restart
				sleep 0.5
				msg=$(doas -n s6-rc -v2 -u change "$sv")
			fi
			;;

		down)
			action=start
			msg=$(doas -n s6-rc -v2 -u change "$sv")
			;;

		*)
			msg=$(printf "unknown service state: %s\n" "$state")
			false
			;;
		esac

		if [ "$?" = 0 ]; then
			notify-send -i "${ico}" -a rc "${sv}" "${action}ed"
		else
			notify-send -i "${ico}" -a rc -u critical -t 30000 "${sv}" "${action}ing ${sv} failed:\n<i>${msg}</i>"
		fi
		printf "%s\n" "${msg}"
	done
}

svs=$(__list | menu --list=10)
menu_exitcode=$?
__handle <<<"$svs"