# devkitPro's official image, used only as a source for the GameCube toolchain
# (see the devkitPPC section below).  `:latest` tracks devkitPro releases; pin a
# dated tag (e.g. :20250527) if a toolchain update ever breaks the build.
FROM devkitpro/devkitppc:latest AS devkitppc

FROM debian:sid-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    curl \
    git \
    git-lfs \
    make \
    sudo \
    python3 \
    python3-venv \
    python3-pip \
    yosys \
    nextpnr-ice40 \
    fpga-icestorm \
    libusb-1.0-0 \
    libftdi1-2 \
    tshark \
    nodejs npm \
    graphviz \
    poppler-utils \
    kicad \
    kicad-libraries \
    && rm -rf /var/lib/apt/lists/*
# libusb-1.0-0 + libftdi1-2: USB backend for `iceprog` (FPGA flash) and `pyftdi`
# (FT2232H EEPROM).  sudo: the /dev/bus/usb nodes are root-owned, so the vscode
# user flashes via `sudo` (NOPASSWD, configured below).

ENV TZ="Europe/Amsterdam"

# Debian marks the system Python as externally-managed (PEP 668), so install
# project deps into a venv. /opt/venv is first on PATH, so `python`/`pip` resolve
# to it for all users.
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv "$VIRTUAL_ENV"
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

# ENV PATH alone is not enough: Debian's /etc/profile REsets PATH unconditionally,
# so any *login* shell (VS Code's integrated terminal, `su - vscode`, ssh) drops
# /opt/venv/bin and then `python` does not exist at all (slim has only python3).
# Re-prepend the venv for login shells so the documented `python -m exi_bba.synth`
# works everywhere.
RUN printf 'PATH="%s/bin:$PATH"\nexport PATH\n' "$VIRTUAL_ENV" > /etc/profile.d/10-venv.sh

RUN npm install -g @anthropic-ai/claude-code

COPY requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir -r /tmp/requirements.txt

# pyftdi: programs the FT2232H EEPROM over libusb, and (with pyserial) reads the
# FT2232H channel-B UART directly over libusb for bring-up diagnostics.
RUN pip install --no-cache-dir pyftdi pyserial

# ── GameCube homebrew toolchain (devkitPPC + libogc) ────────────────────────
# Needed to build the on-console BBA test app in `gc_bba_test/` into a .dol.
#
# Copied out of devkitPro's official `devkitpro/devkitppc` image rather than
# installed with their `install-devkitpro-pacman` script: apt.devkitpro.org and
# pkg.devkitpro.org sit behind Cloudflare, which answers curl/wget/scripted
# fetches with HTTP 403 (browser user-agents get 200) -- from the host's
# `docker build` too, not just from inside the container.  The Docker image is
# devkitPro's sanctioned route for CI/containers and needs no network access to
# their hosts.  The binaries only link against libc, so they run fine on sid.
#
# Only the GameCube/PPC parts are copied (~540 MB); devkitARM, wut and the
# Wii/Wii U portlibs from the source image are skipped.
COPY --from=devkitppc /opt/devkitpro/devkitPPC         /opt/devkitpro/devkitPPC
COPY --from=devkitppc /opt/devkitpro/libogc            /opt/devkitpro/libogc
COPY --from=devkitppc /opt/devkitpro/tools             /opt/devkitpro/tools
COPY --from=devkitppc /opt/devkitpro/cmake             /opt/devkitpro/cmake
COPY --from=devkitppc /opt/devkitpro/licenses          /opt/devkitpro/licenses
COPY --from=devkitppc /opt/devkitpro/portlibs/ppc      /opt/devkitpro/portlibs/ppc
COPY --from=devkitppc /opt/devkitpro/portlibs/gamecube /opt/devkitpro/portlibs/gamecube

ENV DEVKITPRO=/opt/devkitpro
ENV DEVKITPPC=/opt/devkitpro/devkitPPC
ENV PATH="$DEVKITPPC/bin:$DEVKITPRO/tools/bin:$PATH"

# Same /etc/profile PATH-reset problem as the venv above: login shells drop the
# ENV PATH, and devkitPro's Makefiles resolve $(DEVKITPPC)/bin from the
# environment, so export all three for login shells too.
RUN printf 'export DEVKITPRO=/opt/devkitpro\nexport DEVKITPPC=/opt/devkitpro/devkitPPC\nPATH="$DEVKITPPC/bin:$DEVKITPRO/tools/bin:$PATH"\nexport PATH\n' \
      > /etc/profile.d/20-devkitpro.sh

RUN useradd -m -u 1000 -s /bin/bash vscode \
 && echo 'vscode ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/vscode \
 && chmod 0440 /etc/sudoers.d/vscode

USER vscode

# KiCad global library tables (copied from the installed templates) so kicad-cli
# can run the library-parity DRC/ERC checks. Without these, every standard
# footprint/symbol reports "library not configured". KiCad's built-in defaults
# resolve the ${KICAD10_*_DIR} paths inside the templates, so no env vars needed.
RUN mkdir -p /home/vscode/.config/kicad/10.0 \
 && cp /usr/share/kicad/template/fp-lib-table  /home/vscode/.config/kicad/10.0/fp-lib-table \
 && cp /usr/share/kicad/template/sym-lib-table /home/vscode/.config/kicad/10.0/sym-lib-table

WORKDIR /workspace
