MCServerDocker/entrypoint.sh
2024-01-26 15:38:08 +01:00

95 lines
3.3 KiB
Bash
Executable file

#!/usr/sbin/env bash
# Disables warning about echo not expanding escape sequences
# shellcheck disable=SC2028
function print_banner() {
echo " ____ ____ ____ __"
echo " /'\\_/\`\\/\\ _\`\\ /\\ _\`\\ /\\ _\`\\ /\\ \\"
echo "/\\ \\ \\ \\/\\_\\\\ \\,\\L\\_\\ __ _ __ __ __ __ _ __\\ \\ \\/\\ \\ ___ ___\\ \\ \\/'\\ __ _ __"
echo "\\ \\ \\__\\ \\ \\ \\/_/_\\/_\\__ \\ /'__\`\\/\\\`'__\\/\\ \\/\\ \\ /'__\`\\/\\\`'__\\ \\ \\ \\ \\ / __\`\\ /'___\\ \\ , < /'__\`\\/\\\`'__\\"
echo " \\ \\ \\_/\\ \\ \\ \\L\\ \\ /\\ \\L\\ \\/\\ __/\\ \\ \\/ \\ \\ \\_/ |/\\ __/\\ \\ \\/ \\ \\ \\_\\ \\/\\ \\L\\ \\/\\ \\__/\\ \\ \\\\\`\\ /\\ __/\\ \\ \\/"
echo " \\ \\_\\\\ \\_\\ \\____/ \\ \`\\____\\ \\____\\\\ \\_\\ \\ \\___/ \\ \\____\\\\ \\_\\ \\ \\____/\\ \\____/\\ \\____\\\\ \\_\\ \\_\\ \\____\\\\ \\_\\"
echo " \\/_/ \\/_/\\/___/ \\/_____/\\/____/ \\/_/ \\/__/ \\/____/ \\/_/ \\/___/ \\/___/ \\/____/ \\/_/\\/_/\\/____/ \\/_/"
}
function initialize() {
echo ":: Initializing MCSD"
export "ARGS=--nogui "
if [ -z "${MCSD_ENFORCE_FRESH_CACHES}" ]; then
export "ARGS=${ARGS}--eraseCache "
fi
if [ -z "${MCSD_FORCE_UPGRADE}" ]; then
export "ARGS=${ARGS}--forceUpgrade "
fi
if [ -z "${MCSD_SAFEMODE}" ]; then
export "ARGS=${ARGS}--safeMode "
fi
if [ ! "${MCSD_AUTO_RESTART}" == "true" ] && [ ! "${MCSD_AUTO_RESTART}" == "false" ]; then
echo ":: Warning: No/Invalid value supplied to \$MCSD_AUTO_RESTART, defaulting to false"
export "MCSD_AUTO_RESTART=false"
fi
if [ ! "${MCSD_CLEAN_LOGS}" == "true" ] && [ ! "${MCSD_CLEAN_LOGS}" == "false" ]; then
echo ":: Warning: No/Invalid value supplied to \$MCSD_CLEAN_LOGS, defaulting to false"
export "MCSD_CLEAN_LOGS=false"
fi
if [ ! "${MCSD_JAVA_VERSION}" == "17" ] && [ ! "${MCSD_JAVA_VERSION}" == "11" ] && [ ! "${MCSD_JAVA_VERSION}" == "8" ]; then
echo ":: Warning: No/Invalid java version supplied to \$MCSD_JAVA_VERSION, defaulting to java version 17"
export "MCSD_JAVA_VERSION=17"
fi
}
function check() {
echo ":: Checking environment"
if [ ! -d "/data" ]; then
echo ":: Error: Could not locate /data directory"
exit 1
fi
if [ ! -f "/data/server.jar" ]; then
echo ":: Error: Could not locate server.jar"
exit 1
fi
}
function accept_eula() {
echo "eula=true" > /data/eula.txt
}
function kill_server() {
kill -SIGINT "$CHILDPID"
}
function run() {
echo ":: Starting server"
cd /data
# We want to use ${ARGS} without using parenthesis
# shellcheck disable=SC2086
"/usr/lib/jvm/java-${MCSD_JAVA_VERSION}-openjdk/bin/java" -jar "/data/server.jar" ${ARGS} &
export "CHILDPID=${!}"
trap kill_server SIGTERM
trap kill_server SIGINT
wait
case "${MCSD_AUTO_RESTART}" in
"true")
echo ":: Restarting server after shutdown"
run
;;
"false")
echo ":: Ending execution"
exit 0
;;
*)
echo ":: An internal error occured (run function, can't decide MCSD_AUTO_RESTART)"
;;
esac
}
function main() {
print_banner
initialize
check
accept_eula
if [ "${MCSD_CLEAN_LOGS}" == "true" ]; then rm -rf "/data/logs" "/data/crash-reports"; fi
run
}
main