# Convenience only. `go build`, `go test` and ./scripts/verify.sh are the ground truth — nothing here
# is required to work on khosra, and the gate never invokes make.

SITE ?= $(KHOSRA_SITE)
ADDR ?= localhost:8080
BIN  := ./khosra

.PHONY: help build run check new demo test verify quiet surface fmt tidy clean

help: ## list targets
	@grep -hE '^[a-z]+:.*##' $(MAKEFILE_LIST) | sed 's/:[^#]*## /|/' | column -t -s '|'

build: ## compile the binary
	go build -o $(BIN) ./cmd/khosra

run: build ## serve a site root: make run SITE=/path/to/site
	@test -n "$(SITE)" || { echo "set SITE=/path/to/site, or export KHOSRA_SITE"; exit 1; }
	$(BIN) -site "$(SITE)" -addr "$(ADDR)"

check: build ## validate a site root's content: make check SITE=/path/to/site
	@test -n "$(SITE)" || { echo "set SITE=/path/to/site, or export KHOSRA_SITE"; exit 1; }
	$(BIN) check -site "$(SITE)"

new: build ## scaffold a bundle: make new KEY=posts/hello-world SITE=/path/to/site
	@test -n "$(SITE)" || { echo "set SITE=/path/to/site, or export KHOSRA_SITE"; exit 1; }
	@test -n "$(KEY)" || { echo "set KEY=posts/hello-world"; exit 1; }
	$(BIN) new -site "$(SITE)" "$(KEY)"

demo: build ## serve the demo site in examples/, which exercises every feature
	$(BIN) -site examples/demo-site -addr "$(ADDR)"

test: ## run tests with the race detector
	go test -race ./...

verify: ## everything the harness enforces; must be green before a commit
	./scripts/verify.sh

quiet: ## the same gates, printing only what needs acting on
	./scripts/verify.sh --quiet

surface: ## regenerate harness/surface.md, the compressed map of the code
	./scripts/surface.sh --write

fmt: ## format all Go source
	gofmt -w .

tidy: ## tidy go.mod
	go mod tidy

clean: ## remove build output
	rm -f $(BIN)
