blob: e97e915ee33087a52cbabe58801e5eb8de41a3fb (
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
|
#!/bin/sh
set -eu
msg_start='[....] Starting %s... '
msg_stop='[....] Stopping %s... '
msg_restart='[....] Restarting %s... '
msg_start_ok='[[32m ok [39m] Starting %s... done'
msg_stop_ok='[[32m ok [39m] Stopping %s... done'
msg_restart_ok='[[32m ok [39m] Restarting %s... done'
msg_start_fail='[[31mfail[39m] Starting %s... failed'
msg_stop_fail='[[31mfail[39m] Stopping %s... failed'
msg_restart_fail='[[31mfail[39m] Restarting %s... failed'
XDG_DATA_HOME=
XDG_CONFIG_HOME=
XDG_DATA_DIRS=
XDG_CONFIG_DIRS=
XDG_CACHE_HOME=
PIDDIR=
SERVICES=
load_service()
{
local service="${1}"
shift 1
XDG_DATA_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}"
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-${HOME}/.config}"
XDG_DATA_DIRS="${XDG_DATA_DIRS:-/usr/local/share/:/usr/share/}"
XDG_CONFIG_DIRS="${XDG_CONFIG_DIRS:-/etc/xdg}"
XDG_CACHE_HOME="${XDG_CACHE_HOME:-${HOME}/.cache}"
PIDDIR="${XDG_CACHE_HOME}/homerc"
# Default functions
stop() { :; }
restart() { stop && start; }
status() { :; }
# TODO: Errors
. "${XDG_CONFIG_HOME}/homerc/${service}" || return 1
return 0
}
log()
{
local service="${1}"
local action="${2}"
local var="${3}"
shift 3
if ! tty 1>/dev/null 2>&1; then
return 0
fi
case "${action}" in 'start' | 'stop' | 'restart')
eval "printf \"\\r\${msg_${action}${var}}\" \"\${service}\""
esac
return 0
}
act()
{
local service="${1}"
local action="${2}"
shift 2
local out=
local es=
log "${service}" "${action}" ''
if out="$("${action}" 2>&1)"; then
log "${service}" "${action}" '_ok'
case "${out}" in
'') printf '\n';;
*) printf '\n%s\n' "${out}";;
esac
return 0
else
es=${?}
log "${service}" "${action}" '_fail'
case "${out}" in
'') printf '\n';;
*) printf '\n%s\n' "${out}";;
esac
return ${es}
fi
}
match_session()
{
local needle="${1}"
local haystack="${2}"
shift 2
local hay=
IFS=','
for hay in ${haystack}; do
unset IFS
case "${hay}" in
'') continue;;
"${needle}" | "${needle}:"*) return 0;;
esac
done
unset IFS
return 1
}
chg_sessions()
{
local service="${1}"
local old_sessions="${2}"
local new_sessions="${3}"
shift 3
local is_running=
local should_run=
is_running=false
should_run=false
for session in ${SESSIONS}; do
if match_session "${session}" "${old_sessions}"; then
is_running=true
fi
if match_session "${session}" "${new_sessions}"; then
should_run=true
fi
done
if ${is_running} && ! ${should_run}; then
if ! act "${service}" 'stop'; then
return ${?}
fi
fi
if ! ${is_running} && ${should_run}; then
if ! act "${service}" 'start'; then
return ${?}
fi
fi
return 0
}
usage()
{
printf 'Usage: %s <service> {start|stop|restart|status}\n' "${progname}"
return 0
}
main()
{
local service=
local action=
if [ ${#} -ne 2 ]; then
usage 1>&2
return 1
fi
service="${1}"
action="${2}"
case "${action}" in
'start' | 'stop' | 'restart' | 'status')
load_service "${service}" || return 1
if ! act "${service}" "${action}"; then
return ${?}
fi
;;
*';'*)
load_service "${service}" || return 1
if ! chg_sessions "${service}" \
"${action%%;*}" "${action#*;}"; then
return ${?}
fi
;;
*)
usage 1>&2
return 1
;;
esac
return 0
}
main "${@}"
|