ARG GO_VERSION=1.24

FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS builder

WORKDIR /app

COPY go.mod ./
COPY go.sum ./
RUN --mount=type=cache,mode=0777,target=/go/pkg/mod go mod download all

COPY ./plugins ./plugins
COPY ./pkg ./pkg
COPY ./src ./src
COPY ./cmd ./cmd

ARG TARGETOS TARGETARCH
RUN --mount=type=cache,mode=0777,target=/go/pkg/mod \
    GOOS=$TARGETOS GOARCH=$TARGETARCH \
    go build -tags timetzdata \
    -o /tstor ./cmd/tstor/main.go 

FROM scratch

COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /tstor /tstor

ENTRYPOINT ["/tstor"]