# Mathew — the Lean editor server
#
# Builds lean4web (https://github.com/leanprover-community/lean4web) from its
# upstream source and runs its production server: one port serves the editor
# client and the Lean sessions, which is what the wiki frames at /lean.
#
# Lean4web runs a real Lean process per session, so upstream asks for a
# dedicated server — this image carries the editor and Mathlib, the machine
# carries the load.
#
#   docker build -t mathew-lean ./lean
#   docker run -p 8080:8080 mathew-lean          # → http://localhost:8080
#
# The build is heavy by nature (Mathlib is fetched and its precompiled
# cache unpacked); expect a long build and an image of several GB.

FROM node:24-bookworm

# lake fetches dependencies over git/curl, drives clang to link, and elan
# (Lean's toolchain manager) installs the toolchains each project pins
RUN apt-get update && apt-get install -y --no-install-recommends \
      bash curl git ca-certificates unzip rsync \
      build-essential clang pkg-config libssl-dev \
    && rm -rf /var/lib/apt/lists/*

# elan, with no default toolchain — every project pins its own in its
# lean-toolchain file, and elan installs that one when asked
RUN curl -sSfL https://elan.lean-lang.org/elan-init.sh | bash -s -- -y --default-toolchain none
ENV PATH="/root/.elan/bin:${PATH}"

# The lean4web source. Pass a tag or branch to pin a build:
#   docker build --build-arg LEAN4WEB_REF=v0.2.2 ./lean
ARG LEAN4WEB_REF=main
RUN git clone --depth 1 --branch "${LEAN4WEB_REF}" \
      https://github.com/leanprover-community/lean4web.git /opt/lean4web

WORKDIR /opt/lean4web
RUN npm install

# Build the Lean projects under Projects/ (MathlibDemo pulls Mathlib and
# unpacks its precompiled cache — the slow part of the image) and then the
# editor client, which the server serves alongside its websocket
RUN npm run build:projects

# Mathew's theme layer: the wiki's font, stylesheet, and theme handshake
# join the client's public/ so they ship in the built bundle, and a sed
# wires them into index.html (and renames the tab). The wiki opens the
# frame as ?theme=light|dark and the editor opens matching the page
# around it — lean/theme/mathew.css explains how the skin attaches.
COPY theme/mathew.css theme/theme-init.js ./client/public/
COPY theme/fonts/. ./client/public/fonts/
RUN sed -i \
      -e 's#<link rel="stylesheet" href="src/css/index.css" />#<script src="/theme-init.js"></script>\n    <link rel="stylesheet" href="src/css/index.css" />\n    <link rel="stylesheet" href="/mathew.css" />#' \
      -e 's#<title>Lean Web</title>#<title>Lean · Mathew</title>#' \
      client/index.html

RUN npm run build:client

# Bubblewrap cannot sandbox inside a container (no nested user namespaces by
# default) — Docker itself is the isolation here, so the server runs Lean
# directly. PORT 8080 is lean4web's production default: editor + websocket.
ENV NODE_ENV=production \
    NO_BWRAP=true \
    PORT=8080
EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=5s --start-period=90s --retries=3 \
  CMD curl -sf http://localhost:8080/health || exit 1

CMD ["npm", "run", "prod"]
