Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ RUN mkdir -p /config \
zlib1g \
liblzma5 \
liblzo2-2 \
nano
nano \
gosu \
&& gosu nobody true

WORKDIR /config

# Copy the game data from the build container
COPY --from=ottd_build /app /app

# Add the entrypoint
ADD startgame.sh /usr/local/bin/startgame
ADD entrypoint.sh /usr/local/bin/entrypoint

# Expose the volume
Expand All @@ -89,5 +92,4 @@ ENV XDG_DATA_HOME=/config
ENV PATH="$PATH:/app"

# Finally, let's run OpenTTD!
USER openttd
CMD [ "/usr/local/bin/entrypoint" ]
87 changes: 24 additions & 63 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,70 +1,31 @@
#!/bin/sh

# This script is based fairly heavily off bateau84/openttd's. Thanks, man!
# This is the script that will run at the end
set -- /usr/local/bin/startgame "$@"

SAVEPATH="/config/save"
LOADGAME_CHECK="${loadgame}x"
EXTRA_FLAGS="-c /config/openttd.cfg"
# Change openttd user and group ids to what was specified
PUID=${PUID:-911}
PGID=${PGID:-1000}
groupmod -o -g "$PGID" openttd
usermod -o -u "$PUID" openttd

# Required to force config to save to /config
if [ -f /config/.config/openttd.cfg ]; then
export XDG_DATA_HOME=''
SAVEPATH="/config/.config/save"
EXTRA_FLAGS="-c /config/.config/openttd.cfg"
echo "WARN: Using legacy configuration directory /config/.config/ - it is recommended to migrate to all data inside /config/* when possible."
elif [ ! -f /config/openttd.cfg ]; then
# we start the server then kill it quickly to write a config file
# yes this is a horrific hack but whatever
echo "INFO: No config file found: generating one"
timeout 3 /app/openttd -D ${EXTRA_FLAGS} > /dev/null 2>&1
fi
# Take ownership of /config and /app
chown -R openttd:openttd /config
chown -R openttd:openttd /app

if [ "${LOADGAME_CHECK}" != "x" ]; then
case ${loadgame} in
'false')
echo "INFO: Creating a new game."
exec /app/openttd -D ${EXTRA_FLAGS} -x -d ${DEBUG}
exit 0
;;
'last-autosave')
SAVEGAME_TARGET=`ls -rt ${SAVEPATH}/autosave/*.sav | tail -n1`
echo "

User UID: $(id -u openttd)
User GID: $(id -g openttd)

───────────────────────────────────────

"

# Drop from root to the openttd user
set -- gosu openttd "$@"

# Run original entrypoint
exec "$@"

if [ -r "${SAVEGAME_TARGET}" ]; then
echo "INFO: Loading from latest autosave - ${SAVEGAME_TARGET}"
exec /app/openttd -D ${EXTRA_FLAGS} -g "${SAVEGAME_TARGET}" -x -d ${DEBUG}
exit 0
else
echo "FATAL: ${SAVEGAME_TARGET} not found"
exit 1
fi
;;
'exit')
SAVEGAME_TARGET="${SAVEPATH}/autosave/exit.sav"

if [ -r "${SAVEGAME_TARGET}" ]; then
echo "INFO: Loading from exit save"
exec /app/openttd -D ${EXTRA_FLAGS} -g "${SAVEGAME_TARGET}" -x -d ${DEBUG}
exit 0
else
echo "${SAVEGAME_TARGET} not found - Creating a new game."
exec /app/openttd -D ${EXTRA_FLAGS} -x -d ${DEBUG}
exit 0
fi
;;
*)
SAVEGAME_TARGET="${SAVEPATH}/${loadgame}"
if [ -r "${SAVEGAME_TARGET}" ]; then
echo "INFO: Loading ${SAVEGAME_TARGET}"
exec /app/openttd -D ${EXTRA_FLAGS} -g "${SAVEGAME_TARGET}" -x -d ${DEBUG}
exit 0
else
echo "FATAL: ${SAVEGAME_TARGET} not found"
exit 1
fi
;;
esac
else
echo "INFO: loadgame not set - Creating a new game."
exec /app/openttd -D ${EXTRA_FLAGS} -x -d ${DEBUG}
exit 0
fi
70 changes: 70 additions & 0 deletions startgame.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/sh

# This script is based fairly heavily off bateau84/openttd's. Thanks, man!

SAVEPATH="/config/save"
LOADGAME_CHECK="${loadgame}x"
EXTRA_FLAGS="-c /config/openttd.cfg"

# Required to force config to save to /config
if [ -f /config/.config/openttd.cfg ]; then
export XDG_DATA_HOME=''
SAVEPATH="/config/.config/save"
EXTRA_FLAGS="-c /config/.config/openttd.cfg"
echo "WARN: Using legacy configuration directory /config/.config/ - it is recommended to migrate to all data inside /config/* when possible."
elif [ ! -f /config/openttd.cfg ]; then
# we start the server then kill it quickly to write a config file
# yes this is a horrific hack but whatever
echo "INFO: No config file found: generating one"
timeout 3 /app/openttd -D ${EXTRA_FLAGS} > /dev/null 2>&1
fi

if [ "${LOADGAME_CHECK}" != "x" ]; then
case ${loadgame} in
'false')
echo "INFO: Creating a new game."
exec /app/openttd -D ${EXTRA_FLAGS} -x -d ${DEBUG}
exit 0
;;
'last-autosave')
SAVEGAME_TARGET=`ls -rt ${SAVEPATH}/autosave/*.sav | tail -n1`

if [ -r "${SAVEGAME_TARGET}" ]; then
echo "INFO: Loading from latest autosave - ${SAVEGAME_TARGET}"
exec /app/openttd -D ${EXTRA_FLAGS} -g "${SAVEGAME_TARGET}" -x -d ${DEBUG}
exit 0
else
echo "FATAL: ${SAVEGAME_TARGET} not found"
exit 1
fi
;;
'exit')
SAVEGAME_TARGET="${SAVEPATH}/autosave/exit.sav"

if [ -r "${SAVEGAME_TARGET}" ]; then
echo "INFO: Loading from exit save"
exec /app/openttd -D ${EXTRA_FLAGS} -g "${SAVEGAME_TARGET}" -x -d ${DEBUG}
exit 0
else
echo "${SAVEGAME_TARGET} not found - Creating a new game."
exec /app/openttd -D ${EXTRA_FLAGS} -x -d ${DEBUG}
exit 0
fi
;;
*)
SAVEGAME_TARGET="${SAVEPATH}/${loadgame}"
if [ -r "${SAVEGAME_TARGET}" ]; then
echo "INFO: Loading ${SAVEGAME_TARGET}"
exec /app/openttd -D ${EXTRA_FLAGS} -g "${SAVEGAME_TARGET}" -x -d ${DEBUG}
exit 0
else
echo "FATAL: ${SAVEGAME_TARGET} not found"
exit 1
fi
;;
esac
else
echo "INFO: loadgame not set - Creating a new game."
exec /app/openttd -D ${EXTRA_FLAGS} -x -d ${DEBUG}
exit 0
fi