export GOTOOLCHAIN := go1.26.5
export GOPRIVATE := $(GOPRIVATE),amadan.net/oficina/estilo.git

APP := alta

# What `carlos ship -target` defaults to, and so what a release must be
# built for. Override for a one-off: make release RELEASE_GOARCH=amd64
RELEASE_GOOS   ?= linux
RELEASE_GOARCH ?= arm64
RELEASE_BIN := releases/$(APP)-$(RELEASE_GOOS)-$(RELEASE_GOARCH)

# What GET /api/version answers. An unstamped binary answers "dev"
# forever, and the deploy check can then never fail.
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null)

.PHONY: build release adopt version-check deps test vet fmt-check compat ci check compat-check

build:
	CGO_ENABLED=0 go build ./...

release: version-check
	@mkdir -p releases
	CGO_ENABLED=0 GOOS=$(RELEASE_GOOS) GOARCH=$(RELEASE_GOARCH) go build -trimpath \
		-ldflags "-s -w -X amadan.net/rastrillo/rastrillo.BuildVersion=$(VERSION)" \
		-o $(RELEASE_BIN) ./cmd/$(APP)
	@echo "built $(RELEASE_BIN) reporting version $(VERSION)"

# adopt builds the local adoption runner for this machine (it is run here,
# never shipped), stamped like a release: alta-adopt version prints it.
ADOPT_BIN := releases/alta-adopt
adopt: version-check
	@mkdir -p releases
	CGO_ENABLED=0 go build -trimpath \
		-ldflags "-s -w -X main.version=$(VERSION)" \
		-o $(ADOPT_BIN) ./cmd/alta-adopt
	@echo "built $(ADOPT_BIN) reporting version $(VERSION)"

version-check:
	@test -n "$(VERSION)" || { echo "make release: no version to stamp; commit, or name it: make release VERSION=v0.1.0"; exit 1; }
	@case "$(VERSION)" in *-dirty) echo "make release: the tree is dirty, so $(VERSION) does not name what you would ship"; exit 1;; esac

# deps repairs a module cache whose extracted copy lost files (go mod
# verify names it), the CI runner's known failure (memoria's Makefile).
deps:
	@go mod download
	@out=$$(go mod verify 2>&1) || { \
		echo "$$out"; \
		echo "$$out" | sed -n 's/.*: dir has been modified (\(.*\))$$/\1/p' | while read -r dir; do \
			echo "deps: re-extracting $$dir"; chmod -R u+w "$$dir" && rm -rf "$$dir"; \
		done; \
		go mod download && go mod verify; \
	}

test:
	CGO_ENABLED=0 go test ./...

vet: deps
	go vet ./...

fmt-check:
	@out=$$(gofmt -l .); if [ -n "$$out" ]; then echo "gofmt needed:"; echo "$$out"; exit 1; fi

# compat runs Alta's engine against Home's real operator code at the
# pinned commit (homecompat/go.mod). Its own module, so Alta's build
# never depends on Home's internals.
compat:
	cd homecompat && go vet ./... && CGO_ENABLED=0 go test ./...

# check runs one package's tests: make check PKG=./homeop/... RUN=TestX
# PKG is required. Use this, never bare `go test`: it pins the toolchain.
check:
	@test -n "$(PKG)" || { echo "make check: PKG is required, e.g. make check PKG=./provision/... RUN=TestX"; exit 1; }
	CGO_ENABLED=0 go test $(PKG) -run '$(RUN)' -count=1

# compat-check is check for homecompat, which is its own module.
compat-check:
	cd homecompat && CGO_ENABLED=0 go test ./... -run '$(RUN)' -count=1

# ci is the one gate: what the runner executes and what you run before
# pushing. No migration-check: Alta has no GORM models, only SQL
# migrations under provision/migrations.
ci: vet fmt-check test compat
