#!/bin/sh
set -eu
umask 022
mkdir -p /out/bin /out/records /out/source /cache/build /cache/mod /cache/tmp /cache/npm
go version | tee /out/records/go-version.txt
node --version | tee /out/records/node-version.txt
npm --version | tee /out/records/npm-version.txt
cc --version > /out/records/cc-version.txt
dpkg-query -W > /out/records/builder-packages.tsv
test "$(go env GOVERSION)" = go1.27.2
test "$(node --version)" = v24.16.0
test "$(npm --version)" = 11.13.0
printf '%s  %s\n' "$BEYLA_SHA256" /tmp/beyla | sha256sum -c -
mkdir -p internal/component/beyla/ebpf/binaries/amd64
cp /tmp/beyla internal/component/beyla/ebpf/binaries/amd64/beyla
chmod 0755 internal/component/beyla/ebpf/binaries/amd64/beyla
cp go.mod /out/records/upstream.root.go.mod
cp go.sum /out/records/upstream.root.go.sum
cp collector/go.mod /out/records/upstream.collector.go.mod
cp collector/go.sum /out/records/upstream.collector.go.sum
cp syntax/go.mod /out/records/upstream.syntax.go.mod
cp internal/web/ui/package.json internal/web/ui/package-lock.json /out/records/
(cd internal/web/ui && npm ci --no-audit --no-fund --cache=/cache/npm && npm run build)
# Apply compatible bumps together in both module graphs. Preserve OTel1.46/log0.22.
go get ${GO_BUMPS}
(cd collector && go get ${GO_BUMPS})
tags='gore2regex netgo embedalloyui promtail_journal_enabled'
ldflags="-s -w -X github.com/grafana/alloy/internal/build.Version=$VERSION -X github.com/grafana/alloy/internal/build.Revision=$SOURCE_COMMIT -X github.com/grafana/alloy/internal/build.Branch=hardened -X github.com/grafana/alloy/internal/build.BuildUser=thepeoples.io -X github.com/grafana/alloy/internal/build.BuildDate=$BUILD_DATE -X github.com/grafana/alloy/syntax/internal/stdlib.Version=$VERSION"
# The pinned archive contains generated collector components. Calling the Makefile
# downloader would overwrite the checksum-bound source-built child with a release binary.
(cd collector && go build -p 2 -mod=readonly -buildvcs=false -trimpath -tags "$tags" -ldflags "$ldflags" -o /out/bin/alloy .)
go version -m /out/bin/alloy > /out/records/go-version-m.txt
go mod verify
(cd collector && go mod verify && go list -mod=readonly -m -json all > /out/records/collector-modules.json && go list -mod=readonly -tags "$tags" -deps . > /out/records/packages.txt)
go list -mod=readonly -m -json all > /out/records/root-modules.json
printf '%s\n' "$SOURCE_COMMIT" > /out/records/upstream-commit
printf '%s\n' "$GO_BUMPS" > /out/records/bumps
printf '%s\n' "$tags" > /out/records/build-tags
sha256sum /out/bin/alloy > /out/records/binary.sha256
ldd /out/bin/alloy > /out/records/ldd.txt
cp go.mod /out/records/root.go.mod
cp go.sum /out/records/root.go.sum
cp collector/go.mod /out/records/collector.go.mod
cp collector/go.sum /out/records/collector.go.sum
cp syntax/go.mod /out/records/syntax.go.mod
cp syntax/go.sum /out/records/syntax.go.sum
cp LICENSE /out/records/
if [ -f NOTICE ]; then cp NOTICE /out/records/; fi
cp /tmp/upstream.tar.gz /out/source/upstream.tar.gz
# Preserve downloaded original module ZIP/mod/info inputs without duplicating
# the expanded module cache in image layers. This is not a complete source offer.
tar -czf /out/source/go-module-downloads.tar.gz -C /cache/mod/cache/download .
tar -czf /out/source/npm-downloads.tar.gz -C /cache/npm .
tar -czf /out/source/ui-dist.tar.gz -C internal/web/ui/dist .
cp /tmp/node.tar.xz /out/source/node-sdk.tar.xz
cp -R /out/records /out/source/records
(cd /out/source && sha256sum *.tar.gz *.tar.xz records/* > SHA256SUMS)
