summary refs log tree commit diff
path: root/bin/rcmenu
blob: 976afe7f3968ba6410acd0b4bb68b77eb7b3c9f1 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/bash

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

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

__list() {
	(
		find "$svdir" -mindepth 1 -maxdepth 1 -type d -print |
			while read -r sv; do
				printf "%s " "$(basename "$sv")"
				doas -n s6-svstat "$sv"
			done
		find "$rcdir" -mindepth 1 -maxdepth 1 -type d -print |
			while read -r sv; do
				if [ -f "$sv/type" ] && grep -q bundle "$sv/type"; then
					printf "%s " "$(basename "$sv")"
					printf "[bundle] "
					printf "contents: "
					find "$sv/contents.d/" -type f -exec basename {} \; | xargs | sed 's, ,/,g'
				fi
			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 | \[bundle\])
			action=start
			msg=$(doas -n s6-rc -v2 -u change "$sv")
			;;

		*)
			action=noop
			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"