# Muse Cross-Platform Build
APP_NAME=muse
VERSION?=dev
BUILD_DIR=.build
MAIN_PATH=./cmd/muse

# Go build flags
LDFLAGS=-ldflags "-s -w -X main.version=$(VERSION)"

# Platform and architecture combinations
PLATFORMS=\
	darwin/amd64 \
	darwin/arm64 \
	linux/amd64 \
	linux/arm64 \
	windows/amd64 \
	windows/arm64

.PHONY: build release clean help $(PLATFORMS)

# Default target - build all binaries
build: $(PLATFORMS)

# Build for specific platform/architecture
$(PLATFORMS):
	$(eval GOOS=$(word 1,$(subst /, ,$@)))
	$(eval GOARCH=$(word 2,$(subst /, ,$@)))
	$(eval EXT=$(if $(filter windows,$(GOOS)),.exe,))
	@echo "Building $(APP_NAME) for $(GOOS)/$(GOARCH)..."
	@mkdir -p $(BUILD_DIR)/$(GOOS)-$(GOARCH)
	GOOS=$(GOOS) GOARCH=$(GOARCH) go build $(LDFLAGS) -o $(BUILD_DIR)/$(GOOS)-$(GOARCH)/$(APP_NAME)$(EXT) $(MAIN_PATH)

# Create release archives (builds first if needed)
release: build
	@echo "Creating release archives..."
	@for platform in $(PLATFORMS); do \
		GOOS=$$(echo $$platform | cut -d'/' -f1); \
		GOARCH=$$(echo $$platform | cut -d'/' -f2); \
		EXT=""; \
		if [ "$$GOOS" = "windows" ]; then EXT=".exe"; fi; \
		ARCHIVE_NAME="$(APP_NAME)-$(VERSION)-$$GOOS-$$GOARCH"; \
		if [ "$$GOOS" = "windows" ]; then \
			cd $(BUILD_DIR)/$$GOOS-$$GOARCH && zip -q ../$$ARCHIVE_NAME.zip $(APP_NAME)$$EXT && cd ../..; \
		else \
			tar -czf $(BUILD_DIR)/$$ARCHIVE_NAME.tar.gz -C $(BUILD_DIR)/$$GOOS-$$GOARCH $(APP_NAME)$$EXT; \
		fi; \
		echo "Created $$ARCHIVE_NAME archive"; \
	done

# Clean build directory
clean:
	@rm -rf $(BUILD_DIR)

# Help target
help:
	@echo "Muse Build Commands"
	@echo ""
	@echo "Available targets:"
	@echo "  build      - Build binaries for all platforms (default)"
	@echo "  release    - Build binaries and create release archives"
	@echo "  clean      - Remove build directory"
	@echo "  help       - Show this help message"
	@echo ""
	@echo "Supported platforms:"
	@for platform in $(PLATFORMS); do echo "  $$platform"; done
	@echo ""
	@echo "Examples:"
	@echo "  make                    # Build all binaries"
	@echo "  make release VERSION=1.0.0  # Create release archives"
	@echo "  make clean              # Clean build directory"
