# syntax=docker/dockerfile:1.7

ARG GODOT_VERSION=4.6.3

FROM ubuntu:24.04 AS web-builder
ARG GODOT_VERSION
ARG SQLITE_VERSION=4.8
ARG WEB_BUILD_ID=3.0.1-fr-joy-fast
ARG TARGETARCH

RUN apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends ca-certificates curl libfontconfig1 unzip \
    && rm -rf /var/lib/apt/lists/*

RUN case "$TARGETARCH" in \
        amd64) GODOT_ARCH="x86_64" ;; \
        arm64) GODOT_ARCH="arm64" ;; \
        *) echo "Unsupported build architecture: $TARGETARCH" >&2; exit 1 ;; \
    esac \
    && curl -fsSL "https://github.com/godotengine/godot/releases/download/${GODOT_VERSION}-stable/Godot_v${GODOT_VERSION}-stable_linux.${GODOT_ARCH}.zip" -o /tmp/godot.zip \
    && unzip -q /tmp/godot.zip -d /usr/local/bin \
    && mv "/usr/local/bin/Godot_v${GODOT_VERSION}-stable_linux.${GODOT_ARCH}" /usr/local/bin/godot \
    && chmod +x /usr/local/bin/godot \
    && mkdir -p "/root/.local/share/godot/export_templates/${GODOT_VERSION}.stable" \
    && curl -fsSL "https://github.com/godotengine/godot/releases/download/${GODOT_VERSION}-stable/Godot_v${GODOT_VERSION}-stable_export_templates.tpz" -o /tmp/templates.tpz \
    && unzip -q /tmp/templates.tpz -d /tmp/godot-templates \
    && mv /tmp/godot-templates/templates/* "/root/.local/share/godot/export_templates/${GODOT_VERSION}.stable/" \
    && rm -rf /tmp/godot.zip /tmp/templates.tpz /tmp/godot-templates

WORKDIR /src
COPY . .

RUN mkdir -p addons/godot-sqlite/bin \
    && curl -fsSL "https://github.com/2shady4u/godot-sqlite/releases/download/v${SQLITE_VERSION}/addons.zip" -o /tmp/godot-sqlite.zip \
    && unzip -j /tmp/godot-sqlite.zip \
        'addons/godot-sqlite/bin/libgdsqlite.web.template_release.wasm32.wasm' \
        -d addons/godot-sqlite/bin \
    && rm /tmp/godot-sqlite.zip \
    && find assets -type f -name '*.import' -exec sed -i 's|^compress/mode=2$|compress/mode=0|' {} +

RUN mkdir -p dist \
    && godot --headless --path /src --import \
    && find .godot/imported -name 'title.png-*.ctex' ! -name '*.etc2.ctex' ! -name '*.s3tc.ctex' -type f -size +0c | grep -q . \
    && find .godot/imported -name 'kalulu_sprite_sheet.png-*.ctex' ! -name '*.etc2.ctex' ! -name '*.s3tc.ctex' -type f -size +0c | grep -q .

RUN godot --headless --path /src --export-release "Web Test" dist/index.html \
    && mv dist/index.pck "dist/index-${WEB_BUILD_ID}.pck" \
    && sed -i "s|\"index.pck\":|\"index-${WEB_BUILD_ID}.pck\":|; s|\"focusCanvas\":true,|\"focusCanvas\":true,\"mainPack\":\"index-${WEB_BUILD_ID}.pck\",|" dist/index.html \
    && test -s dist/index.html \
    && test -s dist/index.wasm \
    && test -s "dist/index-${WEB_BUILD_ID}.pck"

FROM alpine:3.22 AS french-content
ARG FRENCH_PACK_VERSION=v20260615-9e5d0d4
RUN apk add --no-cache ca-certificates curl \
    && mkdir -p /content \
    && curl -fsSL "https://github.com/Excello-Recherche-Education/Kalulu-Languages/releases/download/${FRENCH_PACK_VERSION}/fr_FR.zip" -o /content/fr_FR.zip \
    && test -s /content/fr_FR.zip

FROM alpine:3.22 AS compressed-web
RUN apk add --no-cache gzip
COPY --from=web-builder /src/dist/ /web/
RUN find /web -type f \( -name '*.js' -o -name '*.pck' -o -name '*.wasm' \) \
      -exec gzip -9 -k {} \;

FROM nginx:1.29.1-alpine AS runtime
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=compressed-web /web/ /usr/share/nginx/html/
COPY --from=french-content /content/fr_FR.zip /usr/share/nginx/html/content/fr_FR.zip

EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD wget -q -O /dev/null http://127.0.0.1:8080/healthz || exit 1
