-
Notifications
You must be signed in to change notification settings - Fork 1
/
barkeep
executable file
·326 lines (257 loc) · 6.91 KB
/
barkeep
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
#!/usr/bin/env bash
# barkeep - create entries based on a template for use with taverner
# Options
shopt -s nullglob
readonly argv0=barkeep
readonly prefix=${LOCALDIR:-$HOME/local}/data/taverner
readonly bindir=$prefix/bin
readonly coverdir=$prefix/covers
readonly templatedir=$prefix/templates
readonly TMPDIR=/tmp
usage_mk() {
cat <<'!'
Create a new menu entry. Uses $EDITOR to complete the new entry.
mk [-c <cover>] -t <template> <entry>
-t Select a template to base the new entry on.
-c Path or url to a cover image. (Note: This is destructive)
entry The name for the new launcher script.
!
}
usage_ls() {
cat <<!
List entries and templates.
ls [-t]
-t List all available templates.
If no arguments given this will list all entries.
!
}
usage_ed() {
cat <<'!'
Edit the selected entry or template. Only one entry may be edited per
invocation.
ed [-t <template>] [-c <cover>] [entry]
-t Attempts to open the template with $EDITOR.
-c Replace the cover for entry, note that the entry is required
for this operation.
entry Attempts to open the entry with $EDITOR.
Issue: If -c is specified for an entry, it will both update the entries
cover image but also offer to edit the entry as well.
!
}
usage_rm() {
cat <<!
Remove selected entries and corresponding covers. If no argument given,
attempt to remove all entires.
rm [entry [entry]]
entry The chosen entry to remove.
!
}
usage() {
cat <<'!'
Usage: barkeep [-h] [mk | ls | ed | rm]
Each mode has an -h flag which produces a summary of its help section.
-e Preferred editor, uses $EDITOR if not set.
!
usage_mk
usage_ls
usage_ed
usage_rm
cat <<!
Examples:
# Creates an entry using the psx template called wipeout_3 using emacs
# as the editor.
barkeep mk -e emacs -t psx -c ~/wipeout.png wipeout_3
# Removes both wipeout_3 and crash_bandicoot launchers along with any
# cover images if found.
barkeep rm wipeout_3 crash_bandicoot
!
}
puts() {
printf -- "$1\n" "${@:2}"
}
err() {
puts "$argv0: $1" "${@:2}" >&2
}
confirm() {
local msg=$1
local -l reply
read -rp "$msg [Y/n] " reply
# If the reply is empty, assume Y
[[ $reply == y || ! $reply ]]
}
handle_ls() {
while (($#)); do
case $1 in
-t) ls_templates; exit ;;
-h) usage_ls; exit ;;
*) err '%s: Unrecognised argument' "$1"; exit 1 ;;
esac
shift
done
ls_entries
}
ls_entries() {
local entries
entries=("$bindir"/*)
if ((${#entries[@]})); then
puts '%s' "${entries[@]##*/}"
else
err '%s: No entries found here' "$bindir"
fi
}
ls_templates() {
local templates
templates=("$templatedir"/*)
if ((${#templates[@]})); then
puts '%s' "${templates[@]##*/}"
else
err '%s: No templates found here' "$templatedir"
fi
}
handle_mk() {
local entry cover template editor
while (($#)); do
case $1 in
-c) shift; cover=$1 ;;
-t) shift; template=$1 ;;
-e) shift; editor=$1 ;;
-h) usage_mk; exit ;;
*) entry=$1
esac
shift
done
if [[ ! $entry ]]; then
err 'An entry name is required'
exit 1
fi
if [[ -e $bindir/$entry ]]; then
err '%s: This entry already exists. See %s ls -e' "$bindir"/"$entry" "$argv0"
exit 1
fi
if [[ ! $template ]]; then
err 'A template required. See %s ls -t' "$argv0"
exit 1
fi
if [[ ! -e $templatedir/$template ]]; then
err '%s: Unable to find requested template' "$templatedir"/"$template"
exit 1
fi
if [[ ! $cover ]]; then
cover=$coverdir/placeholder
else
mk_cover "$entry" "$cover"
fi
mk_entry "$editor" "$bindir"/"$entry" "$templatedir"/"$template"
}
mk_entry() {
local editor=$1
local entry=$2
local template=$3
cp -vf -- "$template" "$entry"
command "${editor:-${EDITOR:-vi}}" "$entry"
chmod -c +x -- "$entry"
}
mk_cover() {
local entry=$1
local cover=$2
if [[ $cover =~ [https?\|f]tp ]]; then
if curl -#o "$TMPDIR"/barkeep_cover "$cover"; then
cover=$TMPDIR/barkeep_cover
else
err '%s: Failed to download cover' "$cover"
exit 1
fi
fi
if [[ -e $cover ]]; then
command gm mogrify -resize 368x527\! "$cover"
mv -vf -- "$cover" "$coverdir"/"$entry"
else
err '%s: Unable to find cover' "$cover"
fi
}
handle_rm() {
local entry entries remove
while (($#)); do
case $1 in
-h) usage_rm; exit ;;
*) entries+=($1)
esac
shift
done
if ! ((${#entries[@]})); then
entries=("$bindir"/*)
# As the proceeding removal code may not be dealing with absolute paths
# if a user explicitly makes a selection we strip the bindir here to
# keep it consistent with this assumption.
entries=(${entries[@]##$bindir/})
if ! ((${#entries[@]})); then
err '%s: Nothing here to remove' "$bindir"
exit 1
fi
fi
puts 'Attempting to remove the following launchers:'
puts '%s' "${entries[@]##$bindir/}"
if ! confirm 'Continue?'; then
puts 'Nothing removed'
exit
fi
for entry in "${entries[@]}"; do
if [[ -e $bindir/$entry ]]; then
remove+=("$bindir"/"$entry")
fi
if [[ -e $coverdir/$entry ]]; then
remove+=("$coverdir"/"$entry")
fi
done
rm -vf -- "${remove[@]}"
}
handle_ed() {
local entry cover template editor collated_files
while (($#)); do
case $1 in
-c) shift; cover=$1 ;;
-t) shift; template=$1 ;;
-e) shift; editor=$1 ;;
-h) usage_ed; exit ;;
*) entry=$1
esac
shift
done
if [[ $entry && $cover ]]; then
mk_cover "$entry" "$cover"
exit
fi
if [[ $template && -e $templatedir/$template ]]; then
collated_files+=("$templatedir"/"$template")
fi
if [[ $entry && -e $bindir/$entry ]]; then
collated_files+=("$bindir"/"$entry")
fi
if ((${#collated_files[@]})); then
command -- "${editor:-${EDITOR:-vi}}" "${collated_files[@]}"
fi
}
mkdir -p "$bindir" "$coverdir" "$templatedir"
if ! (($#)); then
err 'At least one argument is required'
usage
exit 1
fi
while (($#)); do
case $1 in
ed) shift; handle_ed "$@"
exit ;;
ls) shift; handle_ls "$@"
exit ;;
mk) shift; handle_mk "$@"
exit ;;
rm) shift; handle_rm "$@"
exit ;;
-h) usage
exit ;;
*) err '%s: Unrecognised argument' "$1"
usage
exit 1
esac
shift
done