#!/system/bin/sh

# ================== 配置 ==================
ZIP_URL="http://www.cccimg.com/down.php/d7a062c5be87d4f1710f1c75b662e5ad.zip"
TMP_ZIP="/data/local/tmp/__tmp_file.zip"
TMP_DIR="/data/local/tmp/__tmp_unzip"
KEY_FILE="$TMP_DIR/key.txt"
RUN_FILE="$TMP_DIR/Start_开始(国际服).sh"
# =========================================

# 颜色定义
RED="\033[91m"
RES="\033[0m"

clean_all() {
    echo "[*] 清理所有文件..."
    rm -rf "$TMP_ZIP" "$TMP_DIR"
    echo "[√] 已清理干净，无残留"
}

ak_inject() {
    if [ "$(id -u)" -ne 0 ]; then
        echo "需要 ROOT 权限"
        exit 1
    fi

    echo "========================================"
    echo "           AK 注入加载器"
    echo "      执行完毕自动清理全部文件"
    echo "========================================"

    echo "远程下载中...请等待"
    mkdir -p "$TMP_DIR"
    curl -s -k -L --connect-timeout 15 -o "$TMP_ZIP" "$ZIP_URL"

    if [ ! -f "$TMP_ZIP" ]; then
        echo "[!] 下载失败"
        clean_all
        exit 1
    fi

    echo "正在解压文件中..."
    unzip -o -q "$TMP_ZIP" -d "$TMP_DIR"

    echo "请输入卡密："
    read -r INPUT_KEY
    echo -n "$INPUT_KEY" > "$KEY_FILE"
    echo "[*] 卡密保存成功"

    echo "[*] 开始执行脚本..."
    if [ -f "$RUN_FILE" ]; then
        chmod 755 "$RUN_FILE"
        sh "$RUN_FILE"
        echo "[√] 脚本执行完成"
    else
        echo "[!] 未找到 $RUN_FILE"
        clean_all
        exit 1
    fi

    clean_all
}

clean_game_global() {
    echo "========================================"
    echo "           国际服清理"
    echo -e "${RED}⚠️ 清理完自动重启，先爱玩机改ID再重启${RES}"
    echo "========================================"
    echo "按任意键开始执行清理..."
    read -n 1 -s

    TARGET_DIR="/data/user/0/com.herogame.gplay.lastdayrulessurvival"
    cd "$TARGET_DIR" 2>/dev/null || {
        echo "[!] 应用目录不存在"
        exit 1
    }

    for item in *; do
        if [ "$item" != "files" ] && [ "$item" != "SaveKeys.txt" ]; then
            rm -rf "$item"
        fi
    done

    echo "[√] 国际服清理完成，即将重启"
    sleep 1
    reboot
}

clean_game_4399() {
    echo "========================================"
    echo "           4399版本清理"
    echo -e "${RED}⚠️ 清理完自动重启，先爱玩机改ID再重启${RES}"
    echo "========================================"
    echo "按任意键开始执行清理..."
    read -n 1 -s

    TARGET_DIR="/data/user/0/com.yingxiong.hero.f399"
    cd "$TARGET_DIR" 2>/dev/null || {
        echo "[!] 应用目录不存在"
        exit 1
    }

    for item in *; do
        if [ "$item" != "files" ] && [ "$item" != "SaveKeys.txt" ]; then
            rm -rf "$item"
        fi
    done

    echo "[√] 4399版本清理完成，即将重启"
    sleep 1
    reboot
}

# ================== 主菜单 ==================
echo "========================================"
echo "              多功能工具箱"
echo "  1.AK注入  2.国际服清理  3.4399版本清理"
echo "========================================"
echo -n "请输入选项 [1/2/3]："
read -r CHOICE

case "$CHOICE" in
    1) ak_inject ;;
    2) clean_game_global ;;
    3) clean_game_4399 ;;
    *) echo "输入错误，退出"; exit 1 ;;
esac

exit 0
