# OpenCode Tool Init Container
#
# This minimal image provides the OpenCode CLI binary for copying to shared volumes.
# Used as an init container to provision OpenCode to work containers.
#
# Architecture:
#   Init Container (this image) → copies → /tools/opencode → Work Container uses it
#
# Usage:
#   # As init container in Kubernetes Pod:
#   initContainers:
#     - name: opencode-init
#       image: ghcr.io/kubeopencode/kubeopencode-agent-opencode:latest
#       volumeMounts:
#         - name: tools
#           mountPath: /tools
#   containers:
#     - name: agent
#       image: ghcr.io/kubeopencode/kubeopencode-agent-common-dev:latest
#       command: ["/tools/opencode", "run", "--format", "json", "..."]
#       volumeMounts:
#         - name: tools
#           mountPath: /tools
#
# Binary Source:
#   Downloaded from GitHub Releases: https://github.com/anomalyco/opencode/releases
#   - Linux x64: opencode-linux-x64.tar.gz (glibc)
#   - Linux arm64: opencode-linux-arm64.tar.gz (glibc)
#
# Note: The binary is a glibc variant, executed by the glibc-based work container.
# The init container (Alpine) only stores and copies the binary — it does not run it.
# The entrypoint version check gracefully skips if the binary is incompatible at copy time.
#
# Version Management:
#   OPENCODE_VERSION is defined in agents/Makefile (single source of truth).
#   When building via make, it is passed automatically as --build-arg.
#   For standalone docker build, you must pass it explicitly:
#     docker build --build-arg OPENCODE_VERSION=1.15.0 -t opencode .
#

# Stage 1: download and verify the binary using glibc (Debian)
FROM debian:bookworm-20260518-slim AS downloader

# OpenCode version to download — must be passed via --build-arg (no default).
# Single source of truth: agents/Makefile (OPENCODE_VERSION variable).
ARG OPENCODE_VERSION

# Install curl for downloading
RUN apt-get update && apt-get install -y --no-install-recommends \
    curl \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Detect architecture and download appropriate binary
ARG TARGETARCH
RUN set -ex; \
    case "${TARGETARCH}" in \
        amd64) ARCH="x64" ;; \
        arm64) ARCH="arm64" ;; \
        *) echo "Unsupported architecture: ${TARGETARCH}" && exit 1 ;; \
    esac; \
    FILENAME="opencode-linux-${ARCH}.tar.gz"; \
    URL="https://github.com/anomalyco/opencode/releases/download/v${OPENCODE_VERSION}/${FILENAME}"; \
    echo "Downloading OpenCode v${OPENCODE_VERSION} from ${URL}..."; \
    curl --retry 5 --retry-delay 3 --retry-all-errors -fsSL "${URL}" -o /tmp/opencode.tar.gz; \
    tar -xzf /tmp/opencode.tar.gz -C /tmp; \
    mv /tmp/opencode /opencode; \
    chmod +x /opencode; \
    rm -f /tmp/opencode.tar.gz; \
    # Verify the binary
    /opencode --version | tee /opencode-version

# Stage 2: minimal Alpine image that carries the binary
FROM alpine:3.23.4

# Default environment variables
ENV TOOLS_DIR=/tools
ENV OPENCODE_BIN=opencode

# Copy entrypoint script
COPY --from=downloader /opencode /opencode
COPY --from=downloader /opencode-version /opencode-version
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
