#!/bin/bash
echo "=== 编译 键盘工具栏 插件 ==="

cd /www/wwwroot/yyy.xmling.top/tweak-template/KeyboardTools

# 检查 Theos 是否安装
if [ ! -d "$THEOS" ]; then
    echo "错误：未找到 Theos，请先安装 Theos"
    echo "安装命令：git clone --recursive https://github.com/theos/theos.git $THEOS"
    exit 1
fi

# 编译
make clean
make package

# 检查编译结果
if [ -f "packages/com.xmling.keyboardtools_1.0.0-1_iphoneos-arm.deb" ]; then
    echo ""
    echo "✅ 编译成功！"
    echo "输出文件：packages/com.xmling.keyboardtools_1.0.0-1_iphoneos-arm.deb"
    
    # 复制到 debs 目录
    cp packages/com.xmling.keyboardtools_1.0.0-1_iphoneos-arm.deb ../../debs/
    echo "✅ 已复制到 debs/ 目录"
    
    # 更新 Packages 索引
    cd /www/wwwroot/yyy.xmling.top
    ./gen-packages.sh
    echo "✅ Packages 索引已更新"
else
    echo "❌ 编译失败"
    exit 1
fi
