#!/bin/bash
set -e

echo "=========================================="
echo "  Theos 完整环境配置脚本"
echo "=========================================="
echo ""

export THEOS=/opt/theos

# 1. 下载 iOS 工具链
echo "[1/2] 下载 iOS 工具链 (约 2GB，可能需要几分钟)..."
TOOLCHAIN_DIR=$THEOS/toolchain/linux
mkdir -p $TOOLCHAIN_DIR

cd $TOOLCHAIN_DIR
if [ ! -d "iphone" ]; then
    echo "下载 LLVM 工具链..."
    curl -sL "https://github.com/theos/theos-sdks/releases/download/llvm-14.0.0/llvm-14.0.0-linux-x86_64.tar.gz" | tar -xz
    mv llvm-14.0.0-linux-x86_64 iphone
    echo "工具链下载完成"
else
    echo "工具链已存在，跳过"
fi

# 2. 验证环境
echo ""
echo "[2/2] 验证环境..."
echo "THEOS: $THEOS"
echo "ldid: $(which ldid)"
echo "clang: $(which clang)"

# 3. 测试编译
echo ""
echo "=========================================="
echo "  环境配置完成！"
echo "=========================================="
echo ""
echo "编译插件："
echo "  cd /www/wwwroot/yyy.xmling.top/tweak-template/KeyboardTools"
echo "  export THEOS=/opt/theos"
echo "  make package"
echo ""
