# Khimera Portable - Makefile
# Builds portable executables for Windows, Linux, and Android

# Configuration
VERSION := 0.9
BUILD_DATE := $(shell date +%Y%m%d)
OUTPUT_DIR := khimera-portable-v$(VERSION)
LDFLAGS := -s -w -X main.Version=$(VERSION) -X main.BuildDate=$(BUILD_DATE)

# Colors for output
GREEN := \033[0;32m
YELLOW := \033[1;33m
BLUE := \033[0;34m
RED := \033[0;31m
NC := \033[0m

.PHONY: all clean windows linux android portable appimage help test install-deps

# Default target
all: portable

# Help
help:
	@echo "Khimera Portable Build System v$(VERSION)"
	@echo ""
	@echo "Usage:"
	@echo "  make portable     - Build all portable executables (local dev)"
	@echo "  make linux        - Build Linux executable only (local dev)"
	@echo "  make windows      - Build Windows executable only"
	@echo "  make appimage     - Build portable AppImage via Docker (release)"
	@echo "  make android      - Build Android APK only"
	@echo "  make clean        - Clean build artifacts"
	@echo "  make test         - Run tests"
	@echo "  make install-deps - Install build dependencies"
	@echo ""

# Install dependencies
install-deps:
	@echo "$(BLUE)[Setup]$(NC) Installing dependencies..."
	go get fyne.io/fyne/v2
	go get golang.org/x/crypto/scrypt
	go get golang.org/x/crypto/nacl/secretbox
	go get golang.org/x/net/proxy
	go get github.com/flynn/noise
	go mod tidy
	@echo "$(GREEN)✓$(NC) Dependencies installed"

# Build Windows portable
windows: install-deps
	@echo "$(BLUE)[Windows]$(NC) Building Windows portable executable..."
	@mkdir -p $(OUTPUT_DIR)/Windows
	CGO_ENABLED=1 \
	GOOS=windows \
	GOARCH=amd64 \
	CC=x86_64-w64-mingw32-gcc \
	go build \
	    -ldflags="$(LDFLAGS) -H windowsgui" \
	    -tags static \
	    -o $(OUTPUT_DIR)/Windows/khimera.exe \
	    ./khimera-main.go
	@if [ $$? -eq 0 ]; then \
		echo "$(GREEN)✓$(NC) Windows build complete"; \
	else \
		echo "$(RED)✗$(NC) Windows build failed"; \
	fi

# Build Linux portable
linux: install-deps
	@echo "$(BLUE)[Linux]$(NC) Building Linux portable executable..."
	@mkdir -p $(OUTPUT_DIR)/Linux
	CGO_ENABLED=1 \
	GOOS=linux \
	GOARCH=amd64 \
	go build \
	    -ldflags="$(LDFLAGS)" \
	    -o $(OUTPUT_DIR)/Linux/khimera-linux \
	    ./khimera-main.go
	@chmod +x $(OUTPUT_DIR)/Linux/khimera-linux
	@if [ $$? -eq 0 ]; then \
		echo "$(GREEN)✓$(NC) Linux build complete"; \
	else \
		echo "$(RED)✗$(NC) Linux build failed"; \
	fi

# Build AppImage (portable Linux release via Docker)
appimage:
	@echo "$(BLUE)╔════════════════════════════════════════╗$(NC)"
	@echo "$(BLUE)║  KHIMERA APPIMAGE BUILD v$(VERSION)         ║$(NC)"
	@echo "$(BLUE)╚════════════════════════════════════════╝$(NC)"
	@echo ""
	@echo "$(BLUE)[AppImage]$(NC) Building Docker image..."
	docker build --network=host -t khimera-builder -f docker/Dockerfile.appimage .
	@echo "$(BLUE)[AppImage]$(NC) Compiling and packaging..."
	docker run --rm --privileged --network=host \
	    -v $(shell pwd):/build \
	    -w /build \
	    khimera-builder \
	    bash -c " \
	        go mod tidy && \
	        CGO_ENABLED=1 go build \
	            -ldflags='$(LDFLAGS)' \
	            -o /tmp/khimera-bin \
	            ./khimera-main.go && \
	        mkdir -p AppDir/usr/bin AppDir/usr/share/icons/hicolor/256x256/apps && \
	        cp /tmp/khimera-bin AppDir/usr/bin/khimera && \
	        cp appimage/khimera.desktop AppDir/khimera.desktop && \
	        cp appimage/khimera.png AppDir/usr/share/icons/hicolor/256x256/apps/khimera.png && \
	        cp appimage/khimera.png AppDir/khimera.png && \
	        linuxdeploy --appdir AppDir \
	            --library /usr/lib/x86_64-linux-gnu/libGL.so.1 \
	            --library /usr/lib/x86_64-linux-gnu/libX11.so.6 \
	            --output appimage \
	    "
	@mkdir -p $(OUTPUT_DIR)/Linux
	@mv Khimera*.AppImage $(OUTPUT_DIR)/Linux/Khimera-v$(VERSION)-x86_64.AppImage 2>/dev/null || \
	    mv khimera*.AppImage $(OUTPUT_DIR)/Linux/Khimera-v$(VERSION)-x86_64.AppImage
	@rm -rf AppDir
	@echo "$(GREEN)✓ AppImage build complete!$(NC)"
	@echo "Output: $(OUTPUT_DIR)/Linux/Khimera-v$(VERSION)-x86_64.AppImage"

# Build Android APK (placeholder - requires Android SDK)
android:
	@echo "$(YELLOW)[Android]$(NC) Android build not yet implemented"
	@echo "  Use build.sh for Android builds"

# Build all portable executables
portable:
	@echo "$(BLUE)╔════════════════════════════════════════╗$(NC)"
	@echo "$(BLUE)║  KHIMERA PORTABLE BUILD v$(VERSION)         ║$(NC)"
	@echo "$(BLUE)╚════════════════════════════════════════╝$(NC)"
	@echo ""
	@$(MAKE) -s linux
	@echo ""
	@echo "$(GREEN)✓ Build complete!$(NC)"
	@echo "Output: $(OUTPUT_DIR)/"

# Run tests
test:
	@echo "$(BLUE)[Test]$(NC) Running tests..."
	go test -v ./identity/...
	go test -v ./addressbook/...
	go test -v ./session/...
	go test -v ./transport/...
	@echo "$(GREEN)✓$(NC) Tests complete"

# Clean build artifacts
clean:
	@echo "$(YELLOW)[Clean]$(NC) Removing build artifacts..."
	rm -rf $(OUTPUT_DIR)
	rm -rf khimera-portable-v*.zip
	rm -rf khimera-portable-v*.tar.gz
	rm -rf AppDir
	rm -f *.AppImage
	@echo "$(GREEN)✓$(NC) Clean complete"

# Package distribution
package: portable
	@echo "$(YELLOW)[Package]$(NC) Creating distribution archives..."
	@if command -v zip > /dev/null; then \
		zip -r $(OUTPUT_DIR).zip $(OUTPUT_DIR); \
		echo "$(GREEN)✓$(NC) Created $(OUTPUT_DIR).zip"; \
	fi
	@if command -v tar > /dev/null; then \
		tar -czf $(OUTPUT_DIR).tar.gz $(OUTPUT_DIR); \
		echo "$(GREEN)✓$(NC) Created $(OUTPUT_DIR).tar.gz"; \
	fi
