SHA := $(shell git rev-parse --short HEAD 2>/dev/null || echo dev)
LDFLAGS := -s -w -X github.com/carlosframework/platform/internal/version.Build=$(SHA)
GOOS ?=
GOARCH ?=

.PHONY: test build fmt census-card census-propose

# The build gate: vet, format, tests. `.amadan/ci.d/10-gate` runs this same
# target, so what CI checks and what you check are one definition.
#
# -count=1 is CLAUDE.md's rule: never satisfy the gate from a cached pass.
# The 30m timeout is above Go's 10m default because the root package's e2e
# tests compile the carlos binary many times over.
#
# .amadan/test-gate.sh runs launchctx, vet, gofmt and the tests at once,
# and splits the slowest packages' tests across processes. TEST_SPLIT
# overrides which packages and how many ways (TEST_SPLIT= runs none split).
test:
	.amadan/test-gate.sh

build:
	CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -o carlos .

fmt:
	gofmt -w .

# The root-read census as an approval (#369, AGENTS.md "The census"):
# census-card prints what changed and writes nothing; census-propose writes
# the overlay, its chain manifest line and the approval card. CLAIMS has
# one line per changed unit, "<file>:<unit><TAB><what changed>". Kept here
# rather than in a script: every .sh in the tree is itself a census unit.
CENSUS_RUN = out=$$(mktemp "$${TMPDIR:-/tmp}/census.XXXXXX") || exit 1; \
	trap 'rm -f "$$out"' EXIT; \
	GOFLAGS="$${GOFLAGS:--buildvcs=false}" go test ./internal/provision/ -run '^TestPlan14RootReadCensusPropose$$' -count=1 -v >"$$out" 2>&1; rc=$$?; \
	if [ $$rc -eq 0 ]; then sed -n '/^--- census card ---$$/,/^--- end census card ---$$/p' "$$out" | sed '1d;$$d'; \
	else grep -v '^20[0-9][0-9]/' "$$out" | tail -20 >&2; fi; exit $$rc
# CLAIMS is kept as one value, spaces and all, and made absolute for go test.
CENSUS_CLAIMS = c='$(CLAIMS)'; if [ -n "$$c" ]; then case "$$c" in /*) ;; *) c="$$PWD/$$c" ;; esac; \
	CARLOS_CENSUS_CLAIMS="$$c"; export CARLOS_CENSUS_CLAIMS; fi

census-card:
	@CARLOS_CENSUS_CARD=1; export CARLOS_CENSUS_CARD; $(CENSUS_CLAIMS); $(CENSUS_RUN)

census-propose:
	@if [ -z '$(NAME)' ] || [ -z '$(CLAIMS)' ]; then echo "usage: make census-propose NAME=<branch> CLAIMS=<file>" >&2; exit 2; fi; \
	CARLOS_CENSUS_PROPOSE='$(NAME)'; export CARLOS_CENSUS_PROPOSE; $(CENSUS_CLAIMS); $(CENSUS_RUN)
