# ADBKit agent build. Requires Go 1.25+. The version comes from the VERSION file
# (override with `make release VERSION=...`).
VERSION ?= $(shell cat VERSION)
LDFLAGS := -s -w -X main.version=$(VERSION)
DIST    := dist
TARGETS := darwin/amd64 darwin/arm64 linux/amd64 linux/arm64 windows/amd64 windows/arm64

.PHONY: build test vet dist release clean

build:
	CGO_ENABLED=0 go build -trimpath -ldflags "$(LDFLAGS)" -o $(DIST)/adbkit-agent .

vet:
	go vet ./...

test:
	go test -race -count=1 ./...

# Cross-compile every release target into dist/adbkit-agent_<os>_<arch>[.exe]
# and write dist/checksums.txt (sha256).
release: vet test dist

dist:
	rm -rf $(DIST) && mkdir -p $(DIST)
	@for t in $(TARGETS); do \
		os=$${t%/*}; arch=$${t#*/}; ext=""; [ "$$os" = windows ] && ext=".exe"; \
		out="$(DIST)/adbkit-agent_$${os}_$${arch}$$ext"; \
		echo "building $$out"; \
		CGO_ENABLED=0 GOOS=$$os GOARCH=$$arch go build -trimpath -ldflags "$(LDFLAGS)" -o $$out . || exit 1; \
	done
	cd $(DIST) && (shasum -a 256 adbkit-agent_* > checksums.txt 2>/dev/null || sha256sum adbkit-agent_* > checksums.txt)
	@cat $(DIST)/checksums.txt

clean:
	rm -rf $(DIST)
