# clip-sync Makefile
# Cross-compilation, tests, release.

BINARY    := clip-sync
VERSION   := $(shell git describe --tags --always --dirty 2>/dev/null || echo "0.2.0")
LDFLAGS   := -s -w -X main.version=$(VERSION)
BUILD_DIR := bin

GOOSES    := linux windows darwin
GOARCHES  := amd64 arm64

.DEFAULT_GOAL := build

# ── Build ──────────────────────────────────────────────────────────────

.PHONY: build
build:
	@mkdir -p $(BUILD_DIR)
	CGO_ENABLED=0 go build -ldflags "$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY) .

# ── Cross-compile ──────────────────────────────────────────────────────

.PHONY: release
release:
	@mkdir -p $(BUILD_DIR)
	@for os in $(GOOSES); do \
		for arch in $(GOARCHES); do \
			echo "Building $$os/$$arch..."; \
			GOOS=$$os GOARCH=$$arch CGO_ENABLED=0 go build \
				-ldflags "$(LDFLAGS)" \
				-o $(BUILD_DIR)/$(BINARY)-$$os-$$arch$(if $(filter windows,$$os),.exe,) \
				.; \
		done; \
	done
	@echo "Release builds in $(BUILD_DIR)/:"
	@ls -lh $(BUILD_DIR)

# ── Test ───────────────────────────────────────────────────────────────

.PHONY: test
test:
	go test ./... -count=1 -timeout 60s

.PHONY: test-verbose
test-verbose:
	go test ./... -v -count=1 -timeout 60s

.PHONY: test-race
test-race:
	go test ./... -race -count=1 -timeout 120s

.PHONY: test-cover
test-cover:
	go test ./... -coverprofile=coverage.out -count=1 -timeout 60s
	go tool cover -func=coverage.out

# ── Benchmark ─────────────────────────────────────────────────────────

.PHONY: bench
bench:
	go test ./... -bench=. -benchmem -run=^$$ -count=1

# ── Release signing ───────────────────────────────────────────────────

.PHONY: sign
sign:
	@for f in $(BUILD_DIR)/clip-sync-*; do \
		echo "Signing $$f"; \
		gpg --batch --yes --armor --detach-sign "$$f"; \
	done
	@echo "Signed binaries in $(BUILD_DIR)/ (alongside .asc signatures)"

# ── Lint ───────────────────────────────────────────────────────────────

.PHONY: lint
lint:
	golangci-lint run ./...

# ── Clean ──────────────────────────────────────────────────────────────

.PHONY: clean
clean:
	rm -rf $(BUILD_DIR) coverage.out

# ── Install (local dev) ────────────────────────────────────────────────

.PHONY: install
install: build
	install -m 755 $(BUILD_DIR)/$(BINARY) $(HOME)/.local/bin/$(BINARY)

# ── Help ────────────────────────────────────────────────────────────────

.PHONY: help
help:
	@echo "clip-sync Makefile"
	@echo ""
	@echo "  build          Build for current platform"
	@echo "  release        Cross-compile all platforms (linux/windows/darwin, amd64/arm64)"
	@echo "  test           Run all tests"
	@echo "  test-verbose   Run tests with verbose output"
	@echo "  test-race      Run tests with race detector"
	@echo "  test-cover     Run tests with coverage report"
	@echo "  bench          Run benchmarks"
	@echo "  sign           GPG-sign release binaries in bin/"
	@echo "  lint           Run golangci-lint"
	@echo "  clean          Remove build artifacts"
	@echo "  install        Install binary to ~/.local/bin/"
	@echo "  help           Show this help"
