# ── Stage 1: Go Backend Build (CGO-frei, Manticore Search) ───────────────────
FROM golang:1.24-bookworm AS go-builder

WORKDIR /build
COPY go.mod go.sum ./
RUN go mod download

COPY . .
RUN CGO_ENABLED=0 go build -buildvcs=false \
    -ldflags="-s -w" \
    -o /out/archivmail ./cmd/archivmail/

# ── Stage 2: Runtime Image ────────────────────────────────────────────────────
FROM debian:bookworm-slim

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

RUN useradd --system --shell /bin/false --uid 999 --create-home archivmail

COPY --from=go-builder /out/archivmail /usr/local/bin/archivmail

# Volumes: config + data werden von außen gemountet
# Manticore Search läuft als eigener Dienst/Container (Port 9306), nicht hier
RUN mkdir -p /var/archivmail/store /var/archivmail/astore \
    && chown -R archivmail:archivmail /var/archivmail

USER archivmail
EXPOSE 8080 2525 1143

ENTRYPOINT ["/usr/local/bin/archivmail"]
CMD ["-config", "/etc/archivmail/config.yml"]
