# Makefile for Prole Database Installer
# Creates a self-contained universal binary for macOS

# Variables
APP_NAME = Prole Installer
BINARY_NAME = prole-installer
PYTHON = python3
PYINSTALLER = pyinstaller
ICON_FILE = img/proleIcon.png
ICON_ICNS = build/prole.icns
DIST_DIR = dist
BUILD_DIR = build
SPEC_FILE = installer.spec
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")

# Detect architecture
ARCH := $(shell uname -m)
ifeq ($(ARCH),arm64)
	TARGET_ARCH = arm64
else
	TARGET_ARCH = x86_64
endif

.PHONY: all build clean package install test help spec

# Default target
.DEFAULT_GOAL := package

all: package

help:
	@echo "Prole Database Installer Build System"
	@echo ""
	@echo "Targets:"
	@echo "  build       - Build the installer binary with PyInstaller"
	@echo "  clean       - Remove build artifacts"
	@echo "  package     - Create universal macOS app bundle (.app)"
	@echo "  install     - Install dependencies required for building"
	@echo "  test        - Test the built binary"
	@echo "  help        - Show this help message"
	@echo ""
	@echo "Usage:"
	@echo "  make build    # Build static binary"
	@echo "  make package  # Create .app bundle"
	@echo "  make clean    # Clean build artifacts"

# Install build dependencies
install:
	@echo "Installing build dependencies..."
	$(PYTHON) -m pip install --upgrade pip
	$(PYTHON) -m pip install pyinstaller pillow
	@echo "✓ Dependencies installed"

# Convert PNG icon to macOS .icns format
$(ICON_ICNS): $(ICON_FILE)
	@echo "Converting icon to .icns format..."
	@mkdir -p $(BUILD_DIR)
	@mkdir -p $(BUILD_DIR)/icon.iconset
	@sips -z 16 16     $(ICON_FILE) --out $(BUILD_DIR)/icon.iconset/icon_16x16.png > /dev/null 2>&1
	@sips -z 32 32     $(ICON_FILE) --out $(BUILD_DIR)/icon.iconset/icon_16x16@2x.png > /dev/null 2>&1
	@sips -z 32 32     $(ICON_FILE) --out $(BUILD_DIR)/icon.iconset/icon_32x32.png > /dev/null 2>&1
	@sips -z 64 64     $(ICON_FILE) --out $(BUILD_DIR)/icon.iconset/icon_32x32@2x.png > /dev/null 2>&1
	@sips -z 128 128   $(ICON_FILE) --out $(BUILD_DIR)/icon.iconset/icon_128x128.png > /dev/null 2>&1
	@sips -z 256 256   $(ICON_FILE) --out $(BUILD_DIR)/icon.iconset/icon_128x128@2x.png > /dev/null 2>&1
	@sips -z 256 256   $(ICON_FILE) --out $(BUILD_DIR)/icon.iconset/icon_256x256.png > /dev/null 2>&1
	@sips -z 512 512   $(ICON_FILE) --out $(BUILD_DIR)/icon.iconset/icon_256x256@2x.png > /dev/null 2>&1
	@sips -z 512 512   $(ICON_FILE) --out $(BUILD_DIR)/icon.iconset/icon_512x512.png > /dev/null 2>&1
	@sips -z 1024 1024 $(ICON_FILE) --out $(BUILD_DIR)/icon.iconset/icon_512x512@2x.png > /dev/null 2>&1
	@iconutil -c icns $(BUILD_DIR)/icon.iconset -o $(ICON_ICNS)
	@rm -rf $(BUILD_DIR)/icon.iconset
	@echo "✓ Icon created: $(ICON_ICNS)"

# Generate PyInstaller spec file
spec: $(ICON_ICNS)
	@echo "Generating PyInstaller spec file..."
	@$(PYTHON) scripts/generate_spec.py
	@echo "✓ Spec file created: $(SPEC_FILE)"

# Build the binary
build: spec
	@echo "Building installer binary..."
	@echo "Architecture: $(TARGET_ARCH)"
	@echo "Version: $(VERSION)"
	$(PYINSTALLER) --clean --noconfirm $(SPEC_FILE)
	@echo "✓ Build complete: $(DIST_DIR)/prole-installer"
	@echo "✓ App bundle: $(DIST_DIR)/Prole Installer.app"

# Create universal binary package (macOS .app bundle)
package: build
	@echo "Creating universal package..."
	@if [ -d "$(DIST_DIR)/Prole Installer.app" ]; then \
		echo "✓ macOS app bundle created: $(DIST_DIR)/Prole Installer.app"; \
		echo ""; \
		echo "Usage:"; \
		echo "  Double-click: Launch GUI installer"; \
		echo "  Terminal:     ./dist/Prole\\ Installer.app/Contents/MacOS/prole-installer --no-gui"; \
		echo ""; \
		echo "To install:"; \
		echo "  cp -r '$(DIST_DIR)/Prole Installer.app' /Applications/"; \
	else \
		echo "✗ App bundle creation failed"; \
		exit 1; \
	fi

# Test the built binary
test: build
	@echo "Testing binary..."
	@echo "1. Testing help output..."
	@$(DIST_DIR)/prole-installer --help || echo "✗ Help test failed"
	@echo ""
	@echo "2. Testing app bundle..."
	@if [ -d "$(DIST_DIR)/Prole Installer.app" ]; then \
		echo "✓ App bundle exists"; \
		"$(DIST_DIR)/Prole Installer.app/Contents/MacOS/prole-installer" --help > /dev/null && echo "✓ App bundle executable works"; \
	fi
	@echo ""
	@echo "Manual tests required:"
	@echo "  - Double-click app to test GUI"
	@echo "  - Run with --no-gui in terminal without X to test ncurses"

# Clean build artifacts
clean:
	@echo "Cleaning build artifacts..."
	@rm -rf $(BUILD_DIR)
	@rm -rf $(DIST_DIR)
	@rm -f $(SPEC_FILE)
	@rm -rf __pycache__
	@rm -rf installer/__pycache__
	@rm -rf *.pyc
	@echo "✓ Clean complete"

# Development: quick rebuild
rebuild: clean build

.DEFAULT_GOAL := help
