#!/bin/sh

# 获取脚本所在目录
DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
cd "$DIR" || exit 1

# ------------------- 第一级菜单：选择驱动包 -------------------
dirs=$(find . -maxdepth 1 -type d ! -name "." ! -name ".*" -printf "%f\n" | sort)

if [ -z "$dirs" ]; then
    echo "错误：没有找到任何驱动包目录。"
    exit 1
fi

total=$(echo "$dirs" | wc -l)

while true; do
    echo "=================================="
    echo "  请选择要加载的驱动包"
    echo "  天机处理器如果刷驱动1，有重启或没效果的情况，刷驱动2里面的驱动"
   
    echo "=================================="
    i=1
    for d in $dirs; do
        printf "  %2d) %s\n" "$i" "$d"
        i=$((i+1))
    done
    echo "   0) 退出"
    echo "=================================="
    printf "请输入编号 [0-%d]： " "$total"
    read choice

    if [ "$choice" = "0" ] 2>/dev/null; then
        echo "已退出。"
        exit 0
    fi

    if ! echo "$choice" | grep -qE '^[0-9]+$'; then
        echo "无效输入（请输入数字）。"
        continue
    fi
    if [ "$choice" -lt 1 ] || [ "$choice" -gt "$total" ]; then
        echo "编号超出范围。"
        continue
    fi
    break
done

selected_pkg=$(echo "$dirs" | sed -n "${choice}p")
echo "进入驱动包：$selected_pkg"

# 进入选中的驱动包目录
cd "$DIR/$selected_pkg" || exit 1

# ------------------- 第二级菜单：选择驱动文件 -------------------
files=$(find . -maxdepth 1 -type f -printf "%f\n" | sort)

if [ -z "$files" ]; then
    echo "错误：驱动包“$selected_pkg”目录下没有文件。"
    exit 1
fi

total_files=$(echo "$files" | wc -l)

while true; do
    echo "=================================="
    echo "  请选择要加载的驱动文件"
    echo "=================================="
    i=1
    for f in $files; do
        size=$(ls -lh "$f" | awk '{print $5}')
        printf "  %2d) %s  (%s)\n" "$i" "$f" "$size"
        i=$((i+1))
    done
    echo "   0) 返回上级菜单"
    echo "=================================="
    printf "请输入编号 [0-%d]： " "$total_files"
    read file_choice

    if [ "$file_choice" = "0" ] 2>/dev/null; then
        echo "返回上级菜单，请重新运行脚本。"
        exit 0
    fi

    if ! echo "$file_choice" | grep -qE '^[0-9]+$'; then
        echo "无效输入（请输入数字）。"
        continue
    fi
    if [ "$file_choice" -lt 1 ] || [ "$file_choice" -gt "$total_files" ]; then
        echo "编号超出范围。"
        continue
    fi
    break
done

selected_file=$(echo "$files" | sed -n "${file_choice}p")
echo "正在加载驱动文件：$selected_file"

# ------------------- 加载并判断结果 -------------------
success=0
case "$selected_file" in
    *.sh)
        echo "执行脚本：$selected_file"
        sh "$selected_file" </dev/null >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            success=1
        fi
        ;;
    *.ko)
        echo "加载内核模块：$selected_file"
        insmod "$selected_file" </dev/null >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            success=1
        fi
        ;;
    *)
        echo "未知扩展名，尝试作为内核模块加载..."
        insmod "$selected_file" </dev/null >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            success=1
            echo "成功加载内核模块：$selected_file"
        else
            echo "作为内核模块加载失败，尝试作为脚本执行..."
            sh "$selected_file" </dev/null >/dev/null 2>&1
            if [ $? -eq 0 ]; then
                success=1
                echo "成功执行脚本：$selected_file"
            else
                echo "错误：无法加载或执行 $selected_file。"
            fi
        fi
        ;;
esac

# 最终输出结果
if [ $success -eq 1 ]; then
    echo "驱动刷入成功，请执行起源"
else
    echo "驱动刷入失败，请检查文件或环境。"
    exit 1
fi