summaryrefslogtreecommitdiffstats
path: root/bin/rc
blob: a6332bf331f5551ac8a2bb5bdd591a6253cb4c0f (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/bin/sh

set -eu

MAGIC='~/rc'

progname=
rc_fifo=
res_fifo=
sessions=','

info()
{
	local fmt="${1}"
	shift 1

	printf "%s: ${fmt}\n" "${progname}" "${@}"

	return 0
}

error()
{
	local fmt="${1}"
	shift 1

	printf "%s: Error: ${fmt}\n" "${progname}" "${@}" 1>&2

	return 0
}

valid_session_type()
{
	local session_type="${1}"
	shift 1

	case "${session_type}" in *[!A-Za-z0-9:]* | '' | ':'* | *':' | *'::'*)
		return 1
	esac
	return 0
}

run_services()
{
	local old_sessions="${1}"
	local new_sessions="${2}"
	shift 2
	local ret=
	local service_script=

	ret=0
	for service_script in "${XDG_CONFIG_HOME:-${HOME}/.config}/homerc/"*; do
		"$(dirname "${0}")/svc" "${service_script##*/}" \
			"${old_sessions};${new_sessions}" || ret=1
	done

	return ${ret}
}

beg_session()
{
	local session_type="${1}"
	shift 1
	local s_type=
	local add=
	local new_sessions=
	local old_sessions=

	s_type="$(printf '%s' "${session_type}" | tr ':' '_')"

	if eval "[ \${sessions_${s_type}:-0} -eq 0 ]"; then
		add=true
	else
		add=false
	fi

	# Increment.
	eval "sessions_${s_type}=\$((\${sessions_${s_type}:-0} + 1))"

	if ${add}; then
		new_sessions="${sessions}${session_type},"
		old_sessions="${sessions}"
		sessions="${new_sessions}"
		run_services "${old_sessions}" "${new_sessions}" || return 1
	fi

	return 0
}

end_session()
{
	local session_type="${1}"
	shift 1
	local s_type=
	local new_sessions=
	local old_sessions=

	s_type="$(printf '%s' "${session_type}" | tr ':' '_')"

	if eval "[ \${sessions_${s_type}:-0} -eq 0 ]"; then
		# No running sessions of specified type
		return 1
	fi

	# Decrement.
	eval "sessions_${s_type}=\$((\${sessions_${s_type}} - 1))"

	if eval "[ \${sessions_${s_type}} -eq 0 ]"; then
		eval "unset sessions_${s_type}"
		new_sessions="${sessions%,${session_type},*}"
		new_sessions="${new_sessions},${sessions#*,${session_type},}"
		old_sessions="${sessions}"
		sessions="${new_sessions}"
		run_services "${old_sessions}" "${new_sessions}" || return 1
	fi

	return 0
}

list_sessions()
{
	local printed=
	local session=
	local s_type=

	printed=false
	IFS=','
	for session in ${sessions}; do
		unset IFS
		case "${session}" in '') continue;; esac
		s_type="$(printf '%s' "${session}" | tr ':' '_')"
		if ${printed}; then
			printf ', '
		fi
		eval "printf '%s (%d)' \"\${session}\" \${sessions_${s_type}}"
		printed=true
	done
	unset IFS

	return 0
}

listen()
{
	local magic=
	local cmd=
	local arg=

	while :; do
		if read -r magic cmd arg 0<"${rc_fifo}"; then
			case "${magic}" in "${MAGIC}");; *) continue;; esac
			case "${cmd}" in
				'beg' | 'end')
					if valid_session_type "${arg}"; then
						"${cmd}_session" "${arg}" || :
					fi
					;;
				'who')
					{
						printf '%s ' "${MAGIC}"
						list_sessions
						printf '\n'
					} 1>"${arg}" || :
					;;
				'bye')
					break
					;;
				*)
					continue
					;;
			esac
		fi
	done

	return 0
}

run_daemon()
{
	local session_type="${1}"
	shift 1
	local piddir=

	exec 0<&-
	exec 1>>"${XDG_CACHE_HOME:-${HOME}/.cache}/homerc/rc.log" 2>&1
	trap '' HUP

	piddir="${XDG_CACHE_HOME:-${HOME}/.cache}/homerc/run"
	mkdir -p "${piddir}"
	rm -f "${piddir}/"*

	beg_session "${session_type}"
	listen

	rm "${rc_fifo}"
	rm -f "${piddir}/"*

	return 0
}

start()
{
	local session_type="${1}"
	shift 1

	if mkfifo -m 0600 "${rc_fifo}" 2>/dev/null; then
		run_daemon "${session_type}" &
	else
		# Signal the running rc daemon.
		printf '%s beg %s\n' "${MAGIC}" "${session_type}" \
			1>"${rc_fifo}"
	fi

	return 0
}

stop()
{
	local session_type="${1}"
	shift 1

	if mkfifo -m 0600 "${rc_fifo}" 2>/dev/null; then
		# Daemonizing just to end a non-existent session makes no sense.
		error 'No running daemon found'
		rm "${rc_fifo}"
		return 1
	else
		# Signal the running rc daemon.
		printf '%s end %s\n' "${MAGIC}" "${session_type}" \
			1>"${rc_fifo}"
	fi

	return 0
}

status()
{
	local magic=
	local list=

	if mkfifo -m 0600 "${rc_fifo}" 2>/dev/null; then
		# Daemonizing just to list non-existent sessions makes no sense.
		error 'No running daemon found'
		rm "${rc_fifo}"
		return 1
	else
		# Establish response FIFO.
		if ! mkfifo -m 0600 "${res_fifo}" 2>/dev/null; then
			error 'Failed to communicate with running daemon'
			return 1
		fi
		# Signal the running rc daemon.
		printf '%s who %s\n' "${MAGIC}" "${res_fifo}" \
			1>"${rc_fifo}"
		if read -r magic list 0<"${res_fifo}" && \
				[ "x${magic}" = "x${MAGIC}" ]; then
			info 'Sessions: %s' "${list}"
			rm "${res_fifo}"
		else
			error 'Failed to communicate with running daemon'
			rm "${res_fifo}"
			return 1
		fi
	fi

	return 0
}

quit()
{
	if mkfifo -m 0600 "${rc_fifo}" 2>/dev/null; then
		# Daemonizing just to quit makes no sense.
		error 'No running daemon found'
		rm "${rc_fifo}"
		return 1
	else
		# Signal the running rc daemon.
		printf '%s bye\n' "${MAGIC}" 1>"${rc_fifo}"
	fi

	return 0
}

usage()
{
	printf 'Usage: %s {start|stop} <session-type>\n' "${0}"
	printf '   or: %s {status|quit}\n\n' "${0}"
	printf '<session-type> is one or more strings of alphanumeric '
	printf 'characters\nseparated by colons.\n'

	return 0
}

main()
{
	local action="${1-}"
	local session_type="${2-}"

	progname="${0##*/}"
	rc_fifo="/tmp/homercrc-$(id -u)"
	res_fifo="/tmp/homercres-${$}"

	case "${action}" in
		'start' | 'stop')
			if [ ${#} -ne 2 ] || ! valid_session_type \
					"${session_type}"; then
				usage 1>&2
				return 1
			fi
			"${action}" "${session_type}"
			;;
		'status')
			if [ ${#} -ne 1 ]; then
				usage 1>&2
				return 1
			fi
			status
			;;
		'quit')
			if [ ${#} -ne 1 ]; then
				usage 1>&2
				return 1
			fi
			quit
			;;
		*)
			usage 1>&2
			return 1
			;;
	esac

	return 0
}

main "${@}"