.PHONY: build install test clean version

# Get version from git tag, fallback to dev
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
DATE ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")

# Ldflags to inject version information
LDFLAGS := -ldflags "-X git.membo.co.uk/dtomlinson/gitlocal/cmd.Version=$(VERSION) \
                      -X git.membo.co.uk/dtomlinson/gitlocal/cmd.Commit=$(COMMIT) \
                      -X git.membo.co.uk/dtomlinson/gitlocal/cmd.Date=$(DATE)"

# Build the binary
build:
	@echo "Building gitlocal $(VERSION)..."
	go build $(LDFLAGS) -o gitlocal .

# Install to $GOPATH/bin
install:
	@echo "Installing gitlocal $(VERSION)..."
	go install $(LDFLAGS) .

# Run tests
test:
	@echo "Running tests..."
	go test -v ./...

# Run tests with coverage
coverage:
	@echo "Running tests with coverage..."
	go test -coverprofile=coverage.out ./...
	go tool cover -func=coverage.out

# Clean build artifacts
clean:
	@echo "Cleaning..."
	rm -f gitlocal coverage.out

# Show version that would be built
version:
	@echo "Version: $(VERSION)"
	@echo "Commit:  $(COMMIT)"
	@echo "Date:    $(DATE)"
