MCServerDocker/entrypoint.sh

85 lines
3 KiB
Bash
Raw Normal View History

2024-01-25 23:12:09 +01:00
#!/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 auto restart value supplied, defaulting to false"
export "MCSD_AUTO_RESTART=false"
fi
if [ ! "${MCSD_JAVA_VERSION}" == "17" ] && [ ! "${MCSD_JAVA_VERSION}" == "11" ] && [ ! "${MCSD_JAVA_VERSION}" == "8" ]; then
echo ":: Warning: No/Invalid java version supplied, 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 run() {
echo ":: Starting server"
# 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 "exitcode=${?}"
# yes it is assigned you idiot
# shellcheck disable=SC2154
if [ "${exitcode}" == "0" ]; then
echo ":: The server was shut down successfully"
else
echo ":: The server was forcefully shut down (code ${exitcode})"
fi
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
run
}
main