#!/bin/bash
# KongGoo 自動安裝器

APP_NAME="KongGoo.app"
INSTALLER_DIR="$(cd "$(dirname "$0")/../.." && pwd)"
SOURCE_APP="$INSTALLER_DIR/Resources/$APP_NAME"
TARGET_DIR="/Applications"
TARGET_APP="$TARGET_DIR/$APP_NAME"

# 顯示安裝視窗
osascript << 'APPLESCRIPT'
tell application "System Events"
    activate
    display dialog "歡迎使用 KongGoo 安裝器！

將自動為您：
1. 清除安全限制
2. 安裝到 Applications 資料夾
3. 打開應用程式

點擊「安裝」開始。" buttons {"取消", "安裝"} default button "安裝" with icon note
    if button returned of result is "取消" then
        return "cancel"
    end if
end tell
APPLESCRIPT

if [ $? -ne 0 ]; then
    exit 0
fi

# 檢查來源 App 是否存在
if [ ! -d "$SOURCE_APP" ]; then
    osascript -e 'display dialog "錯誤：找不到安裝檔案" buttons {"確定"} default button "確定" with icon stop'
    exit 1
fi

# 顯示進度
osascript << 'APPLESCRIPT'
tell application "System Events"
    display dialog "正在安裝，請稍候..." buttons {} default button "確定" giving up after 2
end tell
APPLESCRIPT

# 1. 清除 quarantine 屬性
echo "清除安全限制..."
xattr -cr "$SOURCE_APP" 2>/dev/null || true
xattr -d com.apple.quarantine "$SOURCE_APP" 2>/dev/null || true

# 如果還是不行，嘗試使用 sudo
if xattr -l "$SOURCE_APP" 2>/dev/null | grep -q "com.apple.quarantine"; then
    # 請求管理員權限
    osascript << 'APPLESCRIPT'
tell application "System Events"
    activate
    display dialog "需要管理員權限來清除安全限制。

請輸入您的 Mac 密碼。" buttons {"取消", "繼續"} default button "繼續" with icon caution
end tell
APPLESCRIPT
    
    if [ $? -eq 0 ]; then
        sudo xattr -cr "$SOURCE_APP" 2>/dev/null || true
        sudo xattr -d com.apple.quarantine "$SOURCE_APP" 2>/dev/null || true
    fi
fi

# 2. 複製到 Applications
echo "安裝到 Applications..."
if [ -d "$TARGET_APP" ]; then
    rm -rf "$TARGET_APP"
fi

cp -R "$SOURCE_APP" "$TARGET_APP"

# 3. 再次清除 Applications 中的 App 的 quarantine
xattr -cr "$TARGET_APP" 2>/dev/null || true
xattr -d com.apple.quarantine "$TARGET_APP" 2>/dev/null || true

# 4. 打開 App
echo "打開應用程式..."
sleep 1
open "$TARGET_APP"

# 顯示完成訊息
osascript << 'APPLESCRIPT'
tell application "System Events"
    activate
    display dialog "✅ 安裝完成！

KongGoo 已安裝到 Applications 資料夾並已打開。

如果 App 顯示「已損毀」，請：
1. 右鍵點擊 Applications 中的 KongGoo
2. 選擇「打開」
3. 在彈出視窗中點擊「打開」" buttons {"確定"} default button "確定" with icon note
end tell
APPLESCRIPT

exit 0
