# The gate. Green is the definition of done (novox/hq how-we-build §5).
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo development)
LDFLAGS := -s -w -X main.version=$(VERSION)

# The bundle a host carries is built INTO it (novox/hq ADR 0038, ADR 0041): a host that needed
# a second file to arrive with it is not "copy it and run it".
BUNDLE ?=

.PHONY: check test vet fmt build clean host

check: fmt vet test build

fmt:
	@test -z "$$(gofmt -l . )" || { echo "unformatted:"; gofmt -l . ; exit 1; }

vet:
	go vet ./...

# Structure and logic, and the same checks against this machine. The boundary is never mocked.
test:
	go test ./... -count=1

# A default build carries no bundle and refuses to reconcile, which is the honest state for a
# host nobody has told what a substrate is.
build:
	CGO_ENABLED=0 go build -ldflags="$(LDFLAGS)" -o mesh-host ./cmd/mesh-host

# A host for a real machine, carrying a real bundle:  make host BUNDLE=path/to/substrate.lock
host:
	@test -n "$(BUNDLE)" || { echo "BUNDLE= is required; a host with no bundle cannot raise a first node"; exit 1; }
	@test -f "$(BUNDLE)" || { echo "no such bundle: $(BUNDLE)"; exit 1; }
	@cp internal/bundle/substrate.lock internal/bundle/substrate.lock.default
	@cp "$(BUNDLE)" internal/bundle/substrate.lock
	@CGO_ENABLED=0 go build -ldflags="$(LDFLAGS)" -o mesh-host ./cmd/mesh-host; \
	  status=$$?; \
	  mv internal/bundle/substrate.lock.default internal/bundle/substrate.lock; \
	  exit $$status
	@echo "built carrying $(BUNDLE)"

clean:
	rm -f mesh-host
