#!/bin/bash

# ==================== 配置 ====================
# 本地配置文件路径（你随便改）
CONFIG_FILE="/storage/emulated/0/shell源码/local_config.txt"
LOCAL_VERSION="1.0"

# 颜色
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
NONE="\033[0m"
# ==============================================

clear
echo -e "${GREEN}==== 本地控制脚本 ====${NONE}"

# 检查配置文件是否存在
if [ ! -f "$CONFIG_FILE" ]; then
    echo -e "${RED}[!] 配置文件不存在，自动创建默认配置${NONE}"
    cat > "$CONFIG_FILE" << EOF
NOTICE=欢迎使用本地控制脚本
VERSION=1.0
UPDATE_FILE=./update.sh
EOF
fi

# 读取本地配置
NOTICE=$(grep "^NOTICE=" "$CONFIG_FILE" | cut -d'=' -f2-)
VERSION=$(grep "^VERSION=" "$CONFIG_FILE" | cut -d'=' -f2-)
UPDATE_FILE=$(grep "^UPDATE_FILE=" "$CONFIG_FILE" | cut -d'=' -f2-)

# 显示公告
echo -e "\n${GREEN}[公告] $NOTICE${NONE}"

# 版本对比
echo -e "\n[本地版本] $LOCAL_VERSION"
echo -e "[配置版本] $VERSION"

if [ "$VERSION" != "$LOCAL_VERSION" ]; then
    echo -e "${YELLOW}[!] 发现新版本，是否更新？[y/n]${NONE}"
    read -r ans
    if [ "$ans" = "y" ] || [ "$ans" = "Y" ]; then
        if [ -f "$UPDATE_FILE" ]; then
            echo -e "${GREEN}[*] 正在从本地更新...${NONE}"
            cp "$UPDATE_FILE" "$0"
            chmod +x "$0"
            echo -e "${GREEN}[√] 更新完成！重新运行即可${NONE}"
            exit 0
        else
            echo -e "${RED}[!] 更新文件不存在${NONE}"
        fi
    fi
fi

# 菜单
echo -e "\n==== 功能菜单 ===="
echo "1  执行91命令"
echo "2  查看公告"
echo "3  重新加载配置"
echo "0  退出"
read -p "请选择：" num

case $num in
    1)
        read -p "命令：" cmd
        $cmd
        ;;
    2)
        echo -e "\n${GREEN}[公告] $NOTICE${NONE}"
        ;;
    3)
        exec "$0"
        ;;
    0)
        exit 0
        ;;
    *)
        echo "输入错误"
        ;;
esac
