# A Tidewall dedicated server image. Build it from the repository's root, which must
# hold the pinned engine in tools/godot/ (see tools/godot.sh):
#
#   docker build -f packaging/docker/Dockerfile -t tidewall-server:dev .
#
# or let packaging/docker/compose.yaml do it. The engine is checked against
# tools/engine.sha512 before it goes in. The game is imported while building, so at
# run time the image needs no writable filesystem apart from a small /tmp.
FROM debian:13

ARG ENGINE=tools/godot/Godot_v4.7.2-stable_linux.x86_64

RUN groupadd --system --gid 10650 tidewall \
	&& useradd --system --uid 10650 --gid tidewall --home-dir /nonexistent --no-create-home --shell /usr/sbin/nologin tidewall

COPY tools/engine.sha512 /tmp/engine.sha512
COPY ${ENGINE} /opt/tidewall/engine/godot
RUN expected="$(awk '$2 ~ /_linux\.x86_64$/ { print $1 }' /tmp/engine.sha512)" \
	&& actual="$(sha512sum /opt/tidewall/engine/godot | cut -d' ' -f1)" \
	&& [ -n "$expected" ] && [ "$expected" = "$actual" ] \
	&& chmod 0755 /opt/tidewall/engine/godot \
	&& rm /tmp/engine.sha512

COPY project.godot LICENSE LICENSE-ASSETS /opt/tidewall/game/
COPY src /opt/tidewall/game/src
COPY data /opt/tidewall/game/data
COPY packaging/server/tidewall-server /usr/local/bin/tidewall-server
COPY packaging/server/server.cfg /etc/tidewall/server.cfg

# Import once here, so the server never has to write into the game's directory.
RUN XDG_DATA_HOME=/tmp/import XDG_CONFIG_HOME=/tmp/import XDG_CACHE_HOME=/tmp/import \
		/opt/tidewall/engine/godot --headless --path /opt/tidewall/game --import --quit >/dev/null 2>&1 ; \
	test -d /opt/tidewall/game/.godot \
	&& rm -rf /tmp/import \
	&& chmod 0755 /usr/local/bin/tidewall-server

USER tidewall
ENV TIDEWALL_STATE=/tmp/tidewall-server
# Game traffic, LAN discovery, and the health check (TCP, on loopback by default).
EXPOSE 24650/udp 24651/udp
STOPSIGNAL SIGTERM
HEALTHCHECK --interval=10s --timeout=3s --start-period=20s --retries=3 \
	CMD ["bash", "-c", "exec 3<>/dev/tcp/127.0.0.1/24650 && printf 'GET /health HTTP/1.0\\r\\n\\r\\n' >&3 && head -n 1 <&3 | grep -q ' 200 '"]
ENTRYPOINT ["/usr/local/bin/tidewall-server"]
CMD ["--config=/etc/tidewall/server.cfg"]
