# DO NOT EDIT
# This file is automatically generated with copier using https://codeberg.org/slidge/legacy-module-template

# Builder
# *******
FROM ghcr.io/astral-sh/uv:python3.13-bookworm-slim AS builder

# install git for setuptools scm
RUN apt-get update -y && \
    apt-get install \
        git \
        make \
        cargo \
        cmake \
        g++ \
        -y --no-install-recommends && \
    rm -rf /var/lib/apt/lists/*
WORKDIR /build
ENV UV_PROJECT_ENVIRONMENT=/venv
ENV PATH="/venv/bin:/root/.cargo/bin:$PATH"
# By making the venv relocatable, we can copy it to /workspace/.venv in CI, and only update
# dependencies that might be out-of-date. If uv.lock is not modified, then updating
# the .venv is a no-op.
# This allows us to not wait on the CI container build for the test workflows to run,
# and also to run workflows for pull requests that modify the lockfile. In this,
# situation, updating the CI container is not an option.
RUN uv venv $UV_PROJECT_ENVIRONMENT --relocatable
COPY uv.lock pyproject.toml README.md .
COPY matridge matridge
# install dependencies in /venv
# .git/ needs to be mounted for setuptools-scm to set the version
RUN --mount=source=.git,target=/build/.git,type=bind \
    uv sync --no-dev

# CI container
# ************
FROM builder AS ci

# In CI we copy /venv to .venv, then update it for the whole workflow.
RUN --mount=source=.git,target=/build/.git,type=bind \
    uv sync --all-groups --no-install-project
ENV UV_PROJECT_ENVIRONMENT=.venv
ENV UV_LINK_MODE=copy
ENV PATH="/woodpecker/src/codeberg.org/slidge/matridge/.venv/bin:$PATH"

# Dev container
# *************
FROM builder AS dev

# copy "localhost" certs from the prosody slidge dev container, so
COPY --from=codeberg.org/slidge/prosody-slidge-dev:latest \
  /etc/prosody/certs/localhost.crt \
  /usr/local/share/ca-certificates/
RUN update-ca-certificates
RUN apt-get update -y \
    && apt-get install -y --no-install-recommends \
        libmagic1 \
        media-types \
        shared-mime-info \
    && rm -rf /var/lib/apt/lists/*
RUN uv pip install watchdog[watchmedo]
COPY --from=ci /venv /venv
ENTRYPOINT ["watchmedo", "auto-restart", \
  "--pattern", "*.py", \
  "--directory", "/build/matridge", \
  "--recursive", \
  "matridge", "--", \
  "--jid", "slidge.localhost", \
  "--secret", "secret", \
  "--debug", \
  "--upload-service", "upload.localhost", \
  "--admins", "test@localhost", \
  "--dev-mode"]

# Prod container
# **************
FROM docker.io/python:3.13-slim-bookworm AS matridge

ENV PYTHONUNBUFFERED=1
ENV PATH="/venv/bin:$PATH"
STOPSIGNAL SIGINT
WORKDIR /var/lib/slidge
# libmagic1: to guess mime type from files
# media-types: to determine file name suffix based on file type
RUN apt-get update -y \
    && apt-get install -y --no-install-recommends \
        libmagic1 \
        media-types \
        shared-mime-info \
    && rm -rf /var/lib/apt/lists/*
RUN addgroup --system --gid 10000 slidge
RUN adduser --system --uid 10000 --ingroup slidge --home /var/lib/slidge slidge
USER slidge
COPY --from=builder /venv /venv
COPY --from=builder /build/matridge /venv/lib/python3.13/site-packages/matridge
ENTRYPOINT ["matridge"]
