# Single Bela image: builds the web app (Kotlin/Wasm) and the game server (Ktor), then ships one runtime
# container where the server runs the online rooms (WebSocket /ws) and serves the web app at /.
# Build context is the repo root. The Android app is not built here (-PbelaNoAndroid=true).

# ---- 1. Build the web bundle and the server ----
FROM eclipse-temurin:21-jdk-noble AS build
# Kotlin/Wasm downloads its own Node.js for webpack; Node needs libatomic.
RUN apt-get update     && apt-get install -y --no-install-recommends libatomic1     && rm -rf /var/lib/apt/lists/*
WORKDIR /src
ENV GRADLE_OPTS="-Dorg.gradle.daemon=false" \
    BELA_GRADLE_FLAGS="-PbelaNoAndroid=true --no-configuration-cache"

# Wrapper and build scripts first: the Gradle distribution, plugins and dependencies are a cached layer
# that survives source-only changes.
COPY gradlew settings.gradle.kts build.gradle.kts gradle.properties ./
COPY gradle ./gradle
COPY engine/build.gradle.kts engine/
COPY shared/build.gradle.kts shared/
COPY web/build.gradle.kts web/
COPY server/build.gradle.kts server/
RUN chmod +x gradlew && ./gradlew $BELA_GRADLE_FLAGS help

COPY . .
# The wrapper may arrive without its executable bit (a checkout on Windows).
RUN chmod +x gradlew
# The commit count numbers the release like the Android app (0.1.2 = commit 12); CI passes it in because
# .git is not part of the build context.
ARG BELA_VERSION_CODE=1
RUN ./gradlew $BELA_GRADLE_FLAGS -PbelaVersionCode=$BELA_VERSION_CODE \
        :engine:jvmTest :server:test :web:wasmJsBrowserDistribution :server:installDist \
    # Pre-compressed copies: the server sends the .gz to browsers that accept it (the wasm shrinks ~3x).
    && find web/build/dist/wasmJs/productionExecutable -type f \
        \( -name '*.wasm' -o -name '*.js' -o -name '*.mjs' -o -name '*.html' -o -name '*.webmanifest' -o -name '*.ttf' \) \
        -exec gzip -9 -k {} \;

# ---- 2. Runtime ----
FROM eclipse-temurin:21-jre-noble AS runtime
# Latest OS security patches on top of the base image. OS_PATCHES (the build date, from CI) re-runs this
# layer daily, so a CVE fixed upstream does not linger in a cached layer.
ARG OS_PATCHES=0
RUN echo "OS patches as of ${OS_PATCHES}" \
    && apt-get update \
    && apt-get upgrade -y \
    && rm -rf /var/lib/apt/lists/* \
    && useradd --system --uid 10001 --no-create-home bela \
    # The open rooms are saved here across restarts; mount a volume or a host folder on it to keep them.
    && mkdir /data && chown bela:bela /data

WORKDIR /app
COPY --from=build /src/server/build/install/server ./server
COPY --from=build /src/web/build/dist/wasmJs/productionExecutable ./web
# Starts as root only to make /data writable for bela (a mounted host folder can be owned by anyone), then runs
# the server as bela. See docker-entrypoint.sh.
COPY --chmod=755 docker-entrypoint.sh ./entrypoint.sh

ENV PORT=8080 \
    WEB_ROOT=/app/web \
    DATA_DIR=/data \
    JAVA_OPTS="-XX:MaxRAMPercentage=75 -XX:+UseSerialGC -XX:+ExitOnOutOfMemoryError"
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
    CMD curl -fsS http://localhost:8080/health || exit 1
ENTRYPOINT ["/app/entrypoint.sh"]
