.PHONY: build snapshot release completions clean help

GO ?= go
BIN := archduino

help: ## Show this help
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "  %-14s %s\n", $$1, $$2}' $(MAKEFILE_LIST)

build: ## Local dev build into ./archduino
	$(GO) build -o $(BIN) ./cmd/archduino

completions: ## Regenerate shell completions into completions/
	@mkdir -p completions
	$(GO) run ./cmd/archduino completion bash > completions/archduino.bash
	$(GO) run ./cmd/archduino completion zsh  > completions/_archduino
	$(GO) run ./cmd/archduino completion fish > completions/archduino.fish

snapshot: ## Local goreleaser dry run into dist/
	goreleaser release --snapshot --clean

clean: ## Remove build artifacts
	rm -rf dist/ completions/ $(BIN)

release: ## Compute next SemVer from commits, tag, and push
	@command -v svu >/dev/null || { echo "svu not found. Run: go install github.com/caarlos0/svu@latest"; exit 1; }
	@test -z "$$(git status --porcelain)" || { echo "working tree is dirty; commit or stash first"; exit 1; }
	@branch=$$(git rev-parse --abbrev-ref HEAD); \
	 if [ "$$branch" != "master" ]; then echo "must be on master (currently on $$branch)"; exit 1; fi
	@current=$$(svu current 2>/dev/null || echo "none"); \
	 next=$$(svu next); \
	 echo "Current: $$current"; \
	 echo "Next:    $$next"; \
	 echo; \
	 echo "Commits since $$current:"; \
	 if [ "$$current" = "none" ]; then git log --oneline; else git log --oneline $$current..HEAD; fi; \
	 echo; \
	 printf "Tag %s and push? [y/N] " "$$next"; read ans; \
	 if [ "$$ans" != "y" ] && [ "$$ans" != "Y" ]; then echo "aborted"; exit 1; fi; \
	 git tag "$$next" && git push origin "$$next"
