# syntax=docker/dockerfile:1
#
# The mm sandbox template: Docker Sandboxes' `shell` image with mm installed.
#
# Build it from the REPOSITORY ROOT (the context is the mm sources), or run
# docker-sbx/build.sh, which does exactly that and stamps mm's tag — the
# image tag is that tag without the "v", k33g/mm-sandbox:0.0.2 for v0.0.2:
#
#     docker build -f docker-sbx/template/Dockerfile \
#         --build-arg MM_VERSION=v0.0.2 -t k33g/mm-sandbox:0.0.2 .
#
# The two kits under docker-sbx/kits/ name this image.
# ENTRYPOINT and CMD are deliberately not set: the sandbox runtime keeps the
# shell template's tini entrypoint and replaces CMD with its own, and the kit's
# `sandbox.entrypoint` is what starts mm when you attach.

ARG GO_VERSION=1.26
ARG SHELL_TEMPLATE=docker/sandbox-templates:shell

# ---- build stage: cross-compile mm for the target platform -----------------
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-bookworm AS build
ARG TARGETOS
ARG TARGETARCH
# What `mm --version` reports: the tag, as 02-build-releases.sh stamps it.
ARG MM_VERSION=dev

WORKDIR /src
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod go mod download
COPY . .
# Same flags as 02-build-releases.sh: no C, no local paths, no symbols, the
# version stamped into internal/version.
RUN --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/root/.cache/go-build \
    CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
    go build -trimpath -ldflags "-s -w -X mm/internal/version.stamped=${MM_VERSION}" -o /out/mm .

# ---- template: the shell sandbox plus mm -----------------------------------
FROM ${SHELL_TEMPLATE}

COPY --from=build --chmod=0755 /out/mm /usr/local/bin/mm
# The two kit entry points. mm-tui runs `mm -tui "$@"`; mm-acp-info prints how
# an editor reaches this sandbox and opens a shell (see docker-sbx/README.md).
COPY --chmod=0755 docker-sbx/template/mm-tui docker-sbx/template/mm-acp-info /usr/local/bin/

# tcell (the window mode) needs a terminal type and a UTF-8 locale; the kits
# set them too, this is the fallback for a plain `docker run`.
ENV TERM=xterm-256color \
    COLORTERM=truecolor \
    LANG=C.UTF-8

# Prove the binary runs on this platform before the image is tagged.
RUN mm --version && mm-tui --version
