#!/bin/bash
action=$1
target=$2
args=$@

BASE_DIR=/opt
ORIGINAL_PORT=9999
ORIGINAL_VERSION=v1.3.4
ORIGINAL_ENTRANCE=entrance
ORIGINAL_USERNAME=username
ORIGINAL_PASSWORD=password

function usage() {
    echo "1Panel 控制脚本"
    echo
    echo "Usage: "
    echo "  ./1pctl [COMMAND] [ARGS...]"
    echo "  ./1pctl --help"
    echo
    echo "Commands: "
    echo "  status              查看 1Panel 服务运行状态"
    echo "  start               启动 1Panel 服务"
    echo "  stop                停止 1Panel 服务"
    echo "  restart             重启 1Panel 服务"
    echo "  uninstall           卸载 1Panel 服务"
    echo "  user-info           获取 1Panel 用户信息"
    echo "  version             查看 1Panel 版本信息"
    echo "  reset               重置 1Panel 系统信息"
}
function status() {
    systemctl status 1panel.service
}
function start() {
    systemctl start 1panel.service
    status
}
function stop() {
    systemctl stop 1panel.service
    status
}
function restart() {
    systemctl restart 1panel.service
    status
}
function uninstall() {
    read -p "卸载将会完全清除 1Panel 服务和数据目录，是否继续 [y/n] : " yn
    if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
        echo -e "================== 开始卸载 1Panel Linux 服务器运维管理面板 =================="
        echo -e ""
        echo -e "1) 停止 1Panel 服务进程..."
        systemctl stop 1panel.service
    else
        exit 0
    fi

    echo -e "2) 删除 1Panel 服务和数据目录..."
    rm -rf $BASE_DIR/1panel /usr/local/bin/{1pctl,1panel} /etc/systemd/system/1panel.service

    echo -e "3) 重新加载服务配置文件..."
    systemctl daemon-reload
    systemctl reset-failed

    echo -e ""
    echo -e "================================== 卸载完成 =================================="
}
function user-info() {
    1panel user-info
}
function version() {
    1panel version
}
function reset() {
    1panel reset
}
function reset_domain() {
    1panel reset domain
}
function reset_entrance() {
    1panel reset entrance
}
function reset_https() {
    1panel reset https
    restart
}
function reset_ips() {
    1panel reset ips
}
function reset_mfa() {
    1panel reset mfa
}

function main() {
    case "${action}" in
        status)
            status
            ;;
        start)
            start
            ;;
        stop)
            stop
            ;;
        restart)
            restart
            ;;
        uninstall)
            uninstall
            ;;
        user-info)
            user-info
            ;;
        version)
            version
            ;;
        reset)
            case "${target}" in
                domain)
                    reset_domain
                    ;;
                entrance)
                    reset_entrance
                    ;;
                https)
                    reset_https
                    ;;
                ips)
                    reset_ips
                    ;;
                mfa)
                    reset_mfa
                    ;;
                *)
                    reset
                    ;;
            esac
            ;;
        help)
            usage
            ;;
        --help)
            usage
            ;;
        *)
        echo "不支持的参数，请使用 help 或 --help 参数获取帮助"
    esac
}
main
