# VoiceInput Makefile
# macOS 14+ 菜单栏语音输入应用

APP_NAME      := VoiceInput
BUILD_DIR     := .build
RELEASE_DIR   := $(BUILD_DIR)/release
PRODUCT       := $(RELEASE_DIR)/$(APP_NAME)
APP_BUNDLE    := $(RELEASE_DIR)/$(APP_NAME).app
INSTALL_DIR   := /Applications/$(APP_NAME).app
ENTITLEMENTS  := Sources/Resources/VoiceInput.entitlements

.PHONY: all build run install clean debug release

all: build

# ============================================================
# Build
# ============================================================
build: debug

debug:
	@echo "🔨 Building Debug..."
	swift build -c debug --disable-sandbox
	@echo "📦 Creating app bundle..."
	@./scripts/create_app_bundle.sh debug
	@echo "✅ Build complete: $(APP_BUNDLE)"

release:
	@echo "🔨 Building Release..."
	swift build -c release --disable-sandbox
	@echo "📦 Creating app bundle..."
	@./scripts/create_app_bundle.sh release
	@echo "✅ Release build complete: $(APP_BUNDLE)"

# ============================================================
# Run
# ============================================================
run: debug
	@echo "🚀 Launching..."
	open $(APP_BUNDLE)

# ============================================================
# Install
# ============================================================
install: release
	@echo "📥 Installing to $(INSTALL_DIR)..."
	@if [ -d "$(INSTALL_DIR)" ]; then rm -rf "$(INSTALL_DIR)"; fi
	cp -R $(APP_BUNDLE) $(INSTALL_DIR)
	@echo "✅ Installed to $(INSTALL_DIR)"

# ============================================================
# Clean
# ============================================================
clean:
	@echo "🧹 Cleaning..."
	swift package clean
	rm -rf $(RELEASE_DIR)
	@echo "✅ Clean"

# ============================================================
# Entitlements
# ============================================================
entitlements: $(ENTITLEMENTS)

$(ENTITLEMENTS):
	@mkdir -p Sources/Resources
	@echo '<?xml version="1.0" encoding="UTF-8"?>' > $(ENTITLEMENTS)
	@echo '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' >> $(ENTITLEMENTS)
	@echo '<plist version="1.0">' >> $(ENTITLEMENTS)
	@echo '<dict>' >> $(ENTITLEMENTS)
	@echo '    <key>com.apple.security.device.audio-input</key>' >> $(ENTITLEMENTS)
	@echo '    <true/>' >> $(ENTITLEMENTS)
	@echo '    <key>com.apple.security.cs.disable-library-validation</key>' >> $(ENTITLEMENTS)
	@echo '    <true/>' >> $(ENTITLEMENTS)
	@echo '</dict>' >> $(ENTITLEMENTS)
	@echo '</plist>' >> $(ENTITLEMENTS)

# ============================================================
# Codesign (ad-hoc)
# ============================================================
sign: release
	@echo "🔏 Signing..."
	codesign --force --deep --sign - $(APP_BUNDLE)
	@echo "✅ Signed (ad-hoc)"

# ============================================================
# DMG (分发)
# ============================================================
DMG_NAME := $(APP_NAME)-$(shell date +%Y%m%d).dmg
DMG_TMP  := $(BUILD_DIR)/dmg_temp

dmg: sign
	@echo "💿 Creating DMG..."
	@rm -rf $(DMG_TMP) $(DMG_NAME)
	@mkdir -p $(DMG_TMP)
	@cp -R $(APP_BUNDLE) $(DMG_TMP)/
	@echo '{"models":[],"default_model":"","tavily_api_key":""}' > $(DMG_TMP)/$(APP_NAME).app/Contents/Resources/Agent/models.json
	@ln -s /Applications $(DMG_TMP)/Applications
	@hdiutil create -volname "$(APP_NAME)" -srcfolder $(DMG_TMP) -ov -format UDZO "$(DMG_NAME)" > /dev/null 2>&1
	@rm -rf $(DMG_TMP)
	@echo "✅ DMG created: $(DMG_NAME)"
