Skip to content

Commit

Permalink
improves bedrock-entry.sh to pass shellcheck (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
bb-Ricardo authored Nov 2, 2023
1 parent 0110314 commit 4631a42
Showing 1 changed file with 29 additions and 32 deletions.
61 changes: 29 additions & 32 deletions bedrock-entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,24 @@ set -eo pipefail
: "${PREVIEW:=false}"

function isTrue() {
case "${1,,}" in
true | on | 1)
return 0
;;
*)
return 1
;;
esac
[[ "${1,,}" =~ ^(true|on|1)$ ]] && return 0
return 1
}

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

# shellcheck disable=SC2034
for i in {1..3}; do
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 '')
if [[ ${DOWNLOAD_URL} ]]; then
break 2
fi
[[ -n "${DOWNLOAD_URL}" ]] && break
sleep 1
done
if [[ -z ${DOWNLOAD_URL} ]]; then
DOWNLOAD_URL=$(curl -s https://mc-bds-helper.vercel.app/api/latest)
fi

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

if [[ ${DEBUG^^} = TRUE ]]; then
if [[ ${DEBUG^^} == TRUE ]]; then
set -x
curlArgs=(-v)
echo "DEBUG: running as $(id -a) with $(ls -ld /data)"
Expand Down Expand Up @@ -103,64 +97,63 @@ case ${VERSION^^} in
;;
esac

if [ ! -f "bedrock_server-${VERSION}" ]; then
if [[ ! -f "bedrock_server-${VERSION}" ]]; then

if [[ ! ${DOWNLOAD_URL} ]]; then
if [[ -z "${DOWNLOAD_URL}" ]]; then
binPath=bin-linux
if isTrue "${PREVIEW}"; then
binPath=bin-linux-preview
fi
isTrue "${PREVIEW}" && binPath+="-preview"
DOWNLOAD_URL="https://minecraft.azureedge.net/${binPath}/bedrock-server-${VERSION}.zip"
fi

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

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

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

bkupDir=backup-pre-${VERSION}
# fixup any previous interrupted upgrades
rm -rf "${bkupDir}"
for d in behavior_packs definitions minecraftpe resource_packs structures treatments world_templates; do
if [[ -d $d && -n "$(ls $d)" ]]; then
mkdir -p $bkupDir/$d
mkdir -p "${bkupDir}/$d"
echo "Backing up $d into $bkupDir"
if [[ "$d" == "resource_packs" ]]; then
mv $d/{chemistry,vanilla} $bkupDir/
[[ -n "$(ls $d)" ]] && cp -a $d/* $bkupDir/
mv $d/{chemistry,vanilla} "${bkupDir}/"
[[ -n "$(ls $d)" ]] && cp -a $d/* "${bkupDir}/"
else
mv $d/* $bkupDir/
mv $d/* "${bkupDir}/"
fi
fi
done

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

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

chmod +x bedrock_server
mv bedrock_server bedrock_server-${VERSION}
mv bedrock_server "bedrock_server-${VERSION}"
fi

if [ -n "$OPS" ] || [ -n "$MEMBERS" ] || [ -n "$VISITORS" ]; then
if [[ -n "$OPS" || -n "$MEMBERS" || -n "$VISITORS" ]]; then
echo "Updating permissions"
jq -n --arg ops "$OPS" --arg members "$MEMBERS" --arg visitors "$VISITORS" '[
[$ops | split(",") | map({permission: "operator", xuid:.})],
Expand All @@ -185,16 +178,20 @@ else
ALLOW_LIST=false
rm -f allowlist.json
fi
sed -i '/^white-list=.*/d' server.properties #Removes white-list= line from server.properties

# prevent issue with bind mounted server.properties which can not be moved (sed tries to move the file when '-i' is used)
_SERVER_PROPERTIES=$(sed '/^white-list=.*/d' server.properties) #Removes white-list= line from server.properties
echo "${_SERVER_PROPERTIES}" > server.properties
export ALLOW_LIST

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

export LD_LIBRARY_PATH=.

echo "Starting Bedrock server..."
if [ -f /usr/local/bin/box64 ] ; then
exec box64 ./bedrock_server-${VERSION}
if [[ -f /usr/local/bin/box64 ]] ; then
exec box64 ./"bedrock_server-${VERSION}"
else
exec ./bedrock_server-${VERSION}
exec ./"bedrock_server-${VERSION}"
fi

0 comments on commit 4631a42

Please sign in to comment.