# syntax=docker/dockerfile:1
# IMAGE_NAME=livecodebench_pro

# Adapted from https://github.com/YanagiOrigami/LightCPVerifier at commit
# f6637f470f7c3d7d879986dc6ee057cdc5e7b0ac, with changes documented in the comments
FROM ubuntu:22.04

ENV DEBIAN_FRONTEND=noninteractive

# Install base dependencies, language toolchains, and Node.js
RUN apt-get update && apt-get install -y --no-install-recommends \
  ca-certificates \
  curl \
  jq \
  git \
  unzip \
  zip \
  build-essential \
  pkg-config \
  python3 \
  python3-pip \
  pypy3 \
  openjdk-17-jdk \
  kotlin \
  rustc \
  cargo \
  golang \
  && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
  && apt-get install -y --no-install-recommends nodejs \
  && rm -rf /var/lib/apt/lists/*

# Change 1: npm@latest requires a newer Node engine than 20.x and fails to install
RUN npm install -g npm@10

# Install go-judge (auto-detect architecture)
RUN set -eux; \
  arch="$(uname -m)"; \
  case "$arch" in \
  x86_64)  goarch="amd64" ;; \
  aarch64) goarch="arm64" ;; \
  *) echo "Unsupported arch: $arch" && exit 1 ;; \
  esac; \
  url=$(curl -fsSL https://api.github.com/repos/criyle/go-judge/releases/latest \
  | jq -r --arg goarch "$goarch" '.assets[] | select(.name | test("linux.*\($goarch).*tar.gz$")) | .browser_download_url' \
  | head -n 1); \
  echo "Downloading go-judge for $arch ($url)"; \
  curl -fsSL "$url" | tar -xz -C /usr/local/bin go-judge; \
  chmod +x /usr/local/bin/go-judge

# Change 2: Fetch the app source from upstream at a pinned commit rather than vendoring it,
# so this Dockerfile stays reproducible against a known-good LightCPVerifier revision
WORKDIR /app

RUN git clone https://github.com/YanagiOrigami/LightCPVerifier.git /tmp/lcv \
  && cd /tmp/lcv \
  && git checkout f6637f470f7c3d7d879986dc6ee057cdc5e7b0ac \
  && cp package.json package-lock.json* /app/ \
  && cp server.js entrypoint.sh /app/ \
  && cp -r src/ /app/src/ \
  && cp -r include/ /app/include/ \
  && cp -r config/ /app/config/ \
  && cp -r include/ /lib/testlib/ \
  && rm -rf /tmp/lcv

RUN npm install --omit=dev --ignore-scripts

RUN chmod +x entrypoint.sh && sed -i 's/\r$//' entrypoint.sh

# Change 3: Hardcoded rather than $(nproc): Dockerfile ENV does not perform shell substitution
ENV PORT=8081 \
  GJ_ADDR=http://127.0.0.1:5050 \
  JUDGE_WORKERS=8 \
  GJ_PARALLELISM=8

ENTRYPOINT ["/app/entrypoint.sh"]
