· 5 years ago · Jun 09, 2020, 02:42 PM
1#!/bin/bash
2
3CHANNEL="stable"
4BRANCH="stable"
5NAME="PocketMine-MP"
6BUILD_URL=""
7
8update=off
9forcecompile=off
10alldone=no
11checkRoot=on
12alternateurl=off
13
14INSTALL_DIRECTORY="./"
15
16IGNORE_CERT="no"
17
18while getopts "rucid:v:t:" opt; do
19 case $opt in
20 a)
21 alternateurl=on
22 ;;
23 r)
24 checkRoot=off
25 ;;
26 u)
27 update=on
28 ;;
29 c)
30 forcecompile=on
31 ;;
32 d)
33 INSTALL_DIRECTORY="$OPTARG"
34 ;;
35 i)
36 IGNORE_CERT="yes"
37 ;;
38 v)
39 CHANNEL="$OPTARG"
40 ;;
41 t)
42 BUILD_URL="$OPTARG"
43 ;;
44 \?)
45 echo "Invalid option: -$OPTARG" >&2
46 exit 1
47 ;;
48 esac
49done
50
51
52if [ `getconf LONG_BIT` == "32" ]; then
53 echo "[ERROR] PocketMine-MP is no longer supported on 32-bit systems."
54 exit 1
55fi
56
57#Needed to use aliases
58shopt -s expand_aliases
59type wget > /dev/null 2>&1
60if [ $? -eq 0 ]; then
61 if [ "$IGNORE_CERT" == "yes" ]; then
62 alias download_file="wget --no-check-certificate -q -O -"
63 else
64 alias download_file="wget -q -O -"
65 fi
66else
67 type curl >> /dev/null 2>&1
68 if [ $? -eq 0 ]; then
69 if [ "$IGNORE_CERT" == "yes" ]; then
70 alias download_file="curl --insecure --silent --show-error --location --globoff"
71 else
72 alias download_file="curl --silent --show-error --location --globoff"
73 fi
74 else
75 echo "error, curl or wget not found"
76 exit 1
77 fi
78fi
79
80if [ "$checkRoot" == "on" ]; then
81 if [ "$(id -u)" == "0" ]; then
82 echo "This script is running as root, this is discouraged."
83 echo "It is recommended to run it as a normal user as it doesn't need further permissions."
84 echo "If you want to run it as root, add the -r flag."
85 exit 1
86 fi
87fi
88
89if [ "$CHANNEL" == "soft" ]; then
90 NAME="PocketMine-Soft"
91fi
92
93ENABLE_GPG="no"
94PUBLICKEY_URL="http://cdn.pocketmine.net/pocketmine.asc"
95PUBLICKEY_FINGERPRINT="20D377AFC3F7535B3261AA4DCF48E7E52280B75B"
96PUBLICKEY_LONGID="${PUBLICKEY_FINGERPRINT: -16}"
97GPG_KEYSERVER="pgp.mit.edu"
98
99function check_signature {
100 echo "[*] Checking signature of $1"
101 "$GPG_BIN" --keyserver "$GPG_KEYSERVER" --keyserver-options auto-key-retrieve=1 --trusted-key $PUBLICKEY_LONGID --verify "$1.sig" "$1"
102 if [ $? -eq 0 ]; then
103 echo "[+] Signature valid and checked!"
104 else
105 "$GPG_BIN" --refresh-keys > /dev/null 2>&1
106 echo "[!] Invalid signature! Please check for file corruption or a wrongly imported public key (signed by $PUBLICKEY_FINGERPRINT)"
107 exit 1
108 fi
109}
110
111function parse_json {
112 echo "$1" | grep "\"$2\"" | cut -d ':' -f2- | tr -d ' ",'
113}
114
115if [[ "$BUILD_URL" != "" && "$CHANNEL" == "custom" ]]; then
116 BASE_VERSION="custom"
117 BUILD="unknown"
118 VERSION_DATE_STRING="unknown"
119 ENABLE_GPG="no"
120 VERSION_DOWNLOAD="$BUILD_URL"
121 MCPE_VERSION="unknown"
122 PHP_VERSION="unknown"
123else
124 echo "[*] Retrieving latest build data for channel \"$CHANNEL\""
125
126 VERSION_DATA=$(download_file "https://update.pmmp.io/api?channel=$(tr '[:lower:]' '[:upper:]' <<< ${CHANNEL:0:1})${CHANNEL:1}")
127
128 if [ "$VERSION_DATA" != "" ]; then
129 error=$(parse_json "$VERSION_DATA" error)
130 if [ "$error" != "" ]; then
131 echo "[!] Failed to get download information: $error"
132 exit 1
133 fi
134 FILENAME=$(parse_json "$VERSION_DATA" phar_name)
135 BASE_VERSION=$(parse_json "$VERSION_DATA" base_version)
136 BUILD=$(parse_json "$VERSION_DATA" build_number)
137 MCPE_VERSION=$(parse_json "$VERSION_DATA" mcpe_version)
138 PHP_VERSION=$(parse_json "$VERSION_DATA" php_version)
139 VERSION_DATE=$(parse_json "$VERSION_DATA" date)
140 BASE_URL=$(parse_json "$VERSION_DATA" url)
141 VERSION_DOWNLOAD=$(parse_json "$VERSION_DATA" download_url)
142
143 if [ "$(uname -s)" == "Darwin" ]; then
144 VERSION_DATE_STRING=$(date -r $VERSION_DATE)
145 else
146 VERSION_DATE_STRING=$(date --date="@$VERSION_DATE")
147 fi
148
149 GPG_SIGNATURE=$(parse_json "$VERSION_DATA" signature_url)
150
151 if [ "$GPG_SIGNATURE" != "" ]; then
152 ENABLE_GPG="yes"
153 fi
154
155 if [ "$BASE_VERSION" == "" ]; then
156 echo "[!] Couldn't get the latest $NAME version"
157 exit 1
158 fi
159
160 GPG_BIN=""
161
162 if [ "$ENABLE_GPG" == "yes" ]; then
163 type gpg > /dev/null 2>&1
164 if [ $? -eq 0 ]; then
165 GPG_BIN="gpg"
166 else
167 type gpg2 > /dev/null 2>&1
168 if [ $? -eq 0 ]; then
169 GPG_BIN="gpg2"
170 fi
171 fi
172 if [ "$GPG_BIN" != "" ]; then
173 gpg --fingerprint $PUBLICKEY_FINGERPRINT > /dev/null 2>&1
174 if [ $? -ne 0 ]; then
175 download_file $PUBLICKEY_URL | gpg --trusted-key $PUBLICKEY_LONGID --import
176 gpg --fingerprint $PUBLICKEY_FINGERPRINT > /dev/null 2>&1
177 if [ $? -ne 0 ]; then
178 gpg --trusted-key $PUBLICKEY_LONGID --keyserver "$GPG_KEYSERVER" --recv-key $PUBLICKEY_FINGERPRINT
179 fi
180 fi
181 else
182 ENABLE_GPG="no"
183 fi
184 fi
185 else
186 echo "[!] Failed to download version information: Empty response from API"
187 exit 1
188 fi
189fi
190
191echo "[*] Found $NAME $BASE_VERSION (build $BUILD) for Minecraft: PE v$MCPE_VERSION (PHP $PHP_VERSION)"
192echo "[*] This $CHANNEL build was released on $VERSION_DATE_STRING"
193
194if [ "$ENABLE_GPG" == "yes" ]; then
195 echo "[+] The build was signed, will check signature"
196elif [ "$GPG_SIGNATURE" == "" ]; then
197 if [[ "$CHANNEL" == "beta" ]] || [[ "$CHANNEL" == "stable" ]]; then
198 echo "[-] This channel should have a signature, none found"
199 fi
200fi
201
202echo "[*] Installing/updating $NAME on directory $INSTALL_DIRECTORY"
203mkdir -m 0777 "$INSTALL_DIRECTORY" 2> /dev/null
204cd "$INSTALL_DIRECTORY"
205echo "[1/3] Cleaning..."
206rm -f "$NAME.phar"
207rm -f README.md
208rm -f CONTRIBUTING.md
209rm -f LICENSE
210rm -f start.sh
211rm -f start.bat
212
213#Old installations
214rm -f PocketMine-MP.php
215rm -r -f src/
216
217echo -n "[2/3] Downloading $NAME phar..."
218set +e
219download_file "$VERSION_DOWNLOAD" > "$NAME.phar"
220if ! [ -s "$NAME.phar" ] || [ "$(head -n 1 $NAME.phar)" == '<!DOCTYPE html>' ]; then
221 rm "$NAME.phar" 2> /dev/null
222 echo " failed!"
223 echo "[!] Couldn't download $NAME automatically from $VERSION_DOWNLOAD"
224 exit 1
225else
226 if [ "$CHANNEL" == "soft" ]; then
227 download_file "https://raw.githubusercontent.com/PocketMine/PocketMine-Soft/${BRANCH}/resources/start.sh" > start.sh
228 else
229 download_file "https://raw.githubusercontent.com/pmmp/PocketMine-MP/${BRANCH}/start.sh" > start.sh
230 fi
231 download_file "https://raw.githubusercontent.com/pmmp/PocketMine-MP/${BRANCH}/LICENSE" > LICENSE
232 download_file "https://raw.githubusercontent.com/pmmp/PocketMine-MP/${BRANCH}/README.md" > README.md
233 download_file "https://raw.githubusercontent.com/pmmp/PocketMine-MP/${BRANCH}/CONTRIBUTING.md" > CONTRIBUTING.md
234 download_file "https://raw.githubusercontent.com/pmmp/php-build-scripts/${BRANCH}/compile.sh" > compile.sh
235fi
236
237chmod +x compile.sh
238chmod +x start.sh
239
240echo " done!"
241
242if [ "$ENABLE_GPG" == "yes" ]; then
243 download_file "$GPG_SIGNATURE" > "$NAME.phar.sig"
244 check_signature "$NAME.phar"
245fi
246
247if [ "$update" == "on" ]; then
248 echo "[3/3] Skipping PHP recompilation due to user request"
249else
250 echo -n "[3/3] Obtaining PHP: detecting if build is available..."
251 while [ "$forcecompile" == "off" ]
252 do
253 rm -r -f bin/ >> /dev/null 2>&1
254
255 if [ "$(uname -s)" == "Darwin" ]; then
256 PLATFORM="MacOS-x86_64"
257 echo -n " MacOS PHP build available"
258
259 elif [ "$(uname -s)" == "Linux" ]; then
260 #if [[ "$(cat /etc/redhat-release 2>/dev/null)" == *CentOS* ]]; then
261 #echo -n " CentOS PHP build available, downloading $CENTOS_BUILD.tar.gz..."
262 #download_file "https://dl.bintray.com/pocketmine/PocketMine/$CENTOS_BUILD.tar.gz" | tar -zx > /dev/null 2>&1
263 #else
264
265 #TODO: check architecture (we might not be on an x86_64 system)
266
267 PLATFORM="Linux-x86_64"
268 echo -n " Linux PHP build available"
269
270 #fi
271 else
272 echo " no prebuilt PHP download available"
273 break
274 fi
275
276 echo -n "... downloading $PHP_VERSION ..."
277 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
278
279 chmod +x ./bin/php7/bin/*
280 if [ -f ./bin/composer ]; then
281 chmod +x ./bin/composer
282 fi
283
284 echo -n " updating php.ini..."
285
286 sed -i'.bak' "s/date.timezone=.*/date.timezone=$(date +%Z)/" bin/php7/bin/php.ini
287
288 EXTENSION_DIR=$(find "$(pwd)/bin" -name *debug-zts*) #make sure this only captures from `bin` in case the user renamed their old binary folder
289 #Modify extension_dir directive if it exists, otherwise add it
290 LF=$'\n'
291 grep -q '^extension_dir' bin/php7/bin/php.ini && sed -i'bak' "s{^extension_dir=.*{extension_dir=\"$EXTENSION_DIR\"{" bin/php7/bin/php.ini || sed -i'bak' "1s{^{extension_dir=\"$EXTENSION_DIR\"\\$LF{" bin/php7/bin/php.ini
292
293 echo -n " checking..."
294
295 if [ "$(./bin/php7/bin/php -r 'echo 1;' 2>/dev/null)" == "1" ]; then
296 echo " done"
297 alldone=yes
298 else
299 echo " downloaded PHP build doesn't work on this platform!"
300 fi
301
302 break
303 done
304 if [ "$alldone" == "no" ]; then
305 set -e
306 echo "[3/3] No prebuilt PHP found, compiling PHP automatically. This might take a while."
307 echo
308 exec "./compile.sh"
309 fi
310fi
311
312rm compile.sh
313
314echo "[*] Everything done! Run ./start.sh to start $NAME"
315exit 0