summaryrefslogtreecommitdiffstats
path: root/bin/rc
blob: 1b6c1cdb8edd25c6c010882c9fa3238bd36f31b1 (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
#!/bin/sh

set -eu

RC_MAGIC='~/rc'

progname=
rc_fifo=

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

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

	return 0
}

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

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

# Returns 0 if the session is the first of its type or was last of its type
# TODO: Handle decrement from 0.
inc_session()
{
	local s_type="${1}"
	local dir=${2}
	shift 2
	local ret=

	ret=1

	s_type="$(printf '%s' "${s_type}" | tr ':' '_')"
	eval "[ \${sessions_${s_type}:-0} -eq 0 ] && ret=0"
	eval "sessions_${s_type}=\$((\${sessions_${s_type}:-0} + ${dir}))"
	eval "[ \${sessions_${s_type}} -eq 0 ] && ret=0"

	return ${ret}
}

chg_session()
{
	local session_type="${1}"
	local inc_dir=${2}
	local diff="${3}"
	shift 3

	while :; do
		case "${session_type}" in *:*);; *) break;; esac
		if inc_session "${session_type}" ${inc_dir}; then
			diff="${diff}${session_type},"
		fi
		session_type="${session_type%:*}"
	done

	diff="${diff%,}"
}

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

	chg_session "${session_type}" 1 '+'
	return ${?}
}

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

	chg_session "${session_type}" -1 '-'
	return ${?}
}

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

	while :; do
		if read -r magic cmd arg; then
			case "${magic}" in "${RC_MAGIC}");; *)
				continue
			esac
		fi
		case "${cmd}" in
			'beg')
				if valid_session_type "${arg}"; then
					beg_session "${arg}"
				fi
				;;
			'end')
				if valid_session_type "${arg}"; then
					end_session "${arg}"
				fi
				;;
			'bye')
				break
				;;
			*)
				continue
				;;
		esac
	done 0<"${rc_fifo}"
}

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

	if mkfifo "${rc_fifo}" 2>/dev/null; then
		beg_session "${session_type}"
		listen
		rm "${rc_fifo}"
	else
		# Signal the running rc daemon.
		printf "%s beg %s\n" "${RC_MAGIC}" "${session_type}" \
			1>"${rc_fifo}"
	fi

	return 0
}

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

	if mkfifo "${rc_fifo}" 2>/dev/null; then
		# Assuming the daemon role 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" "${RC_MAGIC}" "${session_type}" \
			1>"${rc_fifo}"
	fi

	return 0
}

usage()
{
	printf 'Usage: %s {start|stop} <session-type>\n' "${0}"

	return 0
}

main()
{
	local action=
	local session_type=

	if [ ${#} -ne 2 ]; then
		usage 1>&2
		return 1
	fi
	action="${1}"
	session_type="${2}"

	if ! valid_session_type; then
		error 'Invalid session type'
		return 1
	fi

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

	case "${action}" in
		'start')
			start "${session_type}"
			;;
		'stop')
			stop "${session_type}"
			;;
		*)
			usage 1>&2
			return 1
			;;
	esac

	return 0
}

main "${@}"