-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkpaperd
executable file
·524 lines (492 loc) · 9.66 KB
/
mkpaperd
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#!/bin/sh
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>
echo_error () {
printf '%s: %s\n' "${0##*/}" "$*" >&2
}
die () {
local EXIT_CODE=1
if [ "$1" -ge 0 ] 2>/dev/null
then
EXIT_CODE=$(($1))
shift
fi
if [ -n "$1" ]
then
echo_error "$1"
fi
exit $EXIT_CODE
}
nonzero_or_die () {
if [ -z "$1" ]
then
shift
die "$@"
fi
}
command_or_die () {
while [ $# -gt 0 ]
do
nonzero_or_die "$(command -v "$1")" "$1: command not found"
shift
done
}
perl_package_or_die () {
command_or_die perl
while [ $# -gt 0 ]
do
if perl -M"$1" -e '' 2>/dev/null
then
shift
continue
fi
die "$1: perl package not found"
done
}
echo_usage () {
printf "usage: %s " "${0##*/}"
echo '[-group GROUP] -jar JAR [-java JAVA] -mode (launchd|systemd) [-user USER] [-OPTIONS...] [--] [ARGUMENTS...]'
}
case "$1" in
-h|-help|--help)
echo_usage
exit 0
esac
SEQUENCE_DELIMITER='' # \037
GROUP=
JAR=
JAVA=
MODE=
OPTIONS=
USER=
while [ $# -gt 0 ]
do
case "$1" in
-group)
nonzero_or_die "$2" "$1: option requires an argument"
GROUP="$2"
shift 2
;;
-jar)
nonzero_or_die "$2" "$1: option requires an argument"
case "$2" in
/*)
JAR="$2"
;;
*)
JAR="$(pwd)/$2"
esac
shift 2
;;
-java)
nonzero_or_die "$2" "$1: option requires an argument"
case "$2" in
/*)
JAVA="$2"
;;
*)
JAVA="$(pwd)/$2"
esac
shift 2
;;
-mode)
nonzero_or_die "$2" "$1: option requires an argument"
case "$2" in
launchd|systemd)
MODE="$2"
;;
*)
die "$2: unsupported mode"
esac
shift 2
;;
-user)
nonzero_or_die "$2" "$1: option requires an argument"
USER="$2"
shift 2
;;
--)
shift
break
;;
-*)
if [ -z "$OPTIONS" ]
then
OPTIONS="$1"
else
OPTIONS="$OPTIONS$SEQUENCE_DELIMITER$1"
fi
shift
;;
*)
break
esac
done
nonzero_or_die "$JAR" "$(echo_usage)"
nonzero_or_die "$MODE" "$(echo_usage)"
if [ -z "$JAVA" ]
then
command_or_die java
JAVA="$(command -v java)"
fi
ARGUMENTS=
while [ $# -gt 0 ]
do
if [ -z "$ARGUMENTS" ]
then
ARGUMENTS="$1"
else
ARGUMENTS="$ARGUMENTS$SEQUENCE_DELIMITER$1"
fi
shift
done
case "$MODE" in
launchd)
perl_package_or_die HTML::Entities
esac
WORKING_DIRECTORY="${JAR%/*}"
launchd_escape () {
echo "$1" | perl -MHTML::Entities -e 'print encode_entities(<>)'
}
launchd_is_depth_parameter () {
case "$1" in
-*)
if [ "$1" -le 0 ] 2>/dev/null
then
return $(true)
fi
esac
return $(false)
}
launchd_indent () {
local DEPTH=0
if launchd_is_depth_parameter "$1"
then
DEPTH=$(($1 * -1))
shift
fi
if [ $DEPTH -eq 0 ]
then
printf '%s\n' "$*"
return
fi
local INDENT=
while [ $DEPTH -gt 0 ]
do
INDENT="$INDENT\t"
DEPTH=$(($DEPTH - 1))
done
local IFS=$'\n'
local RESULT=
for LINE in $1
do
if [ -z "$RESULT" ]
then
RESULT="$INDENT$LINE"
else
RESULT="$RESULT\n$INDENT$LINE"
fi
done
echo "$RESULT"
}
launchd_array () {
local DEPTH=0
if launchd_is_depth_parameter "$1"
then
DEPTH=$(($1 * -1))
shift
fi
local CHILD_DEPTH=$(($DEPTH + 1))
local RESULT=
for SEQUENCE in "$@"
do
if [ -z "$SEQUENCE" ]
then
continue
fi
if [ -z "$RESULT" ]
then
RESULT="$(launchd_indent -$CHILD_DEPTH "$SEQUENCE")"
else
RESULT="$RESULT\n$(launchd_indent -$CHILD_DEPTH "$SEQUENCE")"
fi
done
if [ -z "$RESULT" ]
then
echo "$(launchd_indent -$DEPTH "<array/>")"
return
fi
echo "$(launchd_indent -$DEPTH "<array>")"
echo "$RESULT"
echo "$(launchd_indent -$DEPTH "</array>")"
}
launchd_true () {
local DEPTH=0
if launchd_is_depth_parameter "$1"
then
DEPTH=$(($1 * -1))
shift
fi
echo "$(launchd_indent -$DEPTH "<true/>")"
}
launchd_false () {
local DEPTH=0
if launchd_is_depth_parameter "$1"
then
DEPTH=$(($1 * -1))
shift
fi
echo "$(launchd_indent -$DEPTH "<false/>")"
}
launchd_boolean () {
local DEPTH=0
if launchd_is_depth_parameter "$1"
then
DEPTH=$(($1 * -1))
shift
fi
case "$1" in
true)
launchd_true -$DEPTH
;;
false)
launchd_false -$DEPTH
;;
*)
die "$1: invalid boolean value"
esac
}
launchd_integer () {
local DEPTH=0
if [ $# -gt 1 ]
then
if launchd_is_depth_parameter "$1"
then
DEPTH=$(($1 * -1))
shift
fi
fi
echo "$(launchd_indent -$DEPTH "<integer>$(($1))</integer>")"
}
launchd_string () {
local DEPTH=0
if launchd_is_depth_parameter "$1"
then
DEPTH=$(($1 * -1))
shift
fi
local ESCAPED="$(launchd_escape "$1")"
if [ -n "$ESCAPED" ]
then
echo "$(launchd_indent -$DEPTH "<string>$(launchd_escape "$1")</string>")"
else
echo "$(launchd_indent -$DEPTH "<string/>")"
fi
}
launchd_string_or_empty () {
local DEPTH=0
if launchd_is_depth_parameter "$1"
then
DEPTH=$(($1 * -1))
shift
fi
if [ -n "$1" ]
then
launchd_string -$DEPTH "$1"
fi
}
launchd_string_sequence () {
local DEPTH=0
if launchd_is_depth_parameter "$1"
then
DEPTH=$(($1 * -1))
shift
fi
local RESULT=
if [ $# -gt 1 ]
then
local IFS="$1"
shift
fi
local SEQUENCE="$1"
for ITEM in $SEQUENCE
do
if [ -z "$RESULT" ]
then
RESULT="$(launchd_string -$DEPTH "$ITEM")"
else
RESULT="$RESULT\n$(launchd_string -$DEPTH "$ITEM")"
fi
done
echo "$RESULT"
}
launchd_key () {
local DEPTH=0
if launchd_is_depth_parameter "$1"
then
DEPTH=$(($1 * -1))
shift
fi
echo "$(launchd_indent -$DEPTH "<key>$1</key>")"
}
systemd_escape () {
echo "$1" | perl -e 's/(["'\''\\])/\\$1/g' -p
}
systemd_quote () {
if [ $# -eq 0 ]
then
return
elif [ -z "$1" ]
then
echo '""'
return
fi
local ESCAPED="$(systemd_escape $1)"
if [ "$ESCAPED" = "$1" ]
then
echo "$ESCAPED"
else
echo "\"$ESCAPED\""
fi
}
systemd_quote_or_empty () {
if [ -n "$1" ]
then
systemd_quote "$1"
fi
}
systemd_quote_sequence () {
local RESULT=
if [ $# -gt 1 ]
then
local IFS="$1"
shift
fi
local SEQUENCE="$1"
for ITEM in $SEQUENCE
do
if [ -z "$RESULT" ]
then
RESULT="$(systemd_quote "$ITEM")"
else
RESULT="$RESULT $(systemd_quote "$ITEM")"
fi
done
echo "$RESULT"
}
case "$MODE" in
launchd)
ARGUMENTS="$(launchd_string_sequence -0 "$SEQUENCE_DELIMITER" "$ARGUMENTS")"
GROUP="$(launchd_string_or_empty -0 "$GROUP")"
JAR="$(launchd_string -0 "$JAR")"
JAVA="$(launchd_string -0 "$JAVA")"
OPTIONS="$(launchd_string_sequence -0 "$SEQUENCE_DELIMITER" "$OPTIONS")"
USER="$(launchd_string_or_empty -0 "$USER")"
WORKING_DIRECTORY="$(launchd_string -0 "$WORKING_DIRECTORY")"
;;
systemd)
ARGUMENTS="$(systemd_quote_sequence "$SEQUENCE_DELIMITER" "$ARGUMENTS")"
GROUP="$(systemd_quote_or_empty "$GROUP")"
JAR="$(systemd_quote "$JAR")"
JAVA="$(systemd_quote "$JAVA")"
OPTIONS="$(systemd_quote_sequence "$SEQUENCE_DELIMITER" "$OPTIONS")"
USER="$(systemd_quote_or_empty "$USER")"
WORKING_DIRECTORY="$(systemd_quote "$WORKING_DIRECTORY")"
esac
echo_launchd_plist () {
echo '<?xml version="1.0" encoding="UTF-8"?>'
echo '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">'
echo '<plist version="1.0">'
echo '<dict>'
echo "$(launchd_key -1 ExitTimeOut)"
echo "$(launchd_integer -1 90)"
if [ -n "$GROUP" ]
then
echo "$(launchd_key -1 GroupName)"
echo "$(launchd_indent -1 "$GROUP")"
fi
echo "$(launchd_key -1 KeepAlive)"
echo "$(launchd_indent -1 '<dict>')"
echo "$(launchd_key -2 SuccessfulExit)"
echo "$(launchd_false -2)"
echo "$(launchd_indent -1 '</dict>')"
echo "$(launchd_key -1 Label)"
echo "$(launchd_string -1 moe.minacle.minecraft.paperd)"
echo "$(launchd_key -1 ProgramArguments)"
echo "$(launchd_array -1 "$JAVA" "$OPTIONS" $(launchd_string -jar) "$JAR" "$ARGUMENTS")"
if [ -n "$USER" ]
then
echo "$(launchd_key -1 UserName)"
echo "$(launchd_indent -1 "$USER")"
fi
echo "$(launchd_key -1 WorkingDirectory)"
echo "$(launchd_indent -1 "$WORKING_DIRECTORY")"
echo '</dict>'
echo '</plist>'
}
echo_systemd_unit () {
echo '[Install]'
echo 'WantedBy=multi-user.target'
echo
echo '[Service]'
printf 'ExecStart=%s' "$JAVA"
if [ -n "$OPTIONS" ]
then
printf ' %s' "$OPTIONS"
fi
printf ' -jar %s' "$JAR"
if [ -n "$ARGUMENTS" ]
then
printf ' -- %s\n' "$ARGUMENTS"
else
echo
fi
if [ -n "$GROUP" ]
then
echo "Group=$GROUP"
fi
echo 'Restart=on-abnormal'
echo 'RestartSec=30'
echo 'SuccessExitStatus=143'
echo 'TimeoutStopSec=90'
echo 'Type=simple'
if [ -n "$USER" ]
then
echo "User=$USER"
fi
echo "WorkingDirectory=$WORKING_DIRECTORY"
echo
echo '[Unit]'
echo 'After=network.target'
echo 'Description=Paper Minecraft Server'
echo 'Wants=network-online.target'
}
case "$MODE" in
launchd)
echo "$(echo_launchd_plist)"
;;
systemd)
echo "$(echo_systemd_unit)"
esac