#!/bin/bash
print_black() {
    echo -e "\033[30m$1\033[0m"
}

print_red() {
    echo -e "\033[31m$1\033[0m"
}

print_green() {
    echo -e "\033[32m$1\033[0m"
}

print_yellow() {
    echo -e "\033[33m$1\033[0m"
}

print_blue() {
    echo -e "\033[34m$1\033[0m"
}

print_magenta() {
    echo -e "\033[35m$1\033[0m"
}

print_cyan() {
    echo -e "\033[36m$1\033[0m"
}

print_grey() {
    echo -e "\033[37m$1\033[0m"
}

print_white() {
    echo "$1"
}

clear
PROGRAM="PortForwardGo"

service=""
mirror="https://pkg.zeroteam.top"

print_cyan "$PROGRAM 管理脚本"

# Read parameters
{
    # Parse parameters
    while [ $# -gt 0 ]; do
        case $1 in
        --service)
            service=$2
            shift
            ;;
        --mirror)
            mirror=$2
            shift
            ;;
        *)
            print_white " 未知的参数: $1"
            exit
            ;;
        esac
        shift
    done

    # Check parameters validity
    {
        if [ -z "$mirror" ]; then
            print_red " 参数 'mirror' 不存在"
            exit 2
        fi
    }
}

# Check system
{
    print_yellow " ** 检查系统信息..."

    # Check systemd
    command -V systemctl >/dev/null
    if [ "$?" -ne 0 ]; then
        print_red "systemd 不存在"
        exit 1
    fi
}

# Check installed program
{
    print_yellow " ** 检查安装信息..."

    if [ -n "$service" ]; then
        if [ "$service" == "all" ]; then
            print_red "服务名 '$service' 无效!"
            exit 1
        fi

        if [ ! -d "/opt/$service" ]; then
            print_red "'/opt/$service' 不存在!"
            exit 1
        fi

        if [ ! -f "/opt/$service/$PROGRAM" ]; then
            print_red "'/opt/$service' 不包含程序文件!"
            exit 1
        fi
    else
        DIRS=()

        if [ ! -d "/opt" ]; then
            print_red "没有找到已安装的程序, 请先安装!"
            exit 1
        fi

        for DIR in $(ls /opt); do
            [ -f "/opt/$DIR/$PROGRAM" ] && DIRS=("${DIRS[@]}" "$DIR")
        done

        case ${#DIRS[@]} in
        0)
            print_red "没有找到已安装的程序, 请先安装!"
            exit 1
            ;;
        1)
            service="${DIRS[0]}"
            version=$(/opt/$service/$PROGRAM -v)

            print_white " 检测到已安装的程序目录: /opt/$service (版本号 $version)"
            ;;
        *)
            print_white " 检测到 ${#DIRS[@]} 个已安装程序的目录, 请选择一个:"
            for DIR in ${DIRS[@]}; do
                version=$(/opt/$DIR/$PROGRAM -v)

                print_white " - $DIR (版本号 $version)"
            done

            while [ -z "$service" ] || [ ! -f "/opt/$service/$PROGRAM" ]; do
                [ -n "$service" ] && print_red " 错误的输入!"

                read -ep " 请输入你想操作的目录: " service
            done
            ;;
        esac
    fi

    version=$(/opt/$service/$PROGRAM -v)
}

clear

print_cyan "$PROGRAM 管理脚本"
print_yellow "当前选择的目录: /opt/$service (版本号 $version)"
print_white ""
print_magenta "---- 菜单 ----"
print_green "1. 启动程序"
print_green "2. 停止程序"
print_green "3. 重启程序"
print_green "4. 查看程序运行状态"
print_magenta "---- 维护 ----"
print_green "5. 查看系统服务日志 (例如 崩溃)"
print_green "6. 查看程序日志 (例如 加载规则)"
print_green "7. 升级程序到最新版本"
print_green "8. 降级程序到指定版本"
print_green "9. 卸载程序"
print_magenta "---- 优化 ----"
print_green "10. 安装TCP调优"
print_green "11. 卸载TCP调优"
print_magenta "---- 其他 ----"
print_green "0. 退出脚本"
print_white ""

while [ true ]; do
    read -ep " 请选择一个操作: " choice

    case $choice in
    0)
        print_white "退出中..."
        exit 0
        ;;
    1)
        systemctl start $service

        exit 0
        ;;
    2)
        systemctl stop $service

        exit 0
        ;;
    3)
        systemctl restart $service

        exit 0
        ;;
    4)
        systemctl status $service -l

        exit 0
        ;;
    5)
        journalctl -fu $service

        exit 0
        ;;
    6)
        tail -f /opt/$service/run.log

        exit 0
        ;;
    7)
        bash <(curl -sSL "https://scripts.zeroteam.top/PortForwardGo/update.sh") --service $service --mirror $mirror

        exit 0
        ;;
    8)
        read -ep "请输入你想降级的版本号: " version
        bash <(curl -sSL "https://scripts.zeroteam.top/PortForwardGo/update.sh") --service $service --mirror $mirror --version $version

        exit 0
        ;;
    9)
        bash <(curl -sSL "https://scripts.zeroteam.top/PortForwardGo/uninstall.sh") --service $service

        exit 0
        ;;
    10)
        bash <(curl -sSL "https://scripts.zeroteam.top/PortForwardGo/tcp_zhcn.sh")

        exit 0
        ;;
    11)
        rm -f /etc/sysctl.d/98-tcp.conf
        sysctl --system 2>&1 >/dev/null

        print_green "成功卸载TCP优化配置文件, 部分设置可能需要重启生效!"
        exit 0
        ;;
    *)
        print_red "错误的输入!"
        ;;
    esac
done
