# GoatCounter v2.7.0 with a current Go toolchain and reviewed dependency updates.
FROM golang:1.26.8-alpine3.24@sha256:ce864e7223ac17b1775e6fd0b4c0db580c2eb50e7953a427916379e4b92a1628 AS build
ENV GOTOOLCHAIN=local CGO_ENABLED=1 GOMAXPROCS=2
RUN apk add --no-cache build-base ca-certificates curl tzdata
WORKDIR /src
RUN curl -fLsS --retry 3 https://codeload.github.com/arp242/goatcounter/tar.gz/7e91d8a9bdbb0dd48496e498c5680f8f3477a1b4 -o /tmp/goatcounter-source.tar.gz \
    && echo 'b7a8a084810afb7249b82c3e3d34d8b30c430079402b723b00b384a5b1de3e3d  /tmp/goatcounter-source.tar.gz' | sha256sum -c - \
    && tar -xzf /tmp/goatcounter-source.tar.gz --strip-components=1 -C /src \
    && rm /tmp/goatcounter-source.tar.gz
COPY go.mod go.sum ./
RUN go mod download && go mod verify && go mod vendor
# Keep the upstream SQLite suite in the image build; no live database is mounted.
RUN CI=1 go test -mod=vendor -p 2 -timeout=3m ./...
RUN mkdir -p /out/data \
    && go build -mod=vendor -p 2 -trimpath \
       -tags='osusergo,netgo,sqlite_omit_load_extension' \
       -ldflags='-s -w -linkmode external -extldflags "-static" -X zgo.at/goatcounter/v2.Version=v2.7.0-gamearrivals.1' \
       -o /out/goatcounter ./cmd/goatcounter \
    && /out/goatcounter version \
    && go version -m /out/goatcounter > /out/build-info.txt \
    && printf 'goatcounter:x:10001:10001:GoatCounter:/data:/sbin/nologin\n' > /out/passwd \
    && printf 'goatcounter:x:10001:\n' > /out/group \
    && chmod 0555 /out/goatcounter
# Bundle the complete modified source and vendored dependencies, including licenses.
COPY Dockerfile README.md dependencies.patch ./gamearrivals-build/
COPY licenses ./gamearrivals-build/licenses
RUN cp /usr/local/go/LICENSE /src/gamearrivals-build/licenses/Go-LICENSE \
    && tar -czf /out/source.tar.gz -C /src . \
    && apk info -v > /out/toolchain-packages.txt

FROM scratch
LABEL org.opencontainers.image.title="GoatCounter — GameArrivals dependency rebuild" \
      org.opencontainers.image.version="2.7.0-gamearrivals.1" \
      org.opencontainers.image.source="https://github.com/arp242/goatcounter" \
      org.opencontainers.image.revision="7e91d8a9bdbb0dd48496e498c5680f8f3477a1b4" \
      org.opencontainers.image.licenses="LicenseRef-GoatCounter-Modified-EUPL-1.2"
COPY --from=build /out/goatcounter /usr/local/bin/goatcounter
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=build /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=build /out/passwd /etc/passwd
COPY --from=build /out/group /etc/group
COPY --from=build /src/LICENSE /usr/share/goatcounter/LICENSE
COPY --from=build /src/gamearrivals-build/licenses /usr/share/goatcounter/licenses
COPY --from=build /out/build-info.txt /usr/share/goatcounter/build-info.txt
COPY --from=build /out/toolchain-packages.txt /usr/share/goatcounter/toolchain-packages.txt
COPY --from=build /out/source.tar.gz /usr/share/goatcounter/source.tar.gz
COPY --from=build --chown=10001:10001 /out/data /data
USER 10001:10001
WORKDIR /data
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/goatcounter"]
