Skip to content

Commit 4631a42

Browse files
authored
improves bedrock-entry.sh to pass shellcheck (#365)
1 parent 0110314 commit 4631a42

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

bedrock-entry.sh

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,24 @@ set -eo pipefail
66
: "${PREVIEW:=false}"
77

88
function isTrue() {
9-
case "${1,,}" in
10-
true | on | 1)
11-
return 0
12-
;;
13-
*)
14-
return 1
15-
;;
16-
esac
9+
[[ "${1,,}" =~ ^(true|on|1)$ ]] && return 0
10+
return 1
1711
}
1812

1913
function lookupVersion() {
2014
platform=${1:?Missing required platform indicator}
2115

16+
# shellcheck disable=SC2034
2217
for i in {1..3}; do
2318
DOWNLOAD_URL=$(restify --user-agent=itzg/minecraft-bedrock-server --headers "accept-language:*" --attribute=data-platform="${platform}" "${downloadPage}" 2> restify.err | jq -r '.[0].href' || echo '')
24-
if [[ ${DOWNLOAD_URL} ]]; then
25-
break 2
26-
fi
19+
[[ -n "${DOWNLOAD_URL}" ]] && break
2720
sleep 1
2821
done
2922
if [[ -z ${DOWNLOAD_URL} ]]; then
3023
DOWNLOAD_URL=$(curl -s https://mc-bds-helper.vercel.app/api/latest)
3124
fi
3225

26+
# shellcheck disable=SC2012
3327
if [[ ${DOWNLOAD_URL} =~ http.*/.*-(.*)\.zip ]]; then
3428
VERSION=${BASH_REMATCH[1]}
3529
elif [[ $(ls -rv bedrock_server-* 2> /dev/null|head -1) =~ bedrock_server-(.*) ]]; then
@@ -49,7 +43,7 @@ function lookupVersion() {
4943
rm -f restify.err
5044
}
5145

52-
if [[ ${DEBUG^^} = TRUE ]]; then
46+
if [[ ${DEBUG^^} == TRUE ]]; then
5347
set -x
5448
curlArgs=(-v)
5549
echo "DEBUG: running as $(id -a) with $(ls -ld /data)"
@@ -103,64 +97,63 @@ case ${VERSION^^} in
10397
;;
10498
esac
10599

106-
if [ ! -f "bedrock_server-${VERSION}" ]; then
100+
if [[ ! -f "bedrock_server-${VERSION}" ]]; then
107101

108-
if [[ ! ${DOWNLOAD_URL} ]]; then
102+
if [[ -z "${DOWNLOAD_URL}" ]]; then
109103
binPath=bin-linux
110-
if isTrue "${PREVIEW}"; then
111-
binPath=bin-linux-preview
112-
fi
104+
isTrue "${PREVIEW}" && binPath+="-preview"
113105
DOWNLOAD_URL="https://minecraft.azureedge.net/${binPath}/bedrock-server-${VERSION}.zip"
114106
fi
115107

116108
[[ $TMP_DIR != /tmp ]] && mkdir -p "$TMP_DIR"
117109
TMP_ZIP="$TMP_DIR/$(basename "${DOWNLOAD_URL}")"
118110

119111
echo "Downloading Bedrock server version ${VERSION} ..."
120-
if ! curl "${curlArgs[@]}" -o "${TMP_ZIP}" -fsSL ${DOWNLOAD_URL}; then
112+
if ! curl "${curlArgs[@]}" -o "${TMP_ZIP}" -fsSL "${DOWNLOAD_URL}"; then
121113
echo "ERROR failed to download from ${DOWNLOAD_URL}"
122114
echo " Double check that the given VERSION is valid"
123115
exit 2
124116
fi
125117

126118
# remove only binaries and some docs, to allow for an upgrade of those
127-
rm -rf bedrock_server bedrock_server-* *.so release-notes.txt bedrock_server_how_to.html valid_known_packs.json premium_cache 2> /dev/null
119+
rm -rf -- bedrock_server bedrock_server-* *.so release-notes.txt bedrock_server_how_to.html valid_known_packs.json premium_cache 2> /dev/null
128120

129121
bkupDir=backup-pre-${VERSION}
130122
# fixup any previous interrupted upgrades
131123
rm -rf "${bkupDir}"
132124
for d in behavior_packs definitions minecraftpe resource_packs structures treatments world_templates; do
133125
if [[ -d $d && -n "$(ls $d)" ]]; then
134-
mkdir -p $bkupDir/$d
126+
mkdir -p "${bkupDir}/$d"
135127
echo "Backing up $d into $bkupDir"
136128
if [[ "$d" == "resource_packs" ]]; then
137-
mv $d/{chemistry,vanilla} $bkupDir/
138-
[[ -n "$(ls $d)" ]] && cp -a $d/* $bkupDir/
129+
mv $d/{chemistry,vanilla} "${bkupDir}/"
130+
[[ -n "$(ls $d)" ]] && cp -a $d/* "${bkupDir}/"
139131
else
140-
mv $d/* $bkupDir/
132+
mv $d/* "${bkupDir}/"
141133
fi
142134
fi
143135
done
144136

145137
# remove old package backups, but keep PACKAGE_BACKUP_KEEP
146138
if (( ${PACKAGE_BACKUP_KEEP:=2} >= 0 )); then
147139
shopt -s nullglob
140+
# shellcheck disable=SC2012
148141
for d in $( ls -td1 backup-pre-* | tail +$(( PACKAGE_BACKUP_KEEP + 1 )) ); do
149-
echo "Pruning $d"
150-
rm -rf $d
142+
echo "Pruning backup directory: $d"
143+
rm -rf "$d"
151144
done
152145
fi
153146

154147
# Do not overwrite existing files, which means the cleanup above needs to account for things
155148
# that MUST be replaced on upgrade
156-
unzip -q -n ${TMP_ZIP}
149+
unzip -q -n "${TMP_ZIP}"
157150
[[ $TMP_DIR != /tmp ]] && rm -rf "$TMP_DIR"
158151

159152
chmod +x bedrock_server
160-
mv bedrock_server bedrock_server-${VERSION}
153+
mv bedrock_server "bedrock_server-${VERSION}"
161154
fi
162155

163-
if [ -n "$OPS" ] || [ -n "$MEMBERS" ] || [ -n "$VISITORS" ]; then
156+
if [[ -n "$OPS" || -n "$MEMBERS" || -n "$VISITORS" ]]; then
164157
echo "Updating permissions"
165158
jq -n --arg ops "$OPS" --arg members "$MEMBERS" --arg visitors "$VISITORS" '[
166159
[$ops | split(",") | map({permission: "operator", xuid:.})],
@@ -185,16 +178,20 @@ else
185178
ALLOW_LIST=false
186179
rm -f allowlist.json
187180
fi
188-
sed -i '/^white-list=.*/d' server.properties #Removes white-list= line from server.properties
181+
182+
# prevent issue with bind mounted server.properties which can not be moved (sed tries to move the file when '-i' is used)
183+
_SERVER_PROPERTIES=$(sed '/^white-list=.*/d' server.properties) #Removes white-list= line from server.properties
184+
echo "${_SERVER_PROPERTIES}" > server.properties
189185
export ALLOW_LIST
190186

187+
# update server.properties with environment settings
191188
set-property --file server.properties --bulk /etc/bds-property-definitions.json
192189

193190
export LD_LIBRARY_PATH=.
194191

195192
echo "Starting Bedrock server..."
196-
if [ -f /usr/local/bin/box64 ] ; then
197-
exec box64 ./bedrock_server-${VERSION}
193+
if [[ -f /usr/local/bin/box64 ]] ; then
194+
exec box64 ./"bedrock_server-${VERSION}"
198195
else
199-
exec ./bedrock_server-${VERSION}
196+
exec ./"bedrock_server-${VERSION}"
200197
fi

0 commit comments

Comments
 (0)