# Container image for davical-go. See issue #12.
#
# Final image is FROM scratch and contains only three statically-linked
# binaries: davicald (the CalDAV/CardDAV server, run on startup), plus
# davical-cron and davical-admin for on-demand maintenance/administration
# (run via `docker exec` or directly from the image's overlay dir).
#
# The IANA time zone database is embedded into each binary (import _
# "time/tzdata"), so no /usr/share/zoneinfo is needed. CA certificates are NOT
# bundled: mount the host bundle read-only if outbound TLS verification is
# required, e.g.
#   -v /etc/ssl/certs/ca-certificates.crt:/etc/ssl/certs/ca-certificates.crt:ro
#
# Built multi-arch (linux/amd64, linux/arm64). TARGETOS/TARGETARCH are supplied
# by `docker buildx --platform`.

FROM --platform=$BUILDPLATFORM golang:1.27-trixie AS build

WORKDIR /src

# Cache module downloads separately from the source.
COPY go.mod go.sum ./
RUN go mod download

COPY . .

ARG TARGETOS
ARG TARGETARCH
# Version reported by -version (the release tag without its "v"; set by CI).
ARG VERSION=devel

# CGO_ENABLED=0 -> fully static binaries that run on scratch (and can also be
# executed directly from the host against the image's overlay dir).
# -trimpath and -ldflags "-s -w" drop debug/path metadata to keep the image small;
# -X stamps the version.
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \
    go build -trimpath -ldflags "-s -w -X gitlab.com/davical-project/davical-go/internal/version.Version=${VERSION}" -o /out/davicald     ./cmd/davicald && \
    CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \
    go build -trimpath -ldflags "-s -w -X gitlab.com/davical-project/davical-go/internal/version.Version=${VERSION}" -o /out/davical-cron  ./cmd/davical-cron && \
    CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \
    go build -trimpath -ldflags "-s -w -X gitlab.com/davical-project/davical-go/internal/version.Version=${VERSION}" -o /out/davical-admin ./cmd/davical-admin

FROM scratch

COPY --from=build /out/davicald     /davicald
COPY --from=build /out/davical-cron  /davical-cron
COPY --from=build /out/davical-admin /davical-admin

# Default listen address is :8080 (LISTEN env overrides).
EXPOSE 8080

ENTRYPOINT ["/davicald"]
