#!/bin/bash
# MUNDO Agent — macOS .app 启动脚本
# 启动前自动检测版本，脱节则自动同步

osascript <<'APPLESCRIPT'
tell application "Terminal"
    activate
    do script "
        clear
        
        # ═══ 自动版本同步 ═══
        SRC=\"$HOME/Desktop/lihongwei-cn/mundo-agent\"
        DST=\"$HOME/.hermes/mundo-agent\"
        
        if [ -f \"$SRC/version.txt\" ] && [ -f \"$DST/version.txt\" ]; then
            SRC_VER=$(cat \"$SRC/version.txt\" | tr -d '[:space:]')
            DST_VER=$(cat \"$DST/version.txt\" | tr -d '[:space:]')
            
            if [ \"$SRC_VER\" != \"$DST_VER\" ]; then
                echo \"  🔄 检测到版本脱节，自动同步中...\"
                
                # 同步 .py 文件
                for f in mundo.py core.py llm.py setup.py tools.py approval.py display.py memory.py memory_import.py models.py agents.py delegation.py cloud_sync.py; do
                    [ -f \"$SRC/$f\" ] && cp \"$SRC/$f\" \"$DST/$f\"
                done
                cp \"$SRC/version.txt\" \"$DST/version.txt\"
                [ -f \"$SRC/requirements.txt\" ] && cp \"$SRC/requirements.txt\" \"$DST/requirements.txt\"
                
                echo \"  ✅ 已同步: $DST_VER → $SRC_VER\"
                echo \"\"
            fi
        fi
        
        cd ~/.hermes/mundo-agent
        source venv/bin/activate
        python3 mundo.py
    "
    set custom title of front window to "MUNDO"
end tell
APPLESCRIPT
