forked from rkuzsma/docker-pocketmine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
installer.sh
executable file
·335 lines (284 loc) · 9.35 KB
/
installer.sh
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
#!/bin/bash
CHANNEL="alpha"
BRANCH="master"
NAME="PocketMine-MP"
BUILD_URL=""
update=off
forcecompile=off
alldone=no
checkRoot=on
alternateurl=off
INSTALL_DIRECTORY="./"
IGNORE_CERT="no"
while getopts "rucid:v:t:" opt; do
case $opt in
a)
alternateurl=on
;;
r)
checkRoot=off
;;
u)
update=on
;;
c)
forcecompile=on
;;
d)
INSTALL_DIRECTORY="$OPTARG"
;;
i)
IGNORE_CERT="yes"
;;
v)
CHANNEL="$OPTARG"
;;
t)
BUILD_URL="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
if [ `getconf LONG_BIT` == "32" ]; then
echo "[ERROR] PocketMine-MP is no longer supported on 32-bit systems."
exit 1
fi
#Needed to use aliases
shopt -s expand_aliases
type wget > /dev/null 2>&1
if [ $? -eq 0 ]; then
if [ "$IGNORE_CERT" == "yes" ]; then
alias download_file="wget --no-check-certificate -q -O -"
else
alias download_file="wget -q -O -"
fi
else
type curl >> /dev/null 2>&1
if [ $? -eq 0 ]; then
if [ "$IGNORE_CERT" == "yes" ]; then
alias download_file="curl --insecure --silent --show-error --location --globoff"
else
alias download_file="curl --silent --show-error --location --globoff"
fi
else
echo "error, curl or wget not found"
exit 1
fi
fi
if [ "$checkRoot" == "on" ]; then
if [ "$(id -u)" == "0" ]; then
echo "This script is running as root, this is discouraged."
echo "It is recommended to run it as a normal user as it doesn't need further permissions."
echo "If you want to run it as root, add the -r flag."
exit 1
fi
fi
if [ "$CHANNEL" == "soft" ]; then
NAME="PocketMine-Soft"
fi
ENABLE_GPG="no"
PUBLICKEY_URL="http://cdn.pocketmine.net/pocketmine.asc"
PUBLICKEY_FINGERPRINT="20D377AFC3F7535B3261AA4DCF48E7E52280B75B"
PUBLICKEY_LONGID="${PUBLICKEY_FINGERPRINT: -16}"
GPG_KEYSERVER="pgp.mit.edu"
function check_signature {
echo "[*] Checking signature of $1"
"$GPG_BIN" --keyserver "$GPG_KEYSERVER" --keyserver-options auto-key-retrieve=1 --trusted-key $PUBLICKEY_LONGID --verify "$1.sig" "$1"
if [ $? -eq 0 ]; then
echo "[+] Signature valid and checked!"
else
"$GPG_BIN" --refresh-keys > /dev/null 2>&1
echo "[!] Invalid signature! Please check for file corruption or a wrongly imported public key (signed by $PUBLICKEY_FINGERPRINT)"
exit 1
fi
}
function parse_json {
echo "$1" | grep "\"$2\"" | cut -d ':' -f2- | tr -d ' ",'
}
if [[ "$BUILD_URL" != "" && "$CHANNEL" == "custom" ]]; then
BASE_VERSION="custom"
VERSION="custom"
BUILD="unknown"
API_VERSION="unknown"
VERSION_DATE_STRING="unknown"
ENABLE_GPG="no"
VERSION_DOWNLOAD="$BUILD_URL"
MCPE_VERSION="unknown"
PHP_VERSION="unknown"
else
echo "[*] Retrieving latest build data for channel \"$CHANNEL\""
VERSION_DATA=$(download_file "https://jenkins.pmmp.io/job/PocketMine-MP/$(tr '[:lower:]' '[:upper:]' <<< ${CHANNEL:0:1})${CHANNEL:1}/api/json?pretty=true&tree=url,artifacts[fileName],number,timestamp")
if [ "$VERSION_DATA" != "" ]; then
FILENAME="unknown"
IFS=$'\n' FILENAMES=($(parse_json "$VERSION_DATA" fileName))
for (( i=0; i<${#FILENAMES[@]}; i++ ))
do
if [[ ${FILENAMES[$i]} == PocketMine*.phar ]]; then
FILENAME=${FILENAMES[$i]}
break
fi
done
if [ "$FILENAME" == "unknown" ]; then
echo "[!] Couldn't determine filename of artifact to download"
exit 1
fi
BUILD_INFO_JSON=$(download_file "https://jenkins.pmmp.io/job/PocketMine-MP/$(tr '[:lower:]' '[:upper:]' <<< ${CHANNEL:0:1})${CHANNEL:1}/artifact/build_info.json")
VERSION=$(parse_json "$BUILD_INFO_JSON" pm_version)
BUILD=$(parse_json "$BUILD_INFO_JSON" build_number)
API_VERSION=$(parse_json "$BUILD_INFO_JSON" api_version)
MCPE_VERSION=$(parse_json "$BUILD_INFO_JSON" mcpe_version)
PHP_VERSION=$(parse_json "$BUILD_INFO_JSON" php_version)
VERSION_DATE=$(($(echo "$VERSION_DATA" | grep -m 1 '"timestamp"' | cut -d ':' -f2- | tr -d ' ",') / 1000))
BASE_URL=$(parse_json "$VERSION_DATA" url)
VERSION_DOWNLOAD="${BASE_URL}artifact/${FILENAME}"
if [ "$alternateurl" == "on" ]; then
VERSION_DOWNLOAD=$(parse_json "$VERSION_DATA" alternate_download_url)
fi
if [ "$(uname -s)" == "Darwin" ]; then
BASE_VERSION=$(echo "$VERSION" | sed -E 's/([A-Za-z0-9_\.]*).*/\1/')
VERSION_DATE_STRING=$(date -r $VERSION_DATE)
else
BASE_VERSION=$(echo "$VERSION" | sed -r 's/([A-Za-z0-9_\.]*).*/\1/')
VERSION_DATE_STRING=$(date --date="@$VERSION_DATE")
fi
GPG_SIGNATURE=$(parse_json "$VERSION_DATA" signature_url)
if [ "$GPG_SIGNATURE" != "" ]; then
ENABLE_GPG="yes"
fi
if [ "$VERSION" == "" ]; then
echo "[!] Couldn't get the latest $NAME version"
exit 1
fi
GPG_BIN=""
if [ "$ENABLE_GPG" == "yes" ]; then
type gpg > /dev/null 2>&1
if [ $? -eq 0 ]; then
GPG_BIN="gpg"
else
type gpg2 > /dev/null 2>&1
if [ $? -eq 0 ]; then
GPG_BIN="gpg2"
fi
fi
if [ "$GPG_BIN" != "" ]; then
gpg --fingerprint $PUBLICKEY_FINGERPRINT > /dev/null 2>&1
if [ $? -ne 0 ]; then
download_file $PUBLICKEY_URL | gpg --trusted-key $PUBLICKEY_LONGID --import
gpg --fingerprint $PUBLICKEY_FINGERPRINT > /dev/null 2>&1
if [ $? -ne 0 ]; then
gpg --trusted-key $PUBLICKEY_LONGID --keyserver "$GPG_KEYSERVER" --recv-key $PUBLICKEY_FINGERPRINT
fi
fi
else
ENABLE_GPG="no"
fi
fi
else
echo "[!] Couldn't download version information automatically from Jenkins server."
exit 1
fi
fi
echo "[*] Found $NAME $BASE_VERSION (build $BUILD) for Minecraft: PE v$MCPE_VERSION (PHP $PHP_VERSION, API $API_VERSION)"
echo "[*] This $CHANNEL build was released on $VERSION_DATE_STRING"
if [ "$ENABLE_GPG" == "yes" ]; then
echo "[+] The build was signed, will check signature"
elif [ "$GPG_SIGNATURE" == "" ]; then
if [[ "$CHANNEL" == "beta" ]] || [[ "$CHANNEL" == "stable" ]]; then
echo "[-] This channel should have a signature, none found"
fi
fi
echo "[*] Installing/updating $NAME on directory $INSTALL_DIRECTORY"
mkdir -m 0777 "$INSTALL_DIRECTORY" 2> /dev/null
cd "$INSTALL_DIRECTORY"
echo "[1/3] Cleaning..."
rm -f "$NAME.phar"
rm -f README.md
rm -f CONTRIBUTING.md
rm -f LICENSE
rm -f start.sh
rm -f start.bat
#Old installations
rm -f PocketMine-MP.php
rm -r -f src/
echo -n "[2/3] Downloading $NAME $VERSION phar..."
set +e
download_file "$VERSION_DOWNLOAD" > "$NAME.phar"
if ! [ -s "$NAME.phar" ] || [ "$(head -n 1 $NAME.phar)" == '<!DOCTYPE html>' ]; then
rm "$NAME.phar" 2> /dev/null
echo " failed!"
echo "[!] Couldn't download $NAME automatically from $VERSION_DOWNLOAD"
exit 1
else
if [ "$CHANNEL" == "soft" ]; then
download_file "https://raw.githubusercontent.com/PocketMine/PocketMine-Soft/${BRANCH}/resources/start.sh" > start.sh
else
download_file "https://raw.githubusercontent.com/pmmp/PocketMine-MP/${BRANCH}/start.sh" > start.sh
fi
download_file "https://raw.githubusercontent.com/pmmp/PocketMine-MP/${BRANCH}/LICENSE" > LICENSE
download_file "https://raw.githubusercontent.com/pmmp/PocketMine-MP/${BRANCH}/README.md" > README.md
download_file "https://raw.githubusercontent.com/pmmp/PocketMine-MP/${BRANCH}/CONTRIBUTING.md" > CONTRIBUTING.md
download_file "https://raw.githubusercontent.com/pmmp/php-build-scripts/${BRANCH}/compile.sh" > compile.sh
fi
chmod +x compile.sh
chmod +x start.sh
echo " done!"
if [ "$ENABLE_GPG" == "yes" ]; then
download_file "$GPG_SIGNATURE" > "$NAME.phar.sig"
check_signature "$NAME.phar"
fi
if [ "$update" == "on" ]; then
echo "[3/3] Skipping PHP recompilation due to user request"
else
echo -n "[3/3] Obtaining PHP: detecting if build is available..."
while [ "$forcecompile" == "off" ]
do
rm -r -f bin/ >> /dev/null 2>&1
if [ "$(uname -s)" == "Darwin" ]; then
PLATFORM="MacOS-x86_64"
echo -n " MacOS PHP build available"
elif [ "$(uname -s)" == "Linux" ]; then
#if [[ "$(cat /etc/redhat-release 2>/dev/null)" == *CentOS* ]]; then
#echo -n " CentOS PHP build available, downloading $CENTOS_BUILD.tar.gz..."
#download_file "https://dl.bintray.com/pocketmine/PocketMine/$CENTOS_BUILD.tar.gz" | tar -zx > /dev/null 2>&1
#else
#TODO: check architecture (we might not be on an x86_64 system)
PLATFORM="Linux-x86_64"
echo -n " Linux PHP build available"
#fi
else
echo " no prebuilt PHP download available"
break
fi
echo -n "... downloading $PHP_VERSION ..."
download_file "https://jenkins.pmmp.io/job/PHP-$PHP_VERSION-Aggregate/lastSuccessfulBuild/artifact/PHP-$PHP_VERSION-$PLATFORM.tar.gz" | tar -zx > /dev/null 2>&1
chmod +x ./bin/php7/bin/*
if [ -f ./bin/composer ]; then
chmod +x ./bin/composer
fi
echo -n " checking..."
if [ "$(./bin/php7/bin/php -r 'echo 1;' 2>/dev/null)" == "1" ]; then
echo -n " updating php.ini..."
sed -i'.bak' "s/date.timezone=.*/date.timezone=$(date +%Z)/" bin/php7/bin/php.ini
EXTENSION_DIR=$(find "$(pwd)/bin" -name *debug-zts*) #make sure this only captures from `bin` in case the user renamed their old binary folder
#Modify extension_dir directive if it exists, otherwise add it
grep -q '^extension_dir' bin/php7/bin/php.ini && sed -i'bak' "s{^extension_dir=.*{extension_dir=\"$EXTENSION_DIR\"{" bin/php7/bin/php.ini || echo "extension_dir=\"$EXTENSION_DIR\"" >> bin/php7/bin/php.ini
echo " done"
alldone=yes
else
echo " downloaded PHP build doesn't work on this platform!"
fi
break
done
if [ "$alldone" == "no" ]; then
set -e
echo "[3/3] No prebuilt PHP found, compiling PHP automatically. This might take a while."
echo
exec "./compile.sh"
fi
fi
rm compile.sh
echo "[*] Everything done! Run ./start.sh to start $NAME"
exit 0