FROM ubuntu:24.04 AS exporter

ARG GODOT_VERSION=4.6.2
ARG GODOT_RELEASE=${GODOT_VERSION}-stable
ARG GODOT_TEMPLATE_DIR=${GODOT_VERSION}.stable
ARG BUILD_REVISION=development
ARG DEPLOYMENT_ENVIRONMENT=production
ARG PUBLIC_API_ORIGIN=https://us3q.ranaze.com

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        ca-certificates curl libarchive-tools unzip libasound2t64 libfontconfig1 libgl1 libx11-6 \
        libxcursor1 libxi6 libxinerama1 libxrandr2 \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /opt/godot
RUN curl -fsSLO "https://github.com/godotengine/godot-builds/releases/download/${GODOT_RELEASE}/Godot_v${GODOT_RELEASE}_linux.x86_64.zip" \
    && curl -fsSLO "https://github.com/godotengine/godot-builds/releases/download/${GODOT_RELEASE}/SHA512-SUMS.txt" \
    && grep "Godot_v${GODOT_RELEASE}_linux.x86_64.zip" SHA512-SUMS.txt > selected-checksums.txt \
    && sha512sum --check selected-checksums.txt \
    && unzip -q "Godot_v${GODOT_RELEASE}_linux.x86_64.zip" \
    && install -m 0755 "Godot_v${GODOT_RELEASE}_linux.x86_64" /usr/local/bin/godot \
    && mkdir -p "/root/.local/share/godot/export_templates/${GODOT_TEMPLATE_DIR}" \
    && curl -fsSL "https://github.com/godotengine/godot-builds/releases/download/${GODOT_RELEASE}/Godot_v${GODOT_RELEASE}_export_templates.tpz" \
        | bsdtar -xOf - templates/web_nothreads_release.zip \
        > "/root/.local/share/godot/export_templates/${GODOT_TEMPLATE_DIR}/web_nothreads_release.zip" \
    && test -s "/root/.local/share/godot/export_templates/${GODOT_TEMPLATE_DIR}/web_nothreads_release.zip" \
    && unzip -tq "/root/.local/share/godot/export_templates/${GODOT_TEMPLATE_DIR}/web_nothreads_release.zip" \
    && rm -f Godot_v*.zip Godot_v*_linux.x86_64 SHA512-SUMS.txt selected-checksums.txt

WORKDIR /src
COPY . .
RUN mkdir -p /build \
    && sed -i "s|build_revision=\"development\"|build_revision=\"${BUILD_REVISION}\"|" project.godot \
    && sed -i "s|deployment_environment=\"uat\"|deployment_environment=\"${DEPLOYMENT_ENVIRONMENT}\"|" project.godot \
    && sed -i "s|https://us3q.ranaze.com|${PUBLIC_API_ORIGIN}|g" project.godot \
    && sed -i "s|custom_template/release=\"\"|custom_template/release=\"/root/.local/share/godot/export_templates/${GODOT_TEMPLATE_DIR}/web_nothreads_release.zip\"|" export_presets.cfg \
    && godot --headless --editor --path /src --quit \
    && godot --verbose --headless --path /src --export-release Web /build/index.html \
    && cp /src/web/ranaze-loader-icon-v1.png /build/ranaze-loader-icon-v1.png \
    && cp /src/assets/fonts/Inter-Variable.ttf /build/medmind-inter.ttf \
    && cp /src/web/donation-complete.html /build/donation-complete.html \
    && cp /src/web/donation-canceled.html /build/donation-canceled.html \
    && cp /src/web/terms.html /src/web/privacy.html /src/web/legal-page.css /src/web/legal-page.js /src/web/external-redirect.html /build/ \
    && mkdir -p /build/legal \
    && cp /src/legal/legal_documents.json /build/legal/legal_documents.json \
    && test -s /build/index.html \
    && test -s /build/ranaze-loader-icon-v1.png \
    && test -s /build/medmind-inter.ttf \
    && test -s /build/donation-complete.html \
    && test -s /build/donation-canceled.html \
    && test -s /build/terms.html \
    && test -s /build/privacy.html \
    && test -s /build/external-redirect.html \
    && test -s /build/legal/legal_documents.json \
    && test -s /build/index.wasm \
    && test -s /build/index.pck

FROM nginx:1.27-alpine
COPY web/nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=exporter /build/ /usr/share/nginx/html/
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD wget -q -O /dev/null http://127.0.0.1:8080/healthz || exit 1
