Files
TFMC-Discord/docker/shulker/entrypoint.sh
Marc 4f13a498a2
Some checks failed
Build and Push Shulker Image / build (push) Failing after 12s
small fixes
2025-10-02 10:35:24 +02:00

85 lines
4.2 KiB
Bash

#!/bin/sh
set -e
# Ensure runtime dirs exist and are writable for the app user when running as root
if [ "$(id -u)" = "0" ]; then
mkdir -p /data /minecraft/logs || true
# 10001 is the shulker user created in the image
chown -R 10001:10001 /data /minecraft || true
fi
# If no config exists in /data, seed it from the example and apply env overrides if provided
if [ ! -f /data/config.json ]; then
echo "Seeding /data/config.json from example"
cp /app/config.example.json /data/config.json
fi
# Ensure LOCAL_FILE_PATH points to the mounted logs unless explicitly overridden via env
if [ -z "${LOCAL_FILE_PATH_OVERRIDE}" ]; then
LOCAL_FILE_PATH="/minecraft/logs/latest.log"
else
LOCAL_FILE_PATH="${LOCAL_FILE_PATH_OVERRIDE}"
fi
# Apply a few common ENV overrides into config.json if provided
json_set() {
KEY="$1"; VALUE="$2"; FILE="$3"
# Use jq if available for robust edits; otherwise sed fallback
if command -v jq >/dev/null 2>&1; then
tmp=$(mktemp)
jq --arg v "$VALUE" ".[$KEY]=$VALUE" "$FILE" > "$tmp" && mv "$tmp" "$FILE" || true
else
# naive sed replace; expects simple string or number values already present
sed -i "s#\(\"$KEY\"\): .*#\\\"$KEY\\\": $VALUE,#" "$FILE" || true
fi
}
# Build a small jq script to apply overrides
if command -v jq >/dev/null 2>&1; then
jq \
--argfile cfg /data/config.json \
'.' >/dev/null 2>&1 || true
fi
# When env vars are set, write them. Values must be valid JSON literals (strings quoted).
[ -n "${DISCORD_TOKEN}" ] && jq ".DISCORD_TOKEN=\"${DISCORD_TOKEN}\"" /data/config.json > /data/config.json.tmp && mv /data/config.json.tmp /data/config.json || true
[ -n "${DISCORD_CHANNEL_ID}" ] && jq ".DISCORD_CHANNEL_ID=\"${DISCORD_CHANNEL_ID}\"" /data/config.json > /data/config.json.tmp && mv /data/config.json.tmp /data/config.json || true
[ -n "${WEBHOOK_URL}" ] && jq ".WEBHOOK_URL=\"${WEBHOOK_URL}\"" /data/config.json > /data/config.json.tmp && mv /data/config.json.tmp /data/config.json || true
[ -n "${DISCORD_MESSAGE_TEMPLATE}" ] && jq ".DISCORD_MESSAGE_TEMPLATE=\"${DISCORD_MESSAGE_TEMPLATE}\"" /data/config.json > /data/config.json.tmp && mv /data/config.json.tmp /data/config.json || true
[ -n "${DEBUG}" ] && jq ".DEBUG=${DEBUG}" /data/config.json > /data/config.json.tmp && mv /data/config.json.tmp /data/config.json || true
# Default behavior: if no WEBHOOK_URL, disable webhooks; otherwise enable them.
# If USE_WEBHOOKS is explicitly set via env, it overrides this default.
if [ -z "${USE_WEBHOOKS}" ]; then
if jq -e '.WEBHOOK_URL and (.WEBHOOK_URL | length) > 0' /data/config.json >/dev/null 2>&1; then
jq ".USE_WEBHOOKS=true" /data/config.json > /data/config.json.tmp && mv /data/config.json.tmp /data/config.json
else
jq ".USE_WEBHOOKS=false" /data/config.json > /data/config.json.tmp && mv /data/config.json.tmp /data/config.json
fi
else
case "${USE_WEBHOOKS}" in
[Tt][Rr][Uu][Ee]|1|yes|on) jq ".USE_WEBHOOKS=true" /data/config.json > /data/config.json.tmp && mv /data/config.json.tmp /data/config.json ;;
[Ff][Aa][Ll][Ss][Ee]|0|no|off) jq ".USE_WEBHOOKS=false" /data/config.json > /data/config.json.tmp && mv /data/config.json.tmp /data/config.json ;;
esac
fi
# Customize server identity shown in Discord when posting server messages via webhook
[ -n "${SERVER_NAME}" ] && jq ".SERVER_NAME=\"${SERVER_NAME}\"" /data/config.json > /data/config.json.tmp && mv /data/config.json.tmp /data/config.json || true
[ -n "${SERVER_IMAGE}" ] && jq ".SERVER_IMAGE=\"${SERVER_IMAGE}\"" /data/config.json > /data/config.json.tmp && mv /data/config.json.tmp /data/config.json || true
# Always set IS_LOCAL_FILE true and path to mounted logs unless overridden explicitly via env IS_LOCAL_FILE
if [ -z "${IS_LOCAL_FILE}" ]; then IS_LOCAL_FILE=true; fi
jq ".IS_LOCAL_FILE=${IS_LOCAL_FILE} | .LOCAL_FILE_PATH=\"${LOCAL_FILE_PATH}\"" /data/config.json > /data/config.json.tmp && mv /data/config.json.tmp /data/config.json
# Link config into app directory where shulker expects it
ln -sf /data/config.json /app/config.json
# If running as root, drop privileges to shulker before starting the app
if [ "$(id -u)" = "0" ]; then
# Ensure ownership after seeding as root
chown -R 10001:10001 /data || true
exec su-exec 10001:10001 "$@"
else
exec "$@"
fi