VERSION ?= 0.1.0
LDFLAGS := -s -w -X main.version=$(VERSION)
GO      ?= go

# Release targets: macOS Apple Silicon + Intel, Linux amd64 + arm64, Windows.
PLATFORMS := darwin/arm64 darwin/amd64 linux/amd64 linux/arm64 windows/amd64

.PHONY: build install test vet run dist clean

build: ## Build a native binary into ./bin/dial
	$(GO) build -trimpath -ldflags "$(LDFLAGS)" -o bin/dial ./cmd/dial

install: ## Install dial into $GOBIN / $GOPATH/bin
	$(GO) install -trimpath -ldflags "$(LDFLAGS)" ./cmd/dial

test: ## Run tests
	$(GO) test ./...

vet: ## Run go vet
	$(GO) vet ./...

run: build ## Build and run
	./bin/dial

dist: ## Cross-compile all release binaries into ./dist
	@mkdir -p dist
	@for p in $(PLATFORMS); do \
		os=$${p%/*}; arch=$${p#*/}; ext=""; \
		[ "$$os" = "windows" ] && ext=".exe"; \
		echo "building dial-$$os-$$arch$$ext"; \
		GOOS=$$os GOARCH=$$arch $(GO) build -trimpath -ldflags "$(LDFLAGS)" \
			-o dist/dial-$$os-$$arch$$ext ./cmd/dial || exit 1; \
	done

clean: ## Remove build artifacts
	rm -rf bin dist
