-
Notifications
You must be signed in to change notification settings - Fork 6
/
minecraft
executable file
·989 lines (851 loc) · 26.5 KB
/
minecraft
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
#!/bin/bash
### BEGIN INIT INFO
# Provides: minecraft
# Required-Start: $remote_fs $syslog $network
# Required-Stop: $remote_fs $syslog $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Minecraft server
# Description: Service for starting, stopping and checking
# the status of a Minecraft server.
### END INIT INFO
#/etc/init.d/minecraft
# Bash 4.2+
# Debug
#set -o verbose
# Name of script
name=$(basename $0)
# Remove startup/shutdown prefix used by /etc/rc*.d, if present.
# Prefix is Snn for startup or Knn for shutdown, where n is a digit.
prefix_regex="^(S|K)\d{2}"
if test `echo ${name} | grep -Pc ${prefix_regex}` -ne 0; then
name=`echo ${name} | grep -Po "(?<=${prefix_regex}).+"`
fi
# Read configuration variable file if it is present
if [ -r /etc/default/$name ]; then
source /etc/default/$name
else
# Default values
user="minecraft"
server="/home/$user/minecraft"
resources="/home/$user/minecraft-resources"
jar="minecraft_server.jar"
version="release"
eula="false"
mem_init="1024M"
mem_max="1024M"
java="$(which java)"
invocation="-Xms$mem_init -Xmx$mem_max -jar $jar nogui"
desc="Minecraft server"
fifo="$server/console.input"
log="$server/console.output"
fi
# Variables
resource_versions="$resources/versions"
log_start_regex="minecraft server version (.*)"
server_properties_file="$server/server.properties"
server_properties_regex="^([a-z-]*)=(.*)"
versions_json=http://s3.amazonaws.com/Minecraft.Download/versions/versions.json
# Define LSB log_* functions.
. /lib/lsb/init-functions
# Check user
if [ ! "$(id -u $user)" ]; then
log_end_msg 1
exit 1
fi
# Run as specified user
as_user() {
if [ $(whoami) = $user ]; then
bash -c "$1"
else
sudo -u $user /bin/bash -c "$1" || exit 1
fi
}
# Check if array contains value matching provided regex
array_contains() {
local array e value
value="$1"
declare -a array=("${!2}")
for e in "${array[@]}"; do
[[ "$value" =~ $e ]] && return 0
done
return 1
}
# Version timestamp
version_timestamp() {
# Usage
# version_timestamp "x.x.x"
# Gives back a printf of version timestamp for comparison, eg:
# [ $(version_timestamp "1.2.5") -lt $(version_timestamp "1.8") ]
local version id_regex releasetime releasetime_regex v
version="$1"
id_regex="\"id\": \"($version)\""
release_regex="\"releaseTime\": \"(.*)\""
as_user "mkdir -p $resources"
as_user "wget -qN -P $resources $versions_json"
v="false"
while read line; do
if [[ $line =~ $id_regex ]]; then
v="true"
fi
if [[ $v = "true" ]]; then
[[ $line =~ $release_regex ]]
[[ ! -z ${BASH_REMATCH[1]} ]] && v="false" && releasetime="${BASH_REMATCH[1]}" && break
fi
done <"$resources/versions.json"
printf "%s" $(date -d "$releasetime" +%s)
return 0
}
service_config() {
# Usage
# service_config <setting> <value>
# Return
# 0 if command was sent successfully
# 1 if command was unknown or failed
local setting=$1 value=$2 updated
# TODO: Add check, eg. if "#" or something fishy
updated="${setting}=\"${value}\""
sudo sed -i 's#^'"$setting"'.*#'"$updated"'#' /etc/default/$name
return 0
}
server_command() {
# Usage
# server_command "<command>"
# Return
# 0 if command was sent successfully
# 1 if command was unknown or failed
# 2 if command response is unknown
#
# For more accurate return value it's recommended to send only one command per second since thats the limit of the log timestamp.
local force failed message timestamp i
# Possible responses for specified commands (when completed).
# If this is filled, it gives a more certain return from function.
# Otherwise it just returns the latest message from server (which
# can be something else than a response to a specific command).
case "$1" in
"defaultgamemode "*)
message=(
"^The world's default game mode is now (.*)"
)
;;
"difficulty "*)
message=(
"^Set game difficulty to (.*)"
)
;;
"setidletimeout "*)
message=(
"^Successfully set the idle timeout to (.*) minutes."
)
;;
"whitelist off")
message=(
"^CONSOLE: Turned off white-listing"
"^Turned off the whitelist"
)
;;
"whitelist on")
message=(
"^CONSOLE: Turned on white-listing"
"^Turned on the whitelist"
)
;;
"whitelist reload")
message=(
"^CONSOLE: Reloaded white-list from file"
"^Reloaded the whitelist"
)
;;
esac
failed=(
"^'(.*)' is not a valid number"
"^The number you have entered \((.*)\) is too small, it must be at least (.*)"
)
unknown=(
"^Unknown console command. Type \"help\" for help."
"^Unknown command. Try /help for a list of commands" # 1.3.1 (1.3) and later
)
timestamp=$(date +%s)
log_modified=$(stat -c %Y $log)
timestamp_date=$(date +%F)
log_modified_date=$(date -d @$log_modified +%F)
# If logfile is from yesterday or older, start a new logfile so old entries aren't considered as current.
# This is due to the absense of dates in Minecraft log files
if [ ! $timestamp_date = $log_modified_date ]; then
if server_version; then
echo "[$(date +%T)] [Init script/INFO]: Current minecraft server version $version_current" > $log
else
echo "[$(date +%T)] [Init script/INFO]: Unknown server version" > $log
fi
echo "[$(date +%T)] [Init script/INFO]: Regenerated $log at $timestamp" >> $log
echo "[$(date +%T)] [Init script/INFO]: For earlier logentries, see $server/logs" >> $log
as_user "printf \"%s\n\" \"list\" > $fifo"
sleep 1
timestamp=$(date +%s)
as_user "printf \"%s\n\" \"$1\" > $fifo"
#sed -i 's/\x0//g' $log
else
as_user "printf \"%s\n\" \"$1\" > $fifo"
fi
if server_version; then
tail -f $log | while read log_line; do
server_log "$log_line"
# TODO: Debug line to be deleted in the future
# echo "Logline: $log_line_timestamp, Timestamp: $timestamp"
# If log timestamp is less than command timestamp, skip iteration
[[ $log_line_timestamp -lt $timestamp ]] && continue
if array_contains "$log_line_message" message[@]; then
printf " %s\n" "$log_line_message"
kill $(ps -o pid,cmd --no-headers --ppid $$ | grep tail | awk '{print $1}')
return 0
elif array_contains "$log_line_message" failed[@]; then
printf " %s\n" "$log_line_message"
kill $(ps -o pid,cmd --no-headers --ppid $$ | grep tail | awk '{print $1}')
return 1
elif array_contains "$log_line_message" unknown[@]; then
printf " %s\n" "Unknown console command"
kill $(ps -o pid,cmd --no-headers --ppid $$ | grep tail | awk '{print $1}')
return 1
elif [[ ${#message[@]} -eq 0 ]]; then
printf " %s\n" "$log_line_message"
kill $(ps -o pid,cmd --no-headers --ppid $$ | grep tail | awk '{print $1}')
return 2
else
((i++))
# Check timeout
fi
# Return 2 if response was not found in 4 lines
if (( i > 3 )); then
kill $(ps -o pid,cmd --no-headers --ppid $$ | grep tail | awk '{print $1}')
return 2
fi
done
else
printf " %s\n" "Sent command: $1"
return 2
fi
}
server_is_running() {
# Return
# 0 if server is running
# 1 if server is not running
if [ -z "$(ps aux | grep -v grep | grep "$java.*nuid=$name-nuid")" ]; then
return 1
else
if [ -z "$(ps aux | grep -v grep | grep "$java.*nuid=$name-nuid" | grep -v "bash")" ]; then
return 1
else
return 0
fi
fi
}
server_download() {
# Usage
# server_download <version>
# Return
# 0 if download succeeded
# 1 if download failed
local version_download server_url
version_download="$1"
server_url="https://s3.amazonaws.com/Minecraft.Download/versions/$version_download/minecraft_server.$version_download.jar"
as_user "mkdir -p $resource_versions"
if [ $(whoami) = $user ]; then
printf " %s" "Downloading..."
wget --progress=dot -N -P $resource_versions $server_url 2>&1 | \
grep -oP --line-buffered "(\d{1,3})%" | \
while read line; do
printf "\r %s %s" "Downloading..." "$line"
done; test ${PIPESTATUS[0]} -eq 0 && printf "\r %s %s\n" "Downloading..." "100%" || printf "\r %s %s\n" "Downloading..." "failed"
else
sudo -u $user /bin/bash -c "wget -N -P $resource_versions $server_url"
sudo -u $user /bin/bash -c "chown $user:$user $resource_versions/minecraft_server.$version_download.jar"
fi
case "$?" in
1) return 1 ;;
esac
return 0
}
server_eula() {
# Return
# 0 if eula.txt was written
# 1 if eula.txt was not written
as_user "printf \"eula=true\" > \"$server/eula.txt\"" &> /dev/null || return 1
return 0
}
server_log() {
# Usage
# server_log <log line>
# Return
# 0 if trying to parse logfile
# 1 if not trying to parse logfile
local log_line_input log_line_regex
log_line_input=$1
if server_version; then
case $version_current in
"rd-132211"|"rd-132328"|"rd-160052"|"rd-161348"|"c0.0.11a"|"c0.0.13a"|"c0.0.13a_03"|"c0.30_01c"|"inf-20100618"|"a1.0.4"|"a1.0.5_01"|"a1.0.11"|"a1.0.14"|"a1.0.15"|"a1.0.16"|"a1.0.17_02"|"a1.0.17_04"|"a1.1.0"|"a1.1.2"|"a1.1.2_01"|"a1.2.0"|"a1.2.0_01"|"a1.2.0_02"|"a1.2.1"|"a1.2.1_01"|"a1.2.2a"|"a1.2.2b"|"a1.2.3"|"a1.2.3_01"|"a1.2.3_02"|"a1.2.3_04"|"a1.2.4_01"|"a1.2.5"|"a1.2.6"|"b1.0"|"b1.0_01"|"b1.0.2"|"b1.1_01"|"b1.1_02"|"b1.2"|"b1.2_01"|"b1.2_02"|"b1.3b"|"b1.3_01"|"b1.4"|"b1.4_01"|"b1.5"|"b1.5_01"|"b1.6"|"b1.6.1"|"b1.6.2"|"b1.6.3"|"b1.6.4"|"b1.6.5"|"b1.6.6"|"b1.7"|"b1.7.2"|"b1.7.3"|"b1.8"|"b1.8.1"|"1.0"|"1.1"|"1.2.1"|"1.2.2"|"1.2.3"|"1.2.4"|"1.2.5"|"1.3"|"1.3.1"|"1.3.2"|"1.4"|"1.4.1"|"1.4.2"|"1.4.3"|"1.4.4"|"1.4.5"|"1.4.6"|"1.4.7"|"1.5"|"1.5.1"|"1.5.2"|"1.6"|"1.6.1"|"1.6.2"|"1.6.4")
log_line_regex="(....-..-..) (..:..:..) \[(.{0,4})\] (.*)"
[[ $log_line_input =~ $log_line_regex ]]
log_line_date="${BASH_REMATCH[1]}"
log_line_time="${BASH_REMATCH[2]}"
# log_line_thread=""
log_line_type="${BASH_REMATCH[3]}"
log_line_message="${BASH_REMATCH[4]}"
log_line_timestamp=$(date -d "$log_line_date $log_line_time" +%s)
;;
"1.7"|*)
log_line_regex="\[(..:..:..)\] \[(.*)/(.{0,4})\]: (.*)"
[[ $log_line_input =~ $log_line_regex ]]
log_line_date=""
log_line_time="${BASH_REMATCH[1]}"
# log_line_thread="${BASH_REMATCH[2]}"
log_line_type="${BASH_REMATCH[3]}"
log_line_message="${BASH_REMATCH[4]}"
# Create log timestamp from log time in hh:mm:ss + todays date
log_line_timestamp=$(date -d "$(date +%F) $log_line_time" +%s)
;;
esac
return 0
else
return 1
fi
}
server_properties() {
# Return
# 0 if properties were stored in associative array server_setting
# 1 if properties were not stored in associative array server_setting
local setting value
declare -Ag server_setting
# Parse server.properties
while read properties_line; do
[[ $properties_line =~ $server_properties_regex ]] || continue
setting="${BASH_REMATCH[1]}"
value="${BASH_REMATCH[2]}"
server_setting["$setting"]="$value"
done <$server_properties_file || return 1
return 0
}
server_properties_update() {
# Usage
# server_properties_update <setting> <value>
# Return
# 0 if command was sent successfully
# 1 if specified setting does not exist
local setting=$1 value=$2 updated
# TODO: Add check, eg. if "#" or something fishy
updated="${setting}=${value}"
server_properties
if [[ ! ${server_setting[$setting]} ]]; then
printf " %s\n" "Error: The setting $setting does not exist in $server_properties_file";
return 1
fi
sed -i 's#^'"$setting"'.*#'"$updated"'#' $server_properties_file
return 0
}
server_reload() {
# Return
# 0 if server has been reloaded
# 1 if server could not be reloaded
local whitelist
server_properties
if [[ "${server_setting[white-list]}" = "false" ]]; then
whitelist="off"
elif [[ "${server_setting[white-list]}" = "true" ]]; then
whitelist="on"
else
log_daemon_msg "Reading white-list setting in $server/server.properties"
return 1
fi
if server_version; then
if server_version_minimum "12w32a"; then
server_command "difficulty ${server_setting[difficulty]}"
sleep 1
fi
if server_version_minimum "12w22a"; then
server_command "defaultgamemode ${server_setting[gamemode]}"
sleep 1
fi
if server_version_minimum "13w38a"; then
server_command "setidletimeout ${server_setting[player-idle-timeout]}"
sleep 1
fi
server_command "whitelist $whitelist"
sleep 1
server_command "whitelist reload"
sleep 1
fi
return 0
}
server_start() {
# Return
# 0 if server has been started
# 1 if server could not be started
local timestamp
timestamp=$(date '+%s')
[ -d "$server" ] || as_user "mkdir -p $server"
[ -f "$server/$jar" ] || exit 1
cd $server || exit 1
# Recreate fifo
[ -e "$fifo" ] && as_user "rm $fifo"
as_user "mkfifo $fifo"
if [[ $eula = "true" ]]; then
server_eula
fi
# Start server
as_user "tail -n +1 -f --pid=\$$ $fifo | { $java $invocation nuid=$name-nuid &> $log; kill \$$; }" &> /dev/null &
# Wait until current log exists
printf " %s\n" "Initializing..."
until [ -r $log ] && [ $(date +%s -r $log ) -gt $timestamp ]; do
sleep 1
done
# Read log until started
tail -f $log | while read log_line; do
server_log "$log_line"
case $log_line in
*"Done ("*")! For help, type \"help\" or \"?\"")
kill $(ps -o pid,cmd --no-headers --ppid $$ | grep tail | awk '{print $1}')
return 0
;;
*"Starting minecraft server"*)
if server_version "update"; then
printf " %s\n" "Server version: $version_current"
fi
;;
*"To start the server with more ram, launch it as \"java -Xmx1024M -Xms1024M -jar minecraft_server.jar\"")
printf " %s\n" "To start the server with more ram, edit /etc/default/$name"
;;
*"You need to agree to the EULA in order to run the server. Go to eula.txt for more info.")
printf " %s\n" "$log_line_message"
printf " %s\n" "Shutting down server"
as_user "printf \"%s\n\" \"stop\" > $fifo"
kill $(ps -o pid,cmd --no-headers --ppid $$ | grep tail | awk '{print $1}')
return 1
;;
*"**** FAILED TO BIND TO PORT!")
printf " %s\n" "$log_line_message"
printf " %s\n" "Shutting down server"
as_user "printf \"%s\n\" \"stop\" > $fifo"
kill $(ps -o pid,cmd --no-headers --ppid $$ | grep tail | awk '{print $1}')
return 1
;;
*)
[ -z "$log_line_message" ] || printf " %s\n" "$log_line_message"
;;
esac
done
}
server_stop() {
# Return
# 0 if server has been stopped
# 1 if server could not be stopped
server_command "save-all"
sleep 1
server_command "stop"
# Sleep until server is stopped
while kill -0 "$(ps aux | grep -v grep | grep "$java.*nuid=$name-nuid" | grep -v "bash" | awk '{print $2}')" &> /dev/null; do
sleep 1
done
return 0
}
server_update() {
# Usage
# server_update <version>
# Return
# 0 if server has been updated
# 1 if server has not been updated
local version_update
version_update="$1"
# TODO: read as user
if [[ -r "$resource_versions/minecraft_server.$version_update.jar" ]]; then
as_user "rm $server/$jar && ln $resource_versions/minecraft_server.$version_update.jar $server/$jar"
return 0
else
return 1
fi
}
server_update_check() {
# Return
# 0 if there is a version available for download
# 1 if theres no difference between online and local version numbers
# 2 if an error occured (prints error message)
local latest_regex line
if [ "$version" = "release" -o "$version" = "snapshot" ]; then
latest_regex="\"($version)\": \"(.*)\""
as_user "mkdir -p $resources" || return 2
# TODO: wget error message
as_user "wget -qN -P $resources $versions_json" || return 2
while read line; do
if [[ $line =~ $latest_regex ]]; then
version_online="${BASH_REMATCH[2]}"
break
fi
done <"$resources/versions.json"
if [ -z "$version_online" ]; then
log_failure_msg "Could not determine latest $version version."
return 2
fi
if server_version; then
if [ "$version_current" = "$version_online" ]; then
return 1
else
return 0
fi
else
log_failure_msg "Could not determine local server version from log."
return 2
fi
elif online_version_exists "$version"; then
if server_version; then
if [ "$version_current" = "$version" ]; then
return 1
else
if online_version_downloadable "$version"; then
version_online="$version"
return 0
else
log_failure_msg "Specified version ($version) is not available for download."
return 2
fi
fi
else
log_failure_msg "Could not determine local server version from log."
return 2
fi
else
log_failure_msg "Invalid version value in configuration file: $version"
return 2
fi
}
server_version() {
# Return
# 0 if version value is stored in $version_current
# 1 if server $version_current could not be determined
local opt=$1 line
if [[ ! $opt = "update" ]]; then
[[ ! -z "$version_current" ]] && return 0
fi
[[ -r $log ]] || return 1
while read line; do
[[ $line =~ $log_start_regex ]]
[[ ! -z ${BASH_REMATCH[1]} ]] && version_current="${BASH_REMATCH[1]}" && break
done <$log || return 1
return 0
}
server_version_minimum() {
# Usage
# server_version_minimum <x.x.x>
# Return
# 0 if $version_current is more than or equal to compared version x.x.x
# 1 if comparison failed
if server_version; then
if [ "$(version_timestamp \"$version_current\")" -ge "$(version_timestamp \"$1\")" ]; then
return 0
else
return 1
fi
else
return 1
fi
}
online_version_downloadable() {
# Usage
# online_version_downloadable <x.x.x>
# Description
# Checks if provided version is downloadable from server
# Return
# 0 if server version x.x.x is downloadable
# 1 if server version x.x.x is not downloadable or check failed
local download_url statuscode version="$1"
download_url="https://s3.amazonaws.com/Minecraft.Download/versions/$version/minecraft_server.$version.jar"
statuscode=$(wget --spider -S "$download_url" 2>&1 | grep "HTTP/" | awk '{print $2}')
if [ "$statuscode" = "200" ]; then
return 0
else
return 1
fi
}
online_version_exists() {
# Usage
# online_version_exists <x.x.x>
# Return
# 0 if server version x.x.x exists
# 1 if server version x.x.x does not exists
# 2 if there was an error saving or downloading versions.json
local id_regex line version="$1" version_exists
as_user "mkdir -p $resources" || return 2
as_user "wget -qN -P $resources $versions_json" || return 2
id_regex="\"id\": \"($version)\""
while read line; do
if [[ $line =~ $id_regex ]]; then
version_exists="${BASH_REMATCH[1]}"
break
fi
done <"$resources/versions.json"
if [ ! -z $version_exists ]; then
return 0
else
return 1
fi
}
case "$1" in
start)
server_is_running
case "$?" in
0) log_daemon_msg "$desc is already running" ;;
1)
log_daemon_msg "Starting $desc"
server_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1; exit 1 ;;
esac
;;
esac
;;
stop)
server_is_running
case "$?" in
0)
log_daemon_msg "Stopping $desc"
server_stop
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1; exit 1 ;;
esac
;;
1) log_daemon_msg "$desc is not running" ;;
esac
;;
status)
server_is_running
case "$?" in
0) log_daemon_msg "$desc is running"; exit 0 ;;
1) log_daemon_msg "$desc is not running"; exit 3 ;;
esac
;;
reload)
server_is_running
case "$?" in
0)
log_daemon_msg "Reloading $desc"
server_reload
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1; exit 1 ;;
esac
;;
1) log_daemon_msg "$desc is not running" ;;
esac
;;
restart|force-reload)
server_is_running
case "$?" in
0)
log_daemon_msg "Restarting $desc"
server_stop
case "$?" in
0)
log_daemon_msg "$desc stopped, starting again"
server_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1; exit 1 ;;
esac
;;
1) log_end_msg 1; exit 1 ;;
esac
;;
1)
log_daemon_msg "Starting $desc"
server_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1; exit 1 ;;
esac
;;
esac
;;
# Special commands
command)
server_is_running
case "$?" in
0)
log_daemon_msg "Sending command to $desc"
server_command "${*:2}"
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1; exit 1 ;;
2) exit 0 ;;
esac
;;
1) log_daemon_msg "$desc is not running" ;;
esac
;;
config)
server_is_running
case "$?" in
0)
log_daemon_msg "$desc is running, restart server for changes to take effect."
service_config "$2" "$3"
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1; exit 1 ;;
esac
;;
1)
log_daemon_msg "Updating $desc init script configuration."
service_config "$2" "$3"
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1; exit 1 ;;
esac
;;
esac
;;
properties)
if [ ! $2 ]; then
log_daemon_msg "No setting specified. Usage: service minecraft properties <setting> [new value]"
exit 0
elif [ ! $3 ]; then
server_properties
log_daemon_msg "Currect setting for $2:"
if [[ ! ${server_setting[$2]} ]]; then
log_end_msg 1
exit 1
fi
printf " %s\n" $2=${server_setting[$2]}
exit 0
fi
server_is_running
case "$?" in
0)
log_daemon_msg "Updating $desc properties."
server_properties_update "$2" "$3"
case "$?" in
0) printf " %s\n" "Note: $desc is running, restart server for changes to take effect."; log_end_msg 0 ;;
1) log_end_msg 1; exit 1 ;;
esac
;;
1)
log_daemon_msg "Updating $desc properties."
server_properties_update "$2" "$3"
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1; exit 1 ;;
esac
;;
esac
;;
check-update)
server_update_check
case "$?" in
0)
if [ $version = "release" -o $version = "snapshot" ]; then
log_daemon_msg "New $version available: $version_online"
else
log_daemon_msg "Specified version ($version) is available for download."
fi
;;
1)
if [ $version = "release" -o $version = "snapshot" ]; then
log_daemon_msg "$desc is running latest $version ($version_current)"
else
log_daemon_msg "$desc is running specified version in configuration ($version_current)"
fi
;;
2) log_end_msg 1; exit 1 ;;
esac
;;
update)
log_daemon_msg "Checking for version tagged \"$version\""
server_update_check
case "$?" in
0)
server_is_running
case "$?" in
0) # Update procedure when server is running
# 1 Download
log_daemon_msg "Downloading minecraft_server.$version_online.jar"
server_download "$version_online"
case "$?" in
1) log_end_msg 1; exit 1 ;;
esac
# 2 Stop
log_daemon_msg "Stopping $desc"
server_stop
case "$?" in
1) log_end_msg 1; exit 1 ;;
esac
# 3 Update
log_daemon_msg "Installing version $version_online"
server_update "$version_online"
case "$?" in
1) log_end_msg 1; exit 1 ;;
esac
# 4 Start
log_daemon_msg "Starting $desc"
server_start
case "$?" in
0)
log_daemon_msg "Successfully installed version $version_online"
log_end_msg 0
;;
1) log_end_msg 1; exit 1 ;;
esac
;;
1) # Update procedure when server is not running
# 1 Download
log_daemon_msg "Downloading minecraft_server.$version_online.jar"
server_download "$version_online"
case "$?" in
1) log_end_msg 1; exit 1 ;;
esac
# 2 Update
log_daemon_msg "Installing version $version_online"
server_update "$version_online"
case "$?" in
0)
as_user "printf \"%s\n\" \"Starting minecraft server version $version_online\" > $log"
log_daemon_msg "Successfully installed version $version_online"
log_end_msg 0
;;
1) log_end_msg 1; exit 1 ;;
esac
;;
esac
;;
1)
if [ $version = "release" -o $version = "snapshot" ]; then
log_daemon_msg "$desc is running latest $version ($version_current)"
else
log_daemon_msg "$desc is running specified version defined in configuration ($version_current)"
fi
;;
2) log_end_msg 1; exit 1 ;;
esac
;;
version)
server_is_running
case "$?" in
0)
if server_version; then
log_daemon_msg "$desc is running version: $version_current"
else
log_daemon_msg "Could not find version of $desc"
fi
;;
1)
if server_version; then
log_daemon_msg "$desc has version $version_current installed."
else
log_daemon_msg "$desc is not running"
fi
;;
esac
;;
# Catch all
help|*)
printf "%s\n" "Usage: service $name {start|stop|status|restart|reload|command|config|properties|check-update|update|version|help}" >&2
exit 3
;;
esac
exit 0