-
Notifications
You must be signed in to change notification settings - Fork 0
/
spacecp
executable file
·994 lines (944 loc) · 44.4 KB
/
spacecp
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
990
991
992
993
994
#!/bin/sh
######################################################################
######################################################################
## ##
## _____ ______ ____ ##
## / ___/ ____ ____ _ _____ ___ / ____// __ \ ,: ##
## \__ \ / __ \ / __ `// ___// _ \ / / / /_/ / ,' | ##
## ___/ // /_/ // /_/ // /__ / __// /___ / ____/ / : ##
## /____// .___/ \__,_/ \___/ \___/ \____//_/ __' / ##
## v0.2 /_/ \/ />/ ##
## / <//_\ ##
## Copyright XereoNet 2014 __/ / ##
## http://xereo.net )'-. / ##
## All rights reserved ./ :\ ##
## /.' ' ##
## Author: Alexander Schulz '/' ##
## Web : https://nope.bz/ + ##
## Github: https://github.com/tubersan ' ##
## Email : [email protected] `. ##
## .-"- ##
## ( | ##
## . .-' '. ##
## ( (. )8: ##
## .' / (_ ) ##
## _. :(. )8P ` ##
## . ( `-' ( `. . ##
## . : ( .a8a) ##
## ##
######################################################################
######################################################################
shesc(){ awk '{ gsub(/(\x27|\$|&|>|<|`|\(|\)|\||!|;|:|\*|\{|}|\[|]|=| |"|\\)/,"\\\\&"); \
o=o (NR!=1?"\n":"") $0 } \
END{ printf o }' /dev/stdin; }
# Dem keys!!
SPACECP_APIKEY=""
# Mmh more tasty variables...
SPACECP_SERVID=""
# If you haven't guessed already, I'm going to declare all the important variables here
SPACECP_URL="http://spacecp.net"
# Yes, yes, variables, good, good
SPACECP_SERVJAR="craftbukkit.jar"
# There are a bunch more...
SPACECP_CONFFILE="SpaceCP/config.yml"
# Yup, and more...
SPACECP_RTKJAR="remotetoolkit.jar"
# ...
SPACECP_SMJAR="toolkit/modules/spacemodule.jar"
# soo...
SPACECP_RPJAR="plugins/rtkplugin.jar"
# glaring display last night, eh?
SPACECP_DLAPIURL="http://dl.api.xereo.net/v1"
# pls respond ;-;
SPACECP_GDNAPIURL="http://gdn.api.xereo.net/v1"
SPACECP______="0.$(((5*4*10)/(4*5*5)))"
[ "$_" != "$0" ] && SOURCED=0 || SOURCED=1
___=""
____=""
_____=""
_____=$(awk 'BEGIN{printf "\x2d\x2d\x65\x74\x63"}')
____=$(awk 'BEGIN{printf "\x6c\x6f\x6c"}')
___=$(awk 'BEGIN{printf "\x75\x72\x20\x61\x20\x66\x67\x74"}')
# Change the following variables if you want to run multiple servers
# IMPORTANT: Must be unique for each server
# DEFAULT: "spacecp.pid"
[ -z "$SPACECP_PIDFILE" ] && SPACECP_PIDFILE="spacecp.pid"
# DEFAULT: "SpaceCP"
[ -z "$SPACECP_SESSION" ] && SPACECP_SESSION="SpaceCP"
# Change the following three variables to use a custom start command
# IMPORTANT: Arguments must be in a string!
# Leave this line be to make environment overrides possible
SPACECP_1=$SPACECP_STARTCOMMAND; SPACECP_2=$SPACECP_STARTARG; SPACECP_3=$SPACECP_STARTEND
# DEFAULT: "start-stop-daemon"
SPACECP_STARTCOMMAND="start-stop-daemon"
# DEFAULT: "--start --pidfile '$SPACECP_PIDFILE' --chdir $(pwd | shesc) --background --make-pidfile --exec"
SPACECP_STARTARG="--start --pidfile '$SPACECP_PIDFILE' --chdir $(pwd | shesc) --background --make-pidfile --exec"
# DEFAULT: ""
SPACECP_STARTEND=""
# Also comment out those if blocks to not make it overwrite it accidentally
if command -v tmux >/dev/null 2>&1
then
# DEFAULT: "tmux"
SPACECP_STARTCOMMAND="tmux"
# DEFAULT: "new-session -d -s '$SPACECP_SESSION' \"echo \\\$PPID > '$SPACECP_PIDFILE'; "
SPACECP_STARTARG="new-session -d -s '$SPACECP_SESSION' \"echo \\\$PPID > '$SPACECP_PIDFILE'; "
# DEFAULT: "\""
SPACECP_STARTEND="\""
elif command -v screen >/dev/null 2>&1
then
# DEFAULT: "screen"
SPACECP_STARTCOMMAND="screen"
# DEFAULT: "-dmLS '$SPACECP_SESSION' sh -c \"echo \\\$PPID > '$SPACECP_PIDFILE'; "
SPACECP_STARTARG="-dmLS '$SPACECP_SESSION' sh -c \"echo \\\$PPID > '$SPACECP_PIDFILE'; "
# DEFAULT: "\""
SPACECP_STARTEND="\""
fi
# Override if they are given as environment variables
[ -n "$SPACECP_1" ] && SPACECP_STARTCOMMAND=$SPACECP_1
[ -n "$SPACECP_2" ] && SPACECP_STARTARG=$SPACECP_2
[ -n "$SPACECP_3" ] && SPACECP_STARTEND=$SPACECP_3
# Variable for custom args passed to RTK
# IMPORTANT: Arguments must be in a string!
# DEFAULT: ""
[ -z "$SPACECP_RTKARGS" ] && SPACECP_RTKARGS=""
# Variable pointing to Javas binary
# DEFAULT: "java"
[ -z "$SPACECP_JAVABIN" ] && SPACECP_JAVABIN="java"
# Variable for custom args passed to Java
# It is suggested to only change this if you are certain of what you are doing.
# IMPORTANT: Arguments must be in a string!
# DEFAULT: "-Djava.library.path=./libs/ -jar"
[ -z "$SPACECP_JAVAARGS" ] && SPACECP_JAVAARGS="-Djava.library.path=./libs/ -jar"
ultima_yes=0 # Never say no! ...or was it never...
force_update=0 # 0 nothing, 1 update, 2 install
o='/dev/null' # Where useless commands output is redirected to, /dev/null is very much recommended.
# If debugging is needed, rather set the -x flag (sh -x spacecp or add set -x und the shebang).
show_help () { # Big ass help/documentation
printf "USAGE: spacecp -k <apikey> -i <serverid> [-u <spacecpurl>] [-du <dlapiurl>] [-gu <gdnapiurl>] [-c <config>]\n"
printf " [-sj <serverjar>] [-rj <rtkjar>] [-mj <modulejar>] [-pj <pluginjar>] [-s/-S] [-U] [-I] [-y] [-h]\n"
printf "\n"
printf "\n"
printf "DESCRIPTION:\n"
printf "\n"
printf " The SpaceCP script installs, updates and starts a minecraft server with all the needed files for SpaceCP.\n"
printf " It can be used to upgrade an existing server to use it with SpaceCP or install a new one from scratch.\n"
printf " It will per default automatically start the server in the background with either start-stop-daemon,\n"
printf " screen or tmux (favoring tmux over screen over start-stop-daemon), but other tools can be specified in the\n"
printf " script with the SPACECP_STARTCOMMAND (PATH to the binary), SPACECP_STARTARG (arguments passed after the\n"
printf " startcommand, but before all the other commands) and SPACECP_STARTEND (arguments passed after everything else).\n"
printf " It will also keep the RemoteToolkit, RTK Plugin, SpaceCP Module jar and the SpaceCP Libraries\n"
printf " updated.\n"
printf " For common usage examples, see EXAMPLES, for all possible commands see OPTIONS and FLAGS,\n"
printf " see also NOTES for helpful and important notes on the different options and flags.\n"
printf "\n"
printf "\n"
printf "EXAMPLES:\n"
printf "\n"
printf " spacecp -k 0123456789abcdef -i fedcba9876543210 -y\n"
printf " Will install SpaceCP with the API Key 0123456789abcdef and the Server ID fedcba9876543210 if it is not\n"
printf " already installed, and will not prompt on overrides.\n"
printf "\n"
printf " spacecp -U -y\n"
printf " Will first update the RemoteToolkit, RTK Plugin, SpaceCP Module jar and SpaceCP Libraries,\n"
printf " not prompting to overwrite them, and will then start the server, even if the Update was unsuccessful.\n"
printf "\n"
printf " spacecp\n"
printf " Same as \`spacecp start\`\n"
printf " Will simply start the server. It may prompt if an automatic update failed,\n"
printf " so use with the -y flag inside init scripts is advised.\n"
printf "\n"
printf " spacecp -s\n"
printf " Same as \`spacecp stop\`\n"
printf " Will peacefully stop the server.\n"
printf " (Use upper case -S or force-stop to forcefully stop it if it doesn't respond.)\n"
printf "\n"
printf "\n"
printf "COMMANDS:\n"
printf "\n"
printf " Commands are special arguments that can be passed to the script to make it easier and more intuitive to use.\n"
printf " The following commands are possible:\n"
printf "\n"
printf " help\n"
printf " Shows this help and exits.\n"
printf "\n"
printf " start\n"
printf " Will simply start update and start spacecp. Same as if no arguments were provided.\n"
printf "\n"
printf " stop\n"
printf " Will try to stop the server peacefully.\n"
printf "\n"
printf " force-stop\n"
printf " Will stop the server with SIGKILL.\n"
printf "\n"
printf " update\n"
printf " Will force an update (overwrite all current files with newly downloaded ones).\n"
printf " WARNING: All current files WILL be overwritten!\n"
printf "\n"
printf " install\n"
printf " Will force a new install.\n"
printf " WARNING: All current files WILL be overwritten!\n"
printf "\n"
printf "\n"
printf "OPTIONS:\n"
printf "\n"
printf " -k, --api-key <apikey>\n"
printf " Set the API Key to <apikey>.\n"
printf " This option is needed, see NOTES for more information.\n"
printf "\n"
printf " -i, --server-id <serverid>\n"
printf " Set the Server ID to <serverid>.\n"
printf " This option is needed, see NOTES for more information.\n"
printf "\n"
printf " -u, --url <spacecpurl>\n"
printf " Set the SpaceCP URL to <spacecpurl>.\n"
printf " Default: http://spacecp.net\n"
printf "\n"
printf " -du, --dl-url <dlapiurl>\n"
printf " Set the DL API URL to <dlapiurl>.\n"
printf " Default: http://dl.api.xereo.net/v1\n"
printf "\n"
printf " -gu, --gdn-url <gdnapiurl>\n"
printf " Set the GDN API URL to <gdnapiurl>.\n"
printf " Note, that the updating and installing processes are explicitely hardcoded for the latest GDN API as by\n"
printf " 'https://github.com/connor4312/SpaceGDN' currently and thus changing the GDN API URL is currently\n"
printf " NOT recommended.\n"
printf " Default: http://gdn.api.xereo.net/v1\n"
printf "\n"
printf " -c, --config <config>\n"
printf " Set the SpaceCP Configuration PATH to <config>.\n"
printf " Default: SpaceCP/config.yml\n"
printf "\n"
printf " -sj, --server-jar <serverjar>\n"
printf " Set the Minecraft Server Jar PATH to <serverjar>.\n"
printf " Note, that currently only Craftbukkit is supported for automatic installing!\n"
printf " Default: craftbukkit.jar\n"
printf "\n"
printf " -rj, --rtk-jar <rtkjar>\n"
printf " Set the RemoteToolkit Jar PATH to <rtkjar>.\n"
printf " Default: remotetoolkit.jar\n"
printf "\n"
printf " -mj, --module-jar <modulejar>\n"
printf " Set the SpaceCP RTK Module Jar PATH to <modulejar>.\n"
printf " Default: tookit/modules/spacemodule.jar\n"
printf "\n"
printf " -pj, --plugin-jar <pluginjar>\n"
printf " Set the RemoteToolkit Plugin Jar PATH to <pluginjar>.\n"
printf " Default: SpaceCP/config.yml\n"
printf "\n"
printf "\n"
printf "FLAGS:\n"
printf "\n"
printf " -U, --force-update\n"
printf " Force an update.\n"
printf " WARNING: The forced update WILL overwrite any current RemoteToolkit, RTK Plugin, SpaceCP Module jar\n"
printf " and SpaceCP Libraries. This can NOT be reverted.\n"
printf " This will also NOT delete any old files if they do not get overwritten by new ones, so make sure to\n"
printf " delete files not being overwritten (i.e. currently only SpaceCP Library files) beforehand.\n"
printf " The forced update will fetch new builds from the channels specified inside the SpaceCP Configuration.\n"
printf "\n"
printf "\n"
printf " -I, --force-install\n"
printf " Force an installation.\n"
printf " WARNING: The forced installation WILL overwrite any current Minecraft Server jar, SpaceCP Configuration,\n"
printf " RemoteToolkit, RTK Plugin, SpaceCP Module jar and SpaceCP Libraries. This can NOT be reverted.\n"
printf " Just like the forced update, this will also NOT delete any old files (i.e. currently SpaceCP Library\n"
printf " files and SpaceCP Configuration files) if they do not get overwritten by new ones.\n"
printf " Unlike the forced update, the forced installation will not fetch the build given in the\n"
printf " SpaceCP Configuration, but always the latest RECOMMENDED build.\n"
printf " Unlike a fresh installation, the forced installation does usually not need the API Key\n"
printf " and Server ID specified, as those should already be present in the old SpaceCP Configuration.\n"
printf " You will ONLY need to specify them, if you want to use a NEW API Key or Server ID.\n"
printf "\n"
printf " -y, --always-yes\n"
printf " Always answer YES on prompts.\n"
printf " This is useful when calling the script inside f.i. an init script,\n"
printf " or if you just can't be bothered hitting enter.\n"
printf "\n"
printf " -s, -S, --stop, --force-stop\n"
printf " Stops the running server in the screen/tmux session or background if started with start-stop-daemon.\n"
printf " -s will first send SIGTERM (ask the server to stop peacefully).\n"
printf " If the server still doesn't stop you can force it with -S which will send SIGKILL (stop it forcefully).\n"
printf "\n"
printf " -h, -?, --help\n"
printf " Shows this help and exits.\n"
printf "\n"
printf "NOTES:\n"
printf "\n"
printf " All PATHS can be an absolute PATH or relative to the location the script is run from.\n"
printf " E.g. /opt/minecraft/SpaceCP/config.yml OR SpaceCP/config.yml if the script is run from /opt/minecraft.\n"
printf " Note that not the location the script is saved in, but the location the script is run from is important.\n"
printf " If the script is saved in /opt/minecraft, but run from inside /tmp, the PATH will be relative to /tmp!\n"
printf "\n"
printf " All URLS can be any valid Address. E.g. and IP Address like 127.0.0.1,\n"
printf " or a Domainname based Address like http://xereo.net. The given URL does not need a trailing slash '/'.\n"
printf "\n"
printf " Both the Server ID and the API Key must be in an alphanumeric, hexadecimal format like 0123456789abcdef.\n"
printf " Accepted characters are numbers from 0 to 9 and letters from a to f in upper and lower case.\n"
printf " Both Server ID and API Key MUST be given on the first startup!\n"
printf " (i.e. if they are not present inside the specified SpaceCP configuration file)\n"
printf "\n"
printf " If an old temporary folder is found on the the start of the script (temporary folders will have a pattern of\n"
printf " 'spacecptmp_XXXXXXXXXX', where X can be any alphanumeric character), the user is prompted to delete it.\n"
printf " This prompt will NOT be automatically answered with the -y flag.\n"
printf " An old temporary folder will usually exist, if the script exited with an error during installation,\n"
printf " if this is the case, it can safely be deleted and the script can be started with the -I flag to overwrite\n"
printf " residual files from the first installation attempt.\n"
printf "\n"
printf " All FLAGS can be given in the short format (f.i. -h or -?) or the long format (f.i. --help)\n"
printf " but do not take any arguments. FLAGS can NOT be concatenated (e.g. -yU will NOT work).\n"
printf " All OPTIONS can be given in the short format with the argument as the next option (f.i. -u http://spacecp.net),\n"
printf " in the long format with the argument as the next option (f.i. --url http://spacecp.net)\n"
printf " or in the long format with the argument \"assigned\" to the option (f.i. --url=http://spacecp.net).\n"
printf "\n"
printf " Additionally to the OPTIONS and FLAGS, this script also accepts some environment variables listed here:\n"
printf " SPACECP_STARTCOMMAND The command used to start/fork the server into the background.\n"
printf " IMPORTANT: Must write its PID to the pidfile or the stopping will NOT work.\n"
printf " SPACECP_STARTARG Arguments passed to SPACECP_STARTCOMMAND.\n"
printf " SPACECP_STARTEND Arguments passed after all other starting commands (f.i. a closing quote).\n"
printf " SPACECP_RTKARGS Arguments passed to the Remotetoolkit (NOT Java).\n"
printf " SPACECP_JAVAARGS Arguments passed to Java.\n"
printf " SPACECP_JAVABIN Path to the Java binary.\n"
printf " SPACECP_SESSION Name of the screen/tmux session (if screen/tmux are used).\n"
printf " SPACECP_PIDFILE Path to the pidfile. Server stopping will not work if it holds a false or no PID.\n"
printf " (See the scripts source for default values.)\n"
printf " These can be set by simply declaring them before the starting the script like so:\n"
printf " > SPACECP_SESSION='SpaceCP' ./spacecp\n"
printf " or:\n"
printf " > SPACECP_SESSION='SpaceCP'\n"
printf " > ./spacecp\n"
printf "\n"
printf "DEPENDENCIES:\n"
printf "\n"
printf " openssl, curl, unzip, coreutils, sed, start-stop-daemon or screen or tmux, and sh or any sh compatible shell\n"
printf "\n"
}
ex () { # Exit or return depending on if the script is being sourced or executed
if [ -n "$SOURCED" ] && [ "$SOURCED" -eq 1 ]
then echo "return $1"
else echo "exit $1"
fi
}
__ () {
printf '%s\n' "$___";
}
ask () { # Interactivity is important!
printf '%s [Y/n] ' "$1"
[ "$ultima_yes" -eq 1 ] && printf "Y\n" && return 0
read yn
case "$yn" in [Yy]*|'') return 0;; esac
return 1
}
install_spacecp () {
## Installing SpaceCP
## Forces install (overwrites) if any argument is given.
## Will not overwrite anything if not forced.
tmp=$(TMPDIR=$(pwd) mktemp -d 'spacecptmp_XXXXXXXXXX')
[ "$force_update" -eq 2 ] && set 1
printf '%s' "[ ] Getting configuration..."
curl -sLA "SpaceCP Script $SPACECP______" -o "$tmp/spacecp_conf.zip" --create-dirs \
"$SPACECP_URL/api/getServerConfigs?key=$SPACECP_APIKEY&serverid=$SPACECP_SERVID"
[ -s "$tmp/spacecp_conf.zip" ] || (printf '\r[ERROR] \n %s\n' "Could not fetch the configuration under\
'$SPACECP_URL/api/getServerConfigs?key=$SPACECP_APIKEY&serverid=$SPACECP_SERVID'." && exit 1) || return 1
if [ -n "$1" ]
then unzip -o "$tmp/spacecp_conf.zip" >$o
else unzip -uo "$tmp/spacecp_conf.zip" >$o
fi
[ -s "$SPACECP_CONFFILE" ] || (printf '\r[ERROR] \n %s\n' "Could not extract/find '$SPACECP_CONFFILE'." && exit 1) \
|| return 1
printf '\r%s\n' "[OK] "
thisdl="$SPACECP_SERVJAR"
thisurl="$SPACECP_GDNAPIURL/jar/2/channel/4/build?sort=build.build.desc"
thisbase=$(basename "$thisdl")
printf '%s' "[ ] $thisdl..."
if [ -n "$1" ] || ([ -s "$thisdl" ] && ask "'$thisdl' already exists. Overwrite?") || ! [ -s "$thisdl" ]
then
## HARDCODED STUFF ;_;
if printf "$thisdl"|awk '{if(!match($1,"craftbukkit.jar$")){exit 2}}' >$o
then
dljson=$(curl -sLA "SpaceCP Script $SPACECP______" -H "accept:application/json" "$thisurl")
dlurl=$(printf '%s' "$dljson" | grep -om1 '"url"[ ]*:[ ]*"[^"]\+"' \
| head -n1 | sed -n 's/"url"[ ]*:[ ]*"\([^"]\+\)"/\1/p')
dlhash=$(printf '%s' "$dljson" | grep -om1 '"checksum"[ ]*:[ ]*"[^"]\+"' \
| head -n1 | sed -n 's/"checksum"[ ]*:[ ]*"\([^"]\+\)"/\1/p')
[ -z "dlhash" ] || [ -z "$dlurl" ] && printf '\r[ERROR] \n %s\n' \
"Could not find any recommended build for '$thisdl' under '$thisurl'." \
&& return 1
curl -sLA "SpaceCP Script $SPACECP______" -o "$tmp/$thisbase" --create-dirs "$dlurl"
[ -s "$tmp/$thisbase" ] || (printf '\r[ERROR] \n %s\n' "Could not fetch '$thisbase' from '$dlurl'." && exit 1) \
|| return 1
thishash=$(openssl md5 "$tmp/$thisbase" | sed 's/.* //')
if [ -z "$dlhash" ] || [ "$thishash" = "$dlhash" ]
then mkdir -p $(dirname "$thisdl") && mv "$tmp/$thisbase" "$thisdl"
else
printf '\t[ERROR] \n %s\n' "Wrong hash '$thishash', should be '$dlhash'."
return 1
fi
else
printf '\r[ERROR] \n %s\n' "Could not find '$thisdl'. Please install it first."
return 1
fi
fi
printf '\r%s\n' "[OK] "
thisdl="$SPACECP_RTKJAR"
thisurl="$SPACECP_DLAPIURL/software/remotetoolkit?channel=rec"
thisbase=$(basename "$thisdl")
printf '%s' "[ ] $thisdl..."
if [ -n "$1" ] || ([ -s "$thisdl" ] && ask "'$thisdl' already exists. Overwrite?") || ! [ -s "$thisdl" ]
then
dljson=$(curl -sLA "SpaceCP Script $SPACECP______" -H "accept:application/json" "$thisurl")
dlurl=$(printf '%s' "$dljson" | grep -om1 '"url"[ ]*:[ ]*"[^"]\+"' \
| head -n1 | sed -n 's/"url"[ ]*:[ ]*"\([^"]\+\)"/\1/p')
dlhash=$(printf '%s' "$dljson" | grep -om1 '"hash"[ ]*:[ ]*"[^"]\+"' \
| head -n1 | sed -n 's/"hash"[ ]*:[ ]*"\([^"]\+\)"/\1/')
[ -z "dlhash" ] || [ -z "$dlurl" ] && printf '\r[ERROR] \n %s\n' \
"Could not find any recommended build for '$thisdl' under '$thisurl'." \
&& return 1
curl -sLA "SpaceCP Script $SPACECP______" -o "$tmp/$thisbase" --create-dirs "$dlurl"
[ -s "$tmp/$thisbase" ] || (printf '\r[ERROR] \n %s\n' "Could not fetch '$thisbase' from '$dlurl'." && exit 1) \
|| return 1
thishash=$(openssl sha1 "$tmp/$thisbase" | sed 's/.* //')
if [ -z "$dlhash" ] || [ "$thishash" = "$dlhash" ]
then mkdir -p $(dirname "$thisdl") && mv "$tmp/$thisbase" "$thisdl"
else
printf '\t[ERROR] \n %s\n' "Wrong hash '$thishash', should be '$dlhash'."
return 1
fi
else
printf '\r[ERROR] \n %s\n' "Could not find '$thisdl'. Please install it first."
return 1
fi
printf '\r%s\n' "[OK] "
thisdl="$SPACECP_RPJAR"
thisurl="$SPACECP_DLAPIURL/software/remotetoolkitplugin?channel=rec"
thisbase=$(basename "$thisdl")
printf '%s' "[ ] $thisdl..."
if [ -n "$1" ] || ([ -s "$thisdl" ] && ask "'$thisdl' already exists. Overwrite?") || ! [ -s "$thisdl" ]
then
dljson=$(curl -sLA "SpaceCP Script $SPACECP______" -H "accept:application/json" "$thisurl")
dlurl=$(printf '%s' "$dljson" | grep -om1 '"url"[ ]*:[ ]*"[^"]\+"' \
| head -n1 | sed -n 's/"url"[ ]*:[ ]*"\([^"]\+\)"/\1/p')
dlhash=$(printf '%s' "$dljson" | grep -om1 '"hash"[ ]*:[ ]*"[^"]\+"' \
| head -n1 | sed -n 's/"hash"[ ]*:[ ]*"\([^"]\+\)"/\1/p')
[ -z "dlhash" ] || [ -z "$dlurl" ] && printf '\r[ERROR] \n %s\n' \
"Could not find any recommended build for '$thisdl' under '$thisurl'." \
&& return 1
curl -sLA "SpaceCP Script $SPACECP______" -o "$tmp/$thisbase" --create-dirs "$dlurl"
[ -s "$tmp/$thisbase" ] || (printf '\r[ERROR] \n %s\n' "Could not fetch '$thisbase' from '$dlurl'." && exit 1) \
|| return 1
thishash=$(openssl sha1 "$tmp/$thisbase" | sed 's/.* //')
if [ -z "$dlhash" ] || [ "$thishash" = "$dlhash" ]
then mkdir -p $(dirname "$thisdl") && mv "$tmp/$thisbase" "$thisdl"
else
printf '\t[ERROR] \n %s\n' "Wrong hash '$thishash', should be '$dlhash'."
return 1
fi
else
printf '\r[ERROR] \n %s\n' "Could not find '$thisdl'. Please install it first."
return 1
fi
printf '\r%s\n' "[OK] "
thisdl="$SPACECP_SMJAR"
thisurl="$SPACECP_DLAPIURL/software/spacecp_module?channel=rec"
thisbase=$(basename "$thisdl")
printf '%s' "[ ] $thisdl..."
if [ -n "$1" ] || ([ -s "$thisdl" ] && ask "'$thisdl' already exists. Overwrite?") || ! [ -s "$thisdl" ]
then
dljson=$(curl -sLA "SpaceCP Script $SPACECP______" -H "accept:application/json" "$thisurl")
dlurl=$(printf '%s' "$dljson" | grep -om1 '"url"[ ]*:[ ]*"[^"]\+"' \
| head -n1 | sed -n 's/"url"[ ]*:[ ]*"\([^"]\+\)"/\1/p')
dlhash=$(printf '%s' "$dljson" | grep -om1 '"checksum"[ ]*:[ ]*"[^"]\+"' \
| head -n1 | sed -n 's/"checksum"[ ]*:[ ]*"\([^"]\+\)"/\1/p')
[ -z "dlhash" ] || [ -z "$dlurl" ] && printf '\r[ERROR] \n %s\n' \
"Could not find any recommended build for '$thisdl' under '$thisurl'." \
&& return 1
curl -sLA "SpaceCP Script $SPACECP______" -o "$tmp/$thisbase" --create-dirs "$dlurl"
[ -s "$tmp/$thisbase" ] || (printf '\r[ERROR] \n %s\n' "Could not fetch '$thisbase' from '$dlurl'." && exit 1) \
|| return 1
thishash=$(openssl sha1 "$tmp/$thisbase" | sed 's/.* //')
if [ -z "$dlhash" ] || [ "$thishash" = "$dlhash" ]
then mkdir -p $(dirname "$thisdl") && mv "$tmp/$thisbase" "$thisdl"
else
printf '\t[ERROR] \n %s\n' "Wrong hash '$thishash', should be '$dlhash'."
return 1
fi
else
printf '\r[ERROR] \n %s\n' "Could not find '$thisdl'. Please install it first."
return 1
fi
printf '\r%s\n' "[OK] "
printf '%s' "[ ] SpaceCP Libraries..."
libver=$(sed -n '/^libraries:$/,/^[^ ]\+/s/^ version: \([0-9]\+\)/\1/p' "$SPACECP_CONFFILE")
libjson=$(curl -sLA "SpaceCP Script $SPACECP______" "$SPACECP_DLAPIURL/software/spacecp_libraries?channel=rec")
newliburl=$(printf '%s' "$libjson" \
| grep -om1 '"url"[ ]*:[ ]*"[^"]*"' \
| head -n1 | sed -n 's/"url"[ ]*:[ ]*"\([^"]*\)"/\1/p')
newlibver=$(printf '%s' "$libjson" \
| grep -om1 '"createdAt"[ ]*:[ ]*[0-9]\+' \
| head -n1 | sed -n 's/"createdAt"[ ]*:[ ]*\([0-9]\+\)/\1/p')
newlibhash=$(printf '%s' "$libjson" \
| grep -om1 '"hash"[ ]*:[ ]*"[a-fA-F0-9]\+"' \
| head -n1 | sed -n 's/"hash"[ ]*:[ ]*"\([a-fA-F0-9]\+\)"/\1/p')
if [ -n "$newliburl" ] && [ -n "$newlibver" ]
then
if [ -n "$1" ] || [ -z "$libver" ] || [ "$libver" -lt "$newlibver" ]
then
curl -sLA "SpaceCP Script $SPACECP______" -o "$tmp/spacecp_libraries.zip" --create-dirs "$newliburl"
if [ -s "$tmp/spacecp_libraries.zip" ]
then
thishash=$(openssl sha1 "$tmp/spacecp_libraries.zip" | sed 's/.* //')
if [ -z "$dlhash" ] || [ "$thishash" = "$newlibhash" ]
then
if [ -n "$1" ]
then unzip -o "$tmp/spacecp_libraries.zip" >$o
else unzip -uo "$tmp/spacecp_libraries.zip" >$o
fi
if grep '^libraries:$' "$SPACECP_CONFFILE" >$o
then
if grep '^ version:' "$SPACECP_CONFFILE" >$o
then
sed -i '/^libraries:$/,/^[^ ]\+/s/^ version:.*$/ version: '"$newlibver"'/' "$SPACECP_CONFFILE" >$o
else sed -i '/^libraries:$/a\
version: '"$newlibver" "$SPACECP_CONFFILE" >$o
fi
if grep '^ hash:' "$SPACECP_CONFFILE" >$o
then
sed -i '/^libraries:$/,/^[^ ]\+/s/^ hash:.*$/ hash: '"$thishash"'/' "$SPACECP_CONFFILE" >$o
else sed -i '/^libraries:$/a\
hash: '"$thishash" "$SPACECP_CONFFILE" >$o
fi
else
printf '\n' >> "$SPACECP_CONFFILE"
printf '%s\n' "libraries:" " version: $newlibver" " hash: $thishash" >> "$SPACECP_CONFFILE"
fi
else printf '\r[ERROR] \n %s\n' "Wrong hash '$thishash', should be '$newlibhash'." && return 1
fi
else printf '\r[ERROR] \n %s\n' "Could not fetch the SpaceCP Libraries from '$newliburl'." && return 1
fi
fi
else
printf '\r[ERROR] \n %s\n' "Could not find any SpaceCP Libraries on SpaceDL under\
'$SPACECP_DLAPIURL/software/spacecp_libraries?channel=rec'."
return 1
fi
printf '\r%s\n' "[OK] "
printf '%s' "[ ] Removing temporary files..."
if rm -r "$tmp"
then printf '\r%s\n' "[OK] "
fi
return 0
}
update_spacecp () {
## Updating and stuff
## What, did you expect a summary for every section?!
## Well tough luck, ain't gonna get one now anymore with that attitude!
## (jk behaves mostly just like install_spacecp)
tmp=$(TMPDIR=$(pwd) mktemp -d 'spacecptmp_XXXXXXXXXX')
[ "$force_update" -eq 1 ] && set 1
thisdl="$SPACECP_RTKJAR"
thischannel=$(sed -n '/^wrapper:$/,/^[^ ]\+/s/^ channel: \([a-zA-Z]\+\)/\1/p' "$SPACECP_CONFFILE")
case "$thischannel" in
development) thisurl="$SPACECP_DLAPIURL/software/remotetoolkit?channel=dev";;
recommended) thisurl="$SPACECP_DLAPIURL/software/remotetoolkit?channel=rec";;
*) thisurl="$SPACECP_DLAPIURL/software/remotetoolkit";;
esac
thisbase=$(basename "$thisdl")
thisstat=$(stat -c '%Y' "$thisdl" 2>$o)
printf '%s' "[ ] $thisdl..."
dljson=$(curl -sLA "SpaceCP Script $SPACECP______" -H "accept:application/json" "$thisurl")
dlurl=$(printf '%s' "$dljson" | grep -om1 '"url"[ ]*:[ ]*"[^"]\+"' \
| head -n1 | sed -n 's/"url"[ ]*:[ ]*"\([^"]\+\)"/\1/p')
dlhash=$(printf '%s' "$dljson" | grep -om1 '"hash"[ ]*:[ ]*"[^"]\+"' \
| head -n1 | sed -n 's/"hash"[ ]*:[ ]*"\([^"]\+\)"/\1/p')
dlstat=$(printf '%s' "$dljson" | grep -om1 '"createdAt"[ ]*:[ ]*[0-9]\+' \
| head -n1 | sed -n 's/"createdAt"[ ]*:[ ]*\([0-9]\+\)/\1/p')
if [ -n "dlhash" ] && [ -n "$dlurl" ] && [ -n "$dlstat" ]
then
if [ -n "$1" ] || ! [ -s "$thisdl" ] || [ "$dlstat" -gt "$thisstat" ]
then
curl -sLA "SpaceCP Script $SPACECP______" -o "$tmp/$thisbase" --create-dirs "$dlurl"
if [ -s "$tmp/$thisbase" ]
then
thishash=$(openssl sha1 "$tmp/$thisbase" | sed 's/.* //')
if [ -z "$dlhash" ] || [ "$thishash" = "$dlhash" ]
then
if mkdir -p $(dirname "$thisdl") && mv "$tmp/$thisbase" "$thisdl"
then printf '\r%s\n' "[OK] $thisdl updated!"
fi
else printf '\t[ERROR] \n %s\n' "Wrong hash '$thishash', should be '$dlhash'."
fi
else printf '\r[ERROR] \n %s\n' "Could not fetch '$thisbase' from '$dlurl'."
fi
else printf "\r[OK] $thisdl already up to date.\n"
fi
else printf '\r[ERROR] \n %s\n' "Could not find any $thischannel build for '$thisdl' under '$thisurl'."
fi
thisdl="$SPACECP_RPJAR"
thischannel=$(sed -n '/^wrapper:$/,/^[^ ]\+/s/^ channel: \([a-zA-Z]\+\)/\1/p' "$SPACECP_CONFFILE")
case "$thischannel" in
development) thisurl="$SPACECP_DLAPIURL/software/remotetoolkitplugin?channel=dev";;
recommended) thisurl="$SPACECP_DLAPIURL/software/remotetoolkitplugin?channel=rec";;
*) thisurl="$SPACECP_DLAPIURL/software/remotetoolkitplugin";;
esac
thisbase=$(basename "$thisdl")
thisstat=$(stat -c '%Y' "$thisdl" 2>$o)
printf '%s' "[ ] $thisdl..."
dljson=$(curl -sLA "SpaceCP Script $SPACECP______" -H "accept:application/json" "$thisurl")
dlurl=$(printf '%s' "$dljson" | grep -om1 '"url"[ ]*:[ ]*"[^"]\+"' \
| head -n1 | sed -n 's/"url"[ ]*:[ ]*"\([^"]\+\)"/\1/p')
dlhash=$(printf '%s' "$dljson" | grep -om1 '"hash"[ ]*:[ ]*"[^"]\+"' \
| head -n1 | sed -n 's/"hash"[ ]*:[ ]*"\([^"]\+\)"/\1/p')
dlstat=$(printf '%s' "$dljson" | grep -om1 '"createdAt"[ ]*:[ ]*[0-9]\+' \
| head -n1 | sed -n 's/"createdAt"[ ]*:[ ]*\([0-9]\+\)/\1/p')
if [ -n "dlhash" ] && [ -n "$dlurl" ] && [ -n "$dlstat" ]
then
if [ -n "$1" ] || ! [ -s "$thisdl" ] || [ "$dlstat" -gt "$thisstat" ]
then
curl -sLA "SpaceCP Script $SPACECP______" -o "$tmp/$thisbase" --create-dirs "$dlurl"
if [ -s "$tmp/$thisbase" ]
then
thishash=$(openssl sha1 "$tmp/$thisbase" | sed 's/.* //')
if [ -z "$dlhash" ] || [ "$thishash" = "$dlhash" ]
then
if mkdir -p $(dirname "$thisdl") && mv "$tmp/$thisbase" "$thisdl"
then printf '\r%s\n' "[OK] $thisdl updated!"
fi
else printf '\t[ERROR] \n %s\n' "Wrong hash '$thishash', should be '$dlhash'."
fi
else printf '\r[ERROR] \n %s\n' "Could not fetch '$thisbase' from '$dlurl'."
fi
else printf "\r[OK] $thisdl already up to date.\n"
fi
else printf '\r[ERROR] \n %s\n' "Could not find any $thischannel build for '$thisdl' under '$thisurl'."
fi
thisdl="$SPACECP_SMJAR"
thischannel=$(sed -n '/^spacecp:$/,/^[^ ]\+/s/^ channel: \([a-zA-Z]\+\)/\1/p' "$SPACECP_CONFFILE")
case "$thischannel" in
development) thisurl="$SPACECP_DLAPIURL/software/spacecp_module?channel=dev";;
release|recommended) thisurl="$SPACECP_DLAPIURL/software/spacecp_module?channel=rec";;
*) thisurl="$SPACECP_DLAPIURL/software/spacecp_module";;
esac
thisbase=$(basename "$thisdl")
thisstat=$(stat -c '%Y' "$thisdl" 2>$o)
printf '%s' "[ ] $thisdl..."
dljson=$(curl -sLA "SpaceCP Script $SPACECP______" -H "accept:application/json" "$thisurl")
dlurl=$(printf '%s' "$dljson" | grep -om1 '"url"[ ]*:[ ]*"[^"]\+"' \
| head -n1 | sed -n 's/"url"[ ]*:[ ]*"\([^"]\+\)"/\1/p')
dlhash=$(printf '%s' "$dljson" | grep -om1 '"hash"[ ]*:[ ]*"[^"]\+"' \
| head -n1 | sed -n 's/"hash"[ ]*:[ ]*"\([^"]\+\)"/\1/p')
dlstat=$(printf '%s' "$dljson" | grep -om1 '"createdAt"[ ]*:[ ]*[0-9]\+' \
| head -n1 | sed -n 's/"createdAt"[ ]*:[ ]*\([0-9]\+\)/\1/p')
[ -z "dlhash" ] || [ -z "$dlurl" ] || [ -z "$dlstat" ] \
&& printf '\r[ERROR] \n %s\n' "Could not find any $thischannel build for '$thisdl' under '$thisurl'."
if [ -n "$1" ] || ! [ -s "$thisdl" ] || [ "$dlstat" -gt "$thisstat" ]
then
curl -sLA "SpaceCP Script $SPACECP______" -o "$tmp/$thisbase" --create-dirs "$dlurl"
if [ -s "$tmp/$thisbase" ]
then
thishash=$(openssl sha1 "$tmp/$thisbase" | sed 's/.* //')
if [ -z "$dlhash" ] || [ "$thishash" = "$dlhash" ]
then
if mkdir -p $(dirname "$thisdl") && mv "$tmp/$thisbase" "$thisdl"
then printf '\r%s\n' "[OK] $thisdl updated!"
fi
else printf '\t[ERROR] \n %s\n' "Wrong hash '$thishash', should be '$dlhash'."
fi
else printf '\r[ERROR] \n %s\n' "Could not fetch '$thisbase' from '$dlurl'."
fi
else printf "\r[OK] $thisdl already up to date.\n"
fi
printf '%s' "[ ] SpaceCP Libraries..."
libver=$(sed -n '/^libraries:$/,/^[^ ]\+/s/^ version: \([0-9]\+\)/\1/p' "$SPACECP_CONFFILE")
libjson=$(curl -sLA "SpaceCP Script $SPACECP______" "$SPACECP_DLAPIURL/software/spacecp_libraries?channel=rec")
newliburl=$(printf '%s' "$libjson" \
| grep -om1 '"url"[ ]*:[ ]*"[^"]*"' \
| head -n1 | sed -n 's/"url"[ ]*:[ ]*"\([^"]*\)"/\1/p')
newlibver=$(printf '%s' "$libjson" \
| grep -om1 '"createdAt"[ ]*:[ ]*[0-9]\+' \
| head -n1 | sed -n 's/"createdAt"[ ]*:[ ]*\([0-9]\+\)/\1/p')
newlibhash=$(printf '%s' "$libjson" \
| grep -om1 '"hash"[ ]*:[ ]*"[a-fA-F0-9]\+"' \
| head -n1 | sed -n 's/"hash"[ ]*:[ ]*"\([a-fA-F0-9]\+\)"/\1/p')
if [ -n "$newliburl" ] && [ -n "$newlibver" ]
then
if [ -n "$1" ] || [ -z "$libver" ] || [ "$libver" -lt "$newlibver" ]
then
curl -sLA "SpaceCP Script $SPACECP______" -o "$tmp/spacecp_libraries.zip" --create-dirs "$newliburl"
if [ -s "$tmp/spacecp_libraries.zip" ]
then
thishash=$(openssl sha1 "$tmp/spacecp_libraries.zip" | sed 's/.* //')
if [ -z "$dlhash" ] || [ "$thishash" = "$newlibhash" ]
then
if [ -n "$1" ]
then
if unzip -o "$tmp/spacecp_libraries.zip" >$o
then printf '\r[OK] SpaceCP Libraries updated!\n'
fi
else
if unzip -uo "$tmp/spacecp_libraries.zip" >$o
then printf '\r[OK] SpaceCP Libraries updated!\n'
fi
fi
if grep '^libraries:$' "$SPACECP_CONFFILE" >$o
then
if grep '^ version:' "$SPACECP_CONFFILE" >$o
then
sed -i '/^libraries:$/,/^[^ ]\+/s/^ version:.*$/ version: '"$newlibver"'/' "$SPACECP_CONFFILE" >$o
else sed -i '/^libraries:$/a\
version: '"$newlibver" "$SPACECP_CONFFILE" >$o
fi
if grep '^ hash:' "$SPACECP_CONFFILE" >$o
then
sed -i '/^libraries:$/,/^[^ ]\+/s/^ hash:.*$/ hash: '"$thishash"'/' "$SPACECP_CONFFILE" >$o
else sed -i '/^libraries:$/a\
hash: '"$thishash" "$SPACECP_CONFFILE" >$o
fi
else
printf '\n' >> "$SPACECP_CONFFILE"
printf '%s\n' "libraries:" " version: $newlibver" " hash: $thishash" >> "$SPACECP_CONFFILE"
fi
else printf '\r[ERROR] \n %s\n' "Wrong hash '$thishash', should be '$newlibhash'."
fi
else printf '\r[ERROR] \n %s\n' "Could not fetch the SpaceCP Libraries from '$newliburl'."
fi
else printf '\r[OK] SpaceCP Libraries already up to date.\n'
fi
else
printf '\r[ERROR] \n %s\n' "Could not find any SpaceCP Libraries on SpaceDL under\
'$SPACECP_DLAPIURL/software/spacecp_libraries?channel=rec'."
return 1
fi
printf '%s' "[ ] Removing temporary files..."
if rm -r "$tmp"
then printf '\r%s\n' "[OK] "
fi
}
start_spacecp () {
## Actually starting RTK now and checking the exit status.
## It is EXTREMELY important for the starting command to automatically fork itself into the background,
## or else we can't correctly check if it started to begin with, and more importantly,
## cannot send a POST request to the SpaceCP servers to notify them that the server started!
printf '%s' "[ ] Starting SpaceCP..."
if [ -e "$SPACECP_PIDFILE" ]
then
pid=$(cat "$SPACECP_PIDFILE")
if ps -p $pid 2>$o >$o
then
printf '\r[ERROR]\r %s\n' "Already running with PID '$pid' ('$SPACECP_PIDFILE' exists)."
return 1
else
printf '\r[WARN ]\r %s\n' "PID file found '$SPACECP_PIDFILE' exists but '$pid' isn't running. Did it crash?"
rm "$SPACECP_PIDFILE"
fi
fi
SPACECP_FULLCOMMAND="$SPACECP_STARTCOMMAND $SPACECP_STARTARG\
$SPACECP_JAVABIN $SPACECP_JAVAARGS\
$SPACECP_RTKJAR $SPACECP_RTKARGS $SPACECP_STARTEND"
if eval "$SPACECP_FULLCOMMAND"
then
pid=$!
printf "$pid" > "$SPACECP_PIDFILE"
printf '\r%s\n' "[OK] Starting SpaceCP... [$SPACECP_STARTCOMMAND]"
curl -sLA "SpaceCP Script $SPACECP______" -X POST \
"$SPACECP_URL/api/serverStarted?key=$SPACECP_APIKEY&serverid=$SPACECP_SERVID" -o $o
else printf '\r[ERROR]\n %s\n' "Could not start '$SPACECP_FULLCOMMAND'."
fi
}
stop_spacecp () {
## Stopping RTK again~
## If any argument is passed it will stop forcefully, if not it will attempt to stop it peacefully.
## It will always wait for 10 seconds (can be changed by setting the variable $i) before concluding wether
## the server still runs or not.
printf '%s' "[ ] Getting PID..."
pid=""
case "$SPACECP_STARTCOMMAND" in
start-stop-daemon) pid=$(cat "$SPACECP_PIDFILE");;
tmux|screen) pid=$(ps --ppid $(cat "$SPACECP_PIDFILE") --no-headers -o pid 2>$o | head -n1);;
esac
if [ -z "$pid" ] || printf "$pid"|awk '{if(match($1,"^[0-9]+$")){exit 2}}'
then
printf '\r[ERROR]\n %s\n' "Could not get the PID. Does '$SPACECP_PIDFILE' exist and hold the PID?"
return 1
fi
printf '\r%s\n' "[OK] Getting PID... [$pid]"
if [ -z "$1" ]
then
printf '%s' "[ ] Trying to stop peacefully"
kill -15 $pid 2>$o >$o
else
printf '%s' "[ ] Trying to stop forcefully"
kill -9 $pid 2>$o >$o
fi
i=10
while ps -p $pid 2>$o >$o && [ $i -ge 0 ]
do
printf '.'
i=$(($i - 1))
sleep 1
done
if ps -p $pid 2>$o >$o
then
printf '\r[ERROR]\n %s\n' "Could not stop the server!"
return 1
else
rm "$SPACECP_PIDFILE"
printf '\r[OK] \n'
fi
}
## Check if all dependencies are met!
for dep in curl openssl awk sed unzip ps printf
do
if ! command -v $dep >$o
then
echo "Dependency not met: $dep"
echo "Please install the corresponding package first."
fi
done
## START Arguments (OPTIONS/FLAGS) handling
[ "$1" = "--" ] && shift
while [ -n "$1" ]
do
case "$1" in
--*=*)
par=$(printf "$1"|sed 's/^\([^=]\)\+=.*$/\1/')
arg=$(printf "$1"|sed 's/^[^=]\+=\(.*\)$/\1/')
ds='';;
--*)
par="$1"
arg="$2"
ds=' ';;
-*)
par="$1"
arg="$2"
ds=' ';;
help|start|stop|force-stop|update|install)
par="$1"
arg="$2"
ds='';;
*)
printf '%s\n' "Invalid argument '$1'"
$(ex 1)
ds='';;
esac
case "$par" in
help|--help|-h|-\?) show_help; $(ex 0);;
--always-yes|-y) ultima_yes=1;;
start) :;;
update|--force-update|-U) force_update=1;;
install|--force-install|-I) force_update=2;;
stop|--stop|-s) stop_spacecp; $(ex);;
force-stop|--force-stop|-S) stop_spacecp 'fak_u'; $(ex);;
--url|-u) SPACECP_URL="$arg"; [ -n "$ds" ] && shift;;
--dl-url|-du) SPACECP_DLAPIURL="$arg"; [ -n "$ds" ] && shift;;
--gdn-url|-gu) SPACECP_GDNAPIURL="$arg"; [ -n "$ds" ] && shift;;
--config|-c)
if [ $force_update -ne 2 ]
then if [ -s "$arg" ]
then SPACECP_CONFFILE="$arg"
else printf '%s\n' "'$arg' is not a valid configuration file."; $(ex 1)
fi
fi
[ -n "$ds" ] && shift;;
--api-key|-k) if printf "$arg"|awk '{if(!match($1,"^[0-9a-fA-F]+$")){exit 2}}' >$o
then SPACECP_APIKEY=$(printf '%s' "$arg" | tr '[:upper:]' '[:lower:]')
else printf '%s\n' "'$arg' is not a valid API key."; $(ex 1)
fi
[ -n "$ds" ] && shift;;
--server-id|-i) if printf "$arg"|awk '{if(!match($1,"^[0-9a-fA-F]+$")){exit 2}}' >$o
then SPACECP_SERVID=$(printf '%s' "$arg" | tr '[:upper:]' '[:lower:]')
else printf '%s\n' "'$arg' is not a valid server id."; $(ex 1)
fi
[ -n "$ds" ] && shift;;
--server-jar|-sj)
if [ $force_update -ne 2 -o -s "$arg" ]
then SPACECP_SERVJAR="$arg"
else printf '%s\n' "'$arg' is not a valid server jar/file."; $(ex 1)
fi
[ -n "$ds" ] && shift;;
--rtk-jar|-rj)
if [ $force_update -eq 2 -o -s "$arg" ]
then SPACECP_RTKJAR="$arg"
else printf '%s\n' "'$arg' is not a valid RTK jar."; $(ex 1)
fi
[ -n "$ds" ] && shift;;
--module-jar|-mj)
if [ $force_update -eq 2 -o -s "$arg" ]
then SPACECP_SMJAR="$arg"
else printf '%s\n' "'$arg' is not a valid SpaceCP module jar."; $(ex 1)
fi
[ -n "$ds" ] && shift;;
--rtk-plugin-jar|-pj)
if [ $force_update -eq 2 -o -s "$arg" ]
then SPACECP_RPJAR="$arg"
else printf '%s\n' "'$arg' is not a valid RTK plugin jar."; $(ex 1)
fi
[ -n "$ds" ] && shift;;
$_____) if [ "$arg" = "$____" ]; then __; $(ex 1); fi;;
*) printf '%s\n' "Invalid argument '$1'"; $(ex 1);;
esac
shift
done
## END Arguments (OPTIONS/FLAGS) handling
## Check for API Key and Server ID
if [ -z "$SPACECP_APIKEY" ] && [ -s "$SPACECP_CONFFILE" ]
then SPACECP_APIKEY=$(sed -n '/^spacecp:$/,/^[^ ]\+/s/^ apikey: \([a-fA-F0-9]\+\)$/\1/p' "$SPACECP_CONFFILE")
fi
if [ -z "$SPACECP_SERVID" ] && [ -s "$SPACECP_CONFFILE" ]
then SPACECP_SERVID=$(sed -n '/^spacecp:$/,/^[^ ]\+/s/^ serverid: \([a-fA-F0-9]\+\)$/\1/p' "$SPACECP_CONFFILE")
fi
if [ -z "$SPACECP_APIKEY" ]
then printf '%s\n' "No API Key, exiting."; $(ex 1)
fi
if [ -z "$SPACECP_SERVID" ]
then printf '%s\n' "No Server ID, exiting."; $(ex 1)
fi
## Check if any old temporary folders from previous installations or updates still exist
## (hints to a failed installation/update)
if /bin/ls | grep "^spacecptmp_[0-9a-zA-Z]\{10\}$" >$o
then
oldtmp=$(/bin/ls|grep -om1 '^spacecptmp_[0-9a-zA-Z]\{10\}$')
printf '%s\n' "Old temporary folder found, please delete it first ($oldtmp)"
printf '%s\n' \
"Delete all newly installed files or use -I/--force-install to force an installation if the last was unsuccesfull."
printf '%s' "Delete old temporary folder '$oldtmp' [Y/n]? "
read yn
yn=$(printf "$yn"|awk '{if(!match($1,/^y/)){print "no"}}')
[ -z "$yn" ] && rm -r "$oldtmp"
$(ex 1)
fi
## Actually start the whole isntallation/updating/starting process
if [ -s "$SPACECP_CONFFILE" ] && [ "$force_update" -ne 2 ]
then # A configuration is present and an installation is not being forced (start normally)
if ! update_spacecp
then # Couldn't successfully update
if ask "Could not update SpaceCP. Start anyway?"
then
if ! start_spacecp
then # Couldn't successfully update nor start (broken update?)
printf '%s\n' "Could not start SpaceCP."
$(ex 1)
fi
fi
else # Updated successfully (an update is also successful if no new updates were found)
if ! start_spacecp
then # Updated successfully but could not start
printf '%s\n' "Could not start SpaceCP."
$(ex 1)
fi
fi
else # No configuration is present or an installation is being forced
if ask "No SpaceCP configuration found. Install SpaceCP?"
then # Install SpaceCP
if install_spacecp
then # Successfully installed
printf '%s\n' "SpaceCP installation succesfull. Starting SpaceCP for the first time."
if ! start_spacecp
then # Successfully installed but couldn't start (broken installation?)
printf '%s\n' "Could not start SpaceCP."
$(ex 1)
fi
else # Couldn't successfully install
printf '%s\n' "Could not install SpaceCP."
$(ex 1)
fi
fi
fi