# syntax=docker/dockerfile:1
#
# AIMem for Apple `container` — Mode B (full) image: aimem (FUSE) + smbd.
# Re-exports /workspace to the macOS host over SMB. Native linux/arm64.
#
# Build from this directory (self-contained context — no source tree):
#   cd examples/apple-container
#   container build --arch arm64 -t aimem-mac-full -f Containerfile.full .

ARG UBUNTU_VERSION=24.04

# Runtime: base tooling + Samba + attr (xattr probe).
FROM ubuntu:${UBUNTU_VERSION}
ARG DEBIAN_FRONTEND=noninteractive
# A mirror mid-sync serves an index of the wrong size for a while; retry
# rather than fail the build on it. Five failures in a row still fail it.
RUN for attempt in 1 2 3 4 5; do \
        apt-get -o Acquire::Retries=3 update && break; \
        [ "$attempt" = 5 ] && exit 1; \
        echo "apt-get update failed (attempt $attempt), retrying"; sleep 15; \
    done \
    && apt-get install -y --no-install-recommends \
        ca-certificates curl iproute2 tini util-linux \
        samba samba-common-bin samba-vfs-modules attr \
    && rm -rf /var/lib/apt/lists/* \
    && smbd --version \
    && test -e /usr/lib/*/samba/vfs/fruit.so

RUN echo "user_allow_other" >> /etc/fuse.conf
COPY aimem-mount /usr/local/bin/aimem-mount
COPY entrypoint-full.sh /usr/local/bin/entrypoint-full.sh
COPY smb.conf /etc/samba/smb.conf

# Standard container binary-acquisition block (arm64). Auto-detects staged
# binaries in ./bin (CI/offline) else curls the released binaries from the
# CDN and verifies each against its published .sha256sum.
ARG AIMEM_VERSION=latest
ARG AIMEM_DL_BASE=https://dl.min.io/aistor/aimem/release
COPY bin/ /opt/aimem-staged/
# latest is served under the bare file name; a pinned tag lives under archive/,
# the only path that still resolves once a newer release ships.
RUN set -eux; \
    suffix=""; dir=""; \
    [ "${AIMEM_VERSION}" = latest ] || { suffix=".${AIMEM_VERSION}"; dir="archive/"; }; \
    for pair in "aimem:/usr/local/bin/aimem" "aimem-fusermount:/usr/bin/aimem-fusermount"; do \
        name=${pair%%:*}; dest=${pair#*:}; \
        if [ -f "/opt/aimem-staged/$name" ]; then \
            install -m 0755 "/opt/aimem-staged/$name" "$dest"; \
        else \
            curl -fsSL "${AIMEM_DL_BASE}/linux-arm64/${dir}${name}${suffix}" -o "$dest"; \
            curl -fsSL "${AIMEM_DL_BASE}/linux-arm64/${dir}${name}${suffix}.sha256sum" -o /tmp/aimem.sum; \
            echo "$(cut -d' ' -f1 /tmp/aimem.sum)  $dest" | sha256sum -c -; \
            chmod +x "$dest"; \
        fi; \
    done; \
    rm -rf /opt/aimem-staged /tmp/aimem.sum; \
    chmod +x /usr/local/bin/aimem-mount /usr/local/bin/entrypoint-full.sh; \
    testparm -s /etc/samba/smb.conf >/dev/null; \
    chown root:root /usr/bin/aimem-fusermount; chmod 4755 /usr/bin/aimem-fusermount; \
    /usr/local/bin/aimem --version

# Optional: layer a coding agent here, e.g.
#   RUN apt-get update && apt-get install -y --no-install-recommends nodejs npm \
#       && npm i -g @anthropic-ai/claude-code && rm -rf /var/lib/apt/lists/*

LABEL org.opencontainers.image.source="https://docs.min.io/aimem" \
      summary="AIMem workspace for Apple container (macOS) — Mode B full (FUSE + SMB)"

ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/entrypoint-full.sh"]
