NAME=""
PORT=""                 # ssh frp port
PORT_SERVER="52.89.207.198"
VAST_INSTALL_KEY=""
START_PORT=""
END_PORT=""


SSH_FRPS=39.107.137.58

while [[ $# -gt 0 ]]; do
    case "$1" in
        --name)
            NAME="$2"
            shift 2
            ;;
        --port)
            PORT="$2"
            shift 2
            ;;
        --start-port)
            START_PORT="$2"
            shift 2
            ;;
        --end-port)
            END_PORT="$2"
            shift 2
            ;;

        --public-ip)
            PORT_SERVER="$2"
            shift 2
            ;;
        --vast-install-key)
            VAST_INSTALL_KEY="$2"
            shift 2
            ;;
        -h|--help)
            echo "Usage: $0 --name <name> --port <ssh_frp_port> [--public-ip <public_ip>] [--vast-install-key <key>]"
            echo "Example:"
            echo "  $0 --name myserver --port 40001 --public-ip 52.89.207.198 --vast-install-key '5ed661e763f06967f7f63ee839002e7c2a352cd6c88fa026cf4e65dfbb2d2c68'"
            exit 0
            ;;
        *)
            echo "未知参数: $1"
            echo "使用 --help 查看帮助"
            exit 1
            ;;
    esac
done

if [ "$EUID" -ne 0 ]; then
    echo "请以 root 用户运行此脚本"
    exit 1
fi

# add param check
if [ -z "$NAME" ] || [ -z "$PORT" ]; then
    echo "Usage: $0 --name <name> --port <ssh_frp_port> [--port-server <public_ip>] [--vast-install-cmd <cmd>]"
    echo "Example:"
    echo "  $0 --name myserver --port 40001 --port-server 52.89.207.198 --vast-install-cmd 'wget https://console.vast.ai/install -O install; sudo python3 install 5ed661e763f06967f7f63ee839002e7c2a352cd6c88fa026cf4e65dfbb2d2c68 --interactive; history -d \$((HISTCMD-1));'"
    exit 1
fi

cd /opt/ && wget https://dl.bitheart.org/installpack.tar.gz && tar zxf /opt/installpack.tar.gz
if [ -z "$VAST_INSTALL_KEY" ];then
    cd /opt/installpack && bash ./install.sh --name $NAME  --port $PORT --start-port $START_PORT --end-port $END_PORT
fi
if [ -n "$VAST_INSTALL_KEY" ];then
    cd /opt/installpack && bash ./install.sh --name $NAME  --port $PORT  --vast-install-key "$VAST_INSTALL_KEY" --start-port $START_PORT --end-port $END_PORT
fi
