# Construye HydroJudge desde el código fuente de este fork (no desde npm).
# Contexto de build: la raíz del repositorio (ver docker-compose.yml).
FROM node:22

ENV TZ=Europe/Madrid

RUN apt-get -qq update && \
    apt-get install -y \
    gcc \
    python3 \
    g++ \
    fp-compiler \
    openjdk-17-jdk-headless \
    php-cli \
    rustc \
    ghc \
    libjavascriptcoregtk-4.0-bin \
    golang \
    ruby \
    mono-runtime \
    mono-mcs && \
    # node is at /usr/local/bin but hydrojudge will find it at /usr/bin
    # so make a symlink to it
    ln -s /usr/local/bin/node /usr/bin/node

WORKDIR /app
COPY . .

# El sandbox go-judge se descarga según la arquitectura (amd64 para el VPS,
# arm64 para probar en local en un Mac con Apple Silicon).
ARG TARGETARCH
RUN yarn set version 4.13.0 && \
    yarn install && \
    yarn build && \
    yarn cache clean --all && \
    npm install -g pm2 && \
    ln -s /app/node_modules/.bin/hydrojudge /usr/local/bin/hydrojudge && \
    case "$TARGETARCH" in \
      arm64) SANDBOX=go-judge_1.9.4_linux_arm64 ;; \
      *) SANDBOX=go-judge_1.9.4_linux_amd64v3 ;; \
    esac && \
    wget -q "https://github.com/criyle/go-judge/releases/download/v1.9.4/$SANDBOX" -O /usr/bin/sandbox && \
    chmod +x /usr/bin/sandbox /app/install/docker/judge/entrypoint.sh

ENTRYPOINT ["/app/install/docker/judge/entrypoint.sh"]
