# AgentHub ACP Server Makefile
BINARY_NAME=agenthub-acp
VERSION?=0.1.0
BUILD_TIME=$(shell date -u '+%Y-%m-%d_%H:%M:%S')

# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get

# Build directory
BUILD_DIR=build

# Platforms for cross-compilation
PLATFORMS=darwin/amd64 darwin/arm64 linux/amd64 linux/arm64 windows/amd64 windows/arm64

# Default target
all: build

# Build for current platform
build:
	GOCACHE=/tmp/go-cache $(GOBUILD) -o $(BINARY_NAME) -ldflags "-X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME)" .

# Build for all platforms
build-all: clean
	@mkdir -p $(BUILD_DIR)
	@for platform in $(PLATFORMS); do \
		GOOS=$$(echo $$platform | cut -d'/' -f1) ; \
		GOARCH=$$(echo $$platform | cut -d'/' -f2) ; \
		output=$(BUILD_DIR)/$(BINARY_NAME)-$$GOOS-$$GOARCH ; \
		if [ "$$GOOS" = "windows" ]; then output="$$output.exe"; fi ; \
		echo "Building $$GOOS/$$GOARCH..." ; \
		GOCACHE=/tmp/go-cache GOOS=$$GOOS GOARCH=$$GOARCH $(GOBUILD) -o $$output -ldflags "-X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME)" . ; \
	done
	@echo "Build complete. Binaries in $(BUILD_DIR)/"

# Build for specific platform (usage: make build-platform GOOS=linux GOARCH=arm64)
build-platform:
	@output=$(BINARY_NAME)-$(GOOS)-$(GOARCH) ; \
	if [ "$(GOOS)" = "windows" ]; then output="$$output.exe"; fi ; \
	GOCACHE=/tmp/go-cache GOOS=$(GOOS) GOARCH=$(GOARCH) $(GOBUILD) -o $$output -ldflags "-X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME)" .

# Clean build files
clean:
	@rm -f $(BINARY_NAME)
	@rm -f $(BINARY_NAME)-*
	@rm -rf $(BUILD_DIR)
	@echo "Cleaned"

# Install dependencies
deps:
	$(GOGET) ./...

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

# Show help
help:
	@echo "Available targets:"
	@echo "  build          - Build for current platform"
	@echo "  build-all      - Build for all platforms (darwin/linux/windows, amd64/arm64)"
	@echo "  build-platform - Build for specific platform (usage: make build-platform GOOS=linux GOARCH=arm64)"
	@echo "  clean          - Remove build artifacts"
	@echo "  deps           - Download dependencies"
	@echo "  test           - Run tests"
	@echo "  help           - Show this help message"
	@echo ""
	@echo "Available platforms:"
	@echo "  $(PLATFORMS)"

.PHONY: all build build-all build-platform clean deps test help
