# PrivateBackup Unix CLI Frontend
# Build and development automation

BINARY_NAME := privatebackup
BUILD_DIR := bin
CMD_DIR := ./cmd/privatebackup
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_TIME := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
LDFLAGS := -ldflags "-X main.version=$(VERSION) -X main.buildTime=$(BUILD_TIME)"

# Go settings
GO := go
GOTEST := $(GO) test
GOVET := $(GO) vet
GOFMT := gofmt
GOLINT := golangci-lint

.PHONY: all build build-all install clean test test-cover lint fmt vet deps help completions

all: build

## Build

# Build for current platform
build:
	@mkdir -p $(BUILD_DIR)
	$(GO) build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) $(CMD_DIR)

# Build for all platforms
build-all: build-linux-amd64 build-linux-arm64 build-darwin-amd64 build-darwin-arm64 build-windows-amd64 build-freebsd-amd64

build-linux-amd64:
	@mkdir -p $(BUILD_DIR)
	GOOS=linux GOARCH=amd64 $(GO) build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-amd64 $(CMD_DIR)

build-linux-arm64:
	@mkdir -p $(BUILD_DIR)
	GOOS=linux GOARCH=arm64 $(GO) build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-linux-arm64 $(CMD_DIR)

build-darwin-amd64:
	@mkdir -p $(BUILD_DIR)
	GOOS=darwin GOARCH=amd64 $(GO) build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-amd64 $(CMD_DIR)

build-darwin-arm64:
	@mkdir -p $(BUILD_DIR)
	GOOS=darwin GOARCH=arm64 $(GO) build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-arm64 $(CMD_DIR)

build-freebsd-amd64:
	@mkdir -p $(BUILD_DIR)
	GOOS=freebsd GOARCH=amd64 $(GO) build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-freebsd-amd64 $(CMD_DIR)

build-windows-amd64:
	@mkdir -p $(BUILD_DIR)
	GOOS=windows GOARCH=amd64 $(GO) build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-windows-amd64.exe $(CMD_DIR)

# Install to $GOPATH/bin
install:
	$(GO) install $(LDFLAGS) $(CMD_DIR)

## Testing

# Run all tests
test:
	$(GOTEST) -v ./...

# Run tests with coverage
test-cover:
	$(GOTEST) -v -coverprofile=coverage.out ./...
	$(GO) tool cover -html=coverage.out -o coverage.html
	@echo "Coverage report generated: coverage.html"

# Run tests with race detector
test-race:
	$(GOTEST) -v -race ./...

## Code Quality

# Run linter
lint:
	$(GOLINT) run ./...

# Format code
fmt:
	$(GOFMT) -s -w .

# Check formatting
fmt-check:
	@if [ -n "$$($(GOFMT) -l .)" ]; then \
		echo "Go files need formatting:"; \
		$(GOFMT) -l .; \
		exit 1; \
	fi

# Vet code
vet:
	$(GOVET) ./...

# Run all checks
check: fmt-check vet lint test

## Dependencies

# Download dependencies
deps:
	$(GO) mod download

# Update dependencies
deps-update:
	$(GO) get -u ./...
	$(GO) mod tidy

# Tidy dependencies
tidy:
	$(GO) mod tidy

## Shell Completions

# Generate shell completions
completions: build
	@mkdir -p completions
	$(BUILD_DIR)/$(BINARY_NAME) completion bash > completions/$(BINARY_NAME).bash
	$(BUILD_DIR)/$(BINARY_NAME) completion zsh > completions/_$(BINARY_NAME)
	$(BUILD_DIR)/$(BINARY_NAME) completion fish > completions/$(BINARY_NAME).fish
	@echo "Shell completions generated in completions/"

## Maintenance

# Clean build artifacts
clean:
	rm -rf $(BUILD_DIR)
	rm -f coverage.out coverage.html

# Show help
help:
	@echo "PrivateBackup Unix CLI Frontend"
	@echo ""
	@echo "Usage: make [target]"
	@echo ""
	@echo "Build targets:"
	@echo "  build          Build for current platform"
	@echo "  build-all      Build for all supported platforms"
	@echo "  install        Install to \$$GOPATH/bin"
	@echo ""
	@echo "Test targets:"
	@echo "  test           Run all tests"
	@echo "  test-cover     Run tests with coverage report"
	@echo "  test-race      Run tests with race detector"
	@echo ""
	@echo "Code quality targets:"
	@echo "  lint           Run linter"
	@echo "  fmt            Format code"
	@echo "  fmt-check      Check code formatting"
	@echo "  vet            Run go vet"
	@echo "  check          Run all quality checks"
	@echo ""
	@echo "Other targets:"
	@echo "  deps           Download dependencies"
	@echo "  deps-update    Update dependencies"
	@echo "  tidy           Tidy go.mod"
	@echo "  completions    Generate shell completions"
	@echo "  clean          Remove build artifacts"
	@echo "  help           Show this help"
