ARG GO_BUILDER
ARG MC_IMAGE

FROM ${GO_BUILDER} AS pins
ARG UPSTREAM_RELEASE
ARG UPSTREAM_COMMIT
ARG MODULE
ARG MODULE_VERSION
ARG MODULE_SUM
ENV GOTOOLCHAIN=local GOFLAGS=-mod=readonly CGO_ENABLED=0 GOOS=linux GOARCH=amd64
COPY src /src
WORKDIR /src
RUN set -eu; \
    test "$(git rev-parse HEAD)" = "${UPSTREAM_COMMIT}" || { echo "REFUSED pins: src HEAD is not UPSTREAM_COMMIT"; exit 1; }; \
    test -z "$(git status --porcelain)" || { echo "REFUSED pins: src working tree is not clean"; git status --porcelain; exit 1; }; \
    test "$(go list -m)" = "${MODULE}" || { echo "REFUSED pins: src module is not MODULE"; exit 1; }; \
    scratch="$(mktemp -d)"; \
    (cd "${scratch}" && env -u GOFLAGS go mod download -json "${MODULE}@${MODULE_VERSION}" > "${scratch}/module.json"); \
    sum="$(sed -n 's/^[[:space:]]*"Sum": "\([^"]*\)".*/\1/p' "${scratch}/module.json")"; \
    hash="$(sed -n 's/^[[:space:]]*"Hash": "\([^"]*\)".*/\1/p' "${scratch}/module.json")"; \
    test "${sum}" = "${MODULE_SUM}" || { echo "REFUSED pins: module sum is ${sum}, PINS says MODULE_SUM=${MODULE_SUM}"; exit 1; }; \
    test "${hash}" = "${UPSTREAM_COMMIT}" || { echo "REFUSED pins: module origin hash is ${hash}, PINS says UPSTREAM_COMMIT=${UPSTREAM_COMMIT}"; exit 1; }; \
    go mod download; \
    go mod verify

FROM pins AS build
ARG UPSTREAM_RELEASE
ARG UPSTREAM_COMMIT
RUN set -eu; \
    LDFLAGS="$(MINIO_RELEASE=RELEASE go run buildscripts/gen-ldflags.go "${UPSTREAM_RELEASE#RELEASE.}")"; \
    case "${LDFLAGS}" in *"cmd.ReleaseTag=${UPSTREAM_RELEASE} "*) ;; *) echo "REFUSED build: ldflags lack ReleaseTag=${UPSTREAM_RELEASE}"; exit 1;; esac; \
    case "${LDFLAGS}" in *"cmd.CommitID=${UPSTREAM_COMMIT} "*) ;; *) echo "REFUSED build: ldflags lack CommitID=${UPSTREAM_COMMIT}"; exit 1;; esac; \
    mkdir -p /out; \
    go build -trimpath -tags kqueue -ldflags "${LDFLAGS}" -o /out/minio .; \
    go version -m /out/minio > /out/buildinfo.txt; \
    grep -Eq "^[[:space:]]+build[[:space:]]+vcs.revision=${UPSTREAM_COMMIT}$" /out/buildinfo.txt || { echo "REFUSED build: vcs.revision is not UPSTREAM_COMMIT"; exit 1; }; \
    grep -Eq "^[[:space:]]+build[[:space:]]+vcs.modified=false$" /out/buildinfo.txt || { echo "REFUSED build: vcs.modified is not false"; exit 1; }; \
    grep -Eq "^[[:space:]]+build[[:space:]]+-trimpath=true$" /out/buildinfo.txt || { echo "REFUSED build: -trimpath is not true"; exit 1; }; \
    grep -Eq "^[[:space:]]+build[[:space:]]+CGO_ENABLED=0$" /out/buildinfo.txt || { echo "REFUSED build: CGO_ENABLED is not 0"; exit 1; }; \
    grep -Eq "^[[:space:]]+build[[:space:]]+-tags=kqueue$" /out/buildinfo.txt || { echo "REFUSED build: -tags is not kqueue"; exit 1; }; \
    cat /out/buildinfo.txt; \
    sha256sum /out/minio | cut -d' ' -f1 > /out/minio.sha256; \
    mkdir -p /rootfs/tmp /rootfs/etc; \
    chmod 1777 /rootfs/tmp; \
    printf 'root:x:0:0:root:/root:/sbin/nologin\n' > /rootfs/etc/passwd; \
    printf 'root:x:0:\n' > /rootfs/etc/group

FROM ${MC_IMAGE} AS mc

FROM scratch
ARG UPSTREAM_RELEASE
ARG UPSTREAM_COMMIT
ARG RECIPE_COMMIT
ARG BINARY_SHA256
LABEL org.opencontainers.image.source="https://git.vezha.io/para/minio" \
      org.opencontainers.image.revision="${UPSTREAM_COMMIT}" \
      org.opencontainers.image.version="${UPSTREAM_RELEASE}" \
      org.opencontainers.image.licenses="AGPL-3.0" \
      io.vezha.recipe="${RECIPE_COMMIT}" \
      io.vezha.binary.sha256="${BINARY_SHA256}"
ENV PATH=/usr/bin MC_CONFIG_DIR=/tmp/.mc
COPY --from=build /rootfs/ /
COPY --from=build /out/minio /usr/bin/minio
COPY --from=mc /usr/bin/mc /usr/bin/mc
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=mc /licenses/ /licenses/mc/
COPY src/LICENSE /licenses/LICENSE
COPY src/CREDITS /licenses/CREDITS
COPY vezha/SOURCE /licenses/SOURCE
EXPOSE 9000
VOLUME ["/data"]
ENTRYPOINT ["/usr/bin/minio"]
CMD ["server", "/data"]
HEALTHCHECK --interval=5s --timeout=5s --retries=10 CMD ["/usr/bin/mc", "ready", "local"]
