#!/bin/bash
# 和平精英辅助脚本 - 管理员工具（修复路径问题）
# 功能：配置更新链接 + 强制更新

clear
echo -e "\033[36m===== 和平精英辅助脚本 - 管理员工具 =====\033[0m"
echo -e "\033[33m===== 配置更新链接 =====\033[0m"
echo

# 自动检测可用路径
detect_path() {
    # 常见的Android路径
    paths=(
        "/sdcard"
        "/storage/emulated/0" 
        "/mnt/sdcard"
        "/storage/sdcard0"
        "./"  # 当前目录作为最后选项
    )
    
    for path in "${paths[@]}"; do
        if [ -d "$path" ] && [ -w "$path" ]; then
            echo "$path"
            return 0
        fi
    done
    
    # 如果所有路径都不可用，创建本地目录
    mkdir -p "./elite_script"
    echo "./elite_script"
    return 1
}

# 自动检测路径
echo -e "\033[32m🔍 正在自动检测可用路径...\033[0m"
AUTO_PATH=$(detect_path)
echo -e "✅ 检测到可用路径：$AUTO_PATH"

# 获取更新链接
read -p "请输入主脚本更新链接（如 https://xxx/card_mgr.sh）：" MAIN_URL
if [ -z "$MAIN_URL" ]; then
    echo -e "\033[31m❌ 主脚本链接不能为空！\033[0m"
    exit 1
fi

read -p "请输入扩展脚本更新链接（如 https://xxx/extension.sh）：" EXT_URL
if [ -z "$EXT_URL" ]; then
    echo -e "\033[31m❌ 扩展脚本链接不能为空！\033[0m"
    exit 1
fi

# 获取目标路径（使用自动检测的路径作为默认值）
echo -e "\n\033[32m✅ 链接输入完成！\033[0m"
echo -e "\033[33m请输入脚本本地存储路径（默认：$AUTO_PATH）：\033[0m"
read -p "路径：" TARGET_PATH
if [ -z "$TARGET_PATH" ]; then
    TARGET_PATH="$AUTO_PATH"
fi

# 确保路径存在并具有写入权限
if [ ! -d "$TARGET_PATH" ]; then
    echo -e "\033[33m⚠️  路径 $TARGET_PATH 不存在，正在创建...\033[0m"
    mkdir -p "$TARGET_PATH"
    if [ $? -ne 0 ]; then
        echo -e "\033[31m❌ 创建路径失败！请检查权限\033[0m"
        exit 1
    fi
fi

if [ ! -w "$TARGET_PATH" ]; then
    echo -e "\033[31m❌ 路径 $TARGET_PATH 没有写入权限！\033[0m"
    exit 1
fi

# 生成配置文件
CONFIG_FILE="${TARGET_PATH}/update_config.txt"
echo "MAIN_URL=${MAIN_URL}" > "$CONFIG_FILE"
echo "EXT_URL=${EXT_URL}" >> "$CONFIG_FILE"
echo "TARGET_PATH=${TARGET_PATH}" >> "$CONFIG_FILE"

echo -e "\n\033[32m✅ 配置文件生成成功！\033[0m"
echo "配置文件路径：$CONFIG_FILE"
echo "主脚本链接：$MAIN_URL"
echo "扩展脚本链接：$EXT_URL"
echo "本地路径：$TARGET_PATH"

# 强制更新
echo -e "\n\033[33m===== 开始强制更新 =====\033[0m"
read -p "是否立即执行更新？（y/n，默认y）：" UPDATE_CHOICE
if [[ -z "$UPDATE_CHOICE" || "$UPDATE_CHOICE" =~ ^[Yy]$ ]]; then
    # 备份旧脚本
    echo -e "\n📦 正在备份旧脚本..."
    if [ -f "${TARGET_PATH}/card_mgr.sh" ]; then
        cp "${TARGET_PATH}/card_mgr.sh" "${TARGET_PATH}/card_mgr.sh.bak.$(date +%Y%m%d%H%M%S)"
    fi
    if [ -f "${TARGET_PATH}/extension.sh" ]; then
        cp "${TARGET_PATH}/extension.sh" "${TARGET_PATH}/extension.sh.bak.$(date +%Y%m%d%H%M%S)"
    fi

    # 下载新主脚本
    echo -e "📥 正在下载主脚本..."
    MAIN_DOWNLOAD=$(curl -s -w "%{http_code}" -o "${TARGET_PATH}/card_mgr_new.sh" "$MAIN_URL")
    if [ "$MAIN_DOWNLOAD" != "200" ]; then
        echo -e "\033[31m❌ 主脚本下载失败！HTTP 状态码：$MAIN_DOWNLOAD\033[0m"
        rm -f "${TARGET_PATH}/card_mgr_new.sh"
        exit 1
    fi

    # 下载新扩展脚本
    echo -e "📥 正在下载扩展脚本..."
    EXT_DOWNLOAD=$(curl -s -w "%{http_code}" -o "${TARGET_PATH}/extension_new.sh" "$EXT_URL")
    if [ "$EXT_DOWNLOAD" != "200" ]; then
        echo -e "\033[31m❌ 扩展脚本下载失败！HTTP 状态码：$EXT_DOWNLOAD\033[0m"
        rm -f "${TARGET_PATH}/card_mgr_new.sh" "${TARGET_PATH}/extension_new.sh"
        exit 1
    fi

    # 覆盖旧脚本
    echo -e "🔧 正在更新脚本..."
    chmod +x "${TARGET_PATH}/card_mgr_new.sh" "${TARGET_PATH}/extension_new.sh"
    mv -f "${TARGET_PATH}/card_mgr_new.sh" "${TARGET_PATH}/card_mgr.sh"
    mv -f "${TARGET_PATH}/extension_new.sh" "${TARGET_PATH}/extension.sh"

    echo -e "\n\033[32m✅ 强制更新成功！\033[0m"
    echo -e "\033[33m脚本路径：$TARGET_PATH\033[0m"
    echo -e "\033[33m运行命令：bash ${TARGET_PATH}/card_mgr.sh\033[0m"
else
    echo -e "\n\033[33m已取消强制更新！\033[0m"
    echo -e "\033[33m你可以手动运行主脚本执行更新：bash ${TARGET_PATH}/card_mgr.sh\033[0m"
fi

exit 0