#!/bin/bash

# -----------------------------------
# Minecraft服务器部署脚本用户须知/协议
# 脚本开发者: bilibili@請叫我零贰
# 脚本版本: v1.0
# -----------------------------------

total_mem=$(free -g | awk '/^Mem:/{print $2}')
check_xmx() {
    while [ "$check" != "1" ]; do
        echo ""
        read -p "输入游戏服务器允许使用的最大内存[单位GB 例如1GB则输入1]: " xmx
        echo ""
        if [[ -z "$xmx" || ! "$xmx" =~ ^[0-9]+$ ]]; then
            echo "请输入有效的数字。"
        elif [ "$xmx" -gt "$total_mem" ]; then
            echo "输入的内存值超过了本机可用内存。本机最大可用内存为 ${total_mem}GB，请重新输入。"
        elif [ "$xmx" == 0 ]; then
            echo "值不能为0"
        fi
        if [[ "$xmx" -gt 0 && "$xmx" -le "$total_mem" ]]; then
            check=1
        fi
    done
    check=0
}
check_xms() {
    while [ "$check" != "1" ]; do
        echo ""
        read -p "输入游戏服务器的初始启动内存[单位GB 例如1GB则输入1]: " xms
        echo ""
        if [[ -z "$xms" || ! "$xms" =~ ^[0-9]+$ ]]; then
            echo "请输入有效的数字。"
        elif [ "$xms" -gt "$xmx" ]; then
            echo "初始启动内存不能大于最大内存，请重新输入。"
        elif [ "$xms" == 0 ]; then
            echo "值不能为0"
        fi
        if [[ "$xms" -gt 0 && "$xmx" -le "$total_mem" ]]; then
            check=1
        fi
    done
    check=0
}
bss() {
    echo "----------基本信息----------"
    ipaddr=$(ip addr show | awk '/inet / && !/inet6/ && !/127.0.0.1/ {sub(/\/.*$/, "", $2); print $2; exit}')
    if [ -f "$installdir/server.properties" ]; then
        if [ $versionbj -le 13 ]; then
            port=$(grep 'server-port=' $installdir/server.properties | cut -d'=' -f2)
        else
            port=$(grep 'query.port=' $installdir/server.properties | cut -d'=' -f2)
        fi
        echo "JAVA版本=${jdkversion}"
        echo "游戏版本=${version}"
        cat $installdir/server.properties | grep -E '^gamemode' | sed 's/^gamemode/游戏模式/'
        cat $installdir/server.properties | grep 'difficulty' | sed 's/^difficulty/游戏难度/'
        cat $installdir/server.properties | grep 'online-mode' | sed 's/^online-mode/正版验证开启情况/'
        if [ $versionbj -le 13 ]; then
            cat $installdir/server.properties | grep 'server-port' | sed 's/^server-port/端口号/'
        else
            cat $installdir/server.properties | grep 'query.port' | sed 's/^query.port/端口号/'
        fi
        echo "初始启动内存=${xms}GB"
        echo "最大运行内存=${xmx}GB"
        echo "服务器安装目录=${installdir}"
        echo "服务器IP地址=${ipaddr}:${port}"
    else
        echo "ERROR: server.properties 文件未生成，无法显示服务器信息，即将退出安装。"
        sleep 10
        exit 0
    fi
echo "-----------------------------"
}
set_gamemode() {
    read -p "请输入新的游戏模式 [0=生存, 1=创造, 2=冒险, 3=旁观者]: " newgamemode
    if [ $versionbj -le 13 ]; then
        case "$newgamemode" in
        0) newgamemode=0 ;;
        1) newgamemode=1 ;;
        2) newgamemode=2 ;;
        3) newgamemode=3 ;;
        *)
            echo "输入错误，请输入有效的游戏模式 (0-3)"
            continue
            ;;
        esac
    else
        case "$newgamemode" in
        0) newgamemode=survival ;;
        1) newgamemode=creative ;;
        2) newgamemode=adventure ;;
        3) newgamemode=spectator ;;
        *)
            echo "输入错误，请输入有效的游戏模式 (0-3)"
            continue
            ;;
        esac
    fi
    sed -i "s/^gamemode=.*/gamemode=${newgamemode}/" "$installdir/server.properties"
    echo "游戏模式已经修改为: $newgamemode"
    echo ""
}
set_difficulty() {
    read -p "请输入新的游戏难度 [0=和平, 1=简单, 2=普通, 3=困难]: " newdifficulty
    if [ $versionbj -le 13 ]; then
        case "$newdifficulty" in
        0) newdifficulty=0 ;;
        1) newdifficulty=1 ;;
        2) newdifficulty=2 ;;
        3) newdifficulty=3 ;;
        *)
            echo "输入错误，请输入有效的游戏难度 (0-3)"
            continue
            ;;
        esac
    else
        case "$newdifficulty" in
        0) newdifficulty=peaceful ;;
        1) newdifficulty=easy ;;
        2) newdifficulty=normal ;;
        3) newdifficulty=hard ;;
        *)
            echo "输入错误，请输入有效的游戏难度 (0-3)"
            continue
            ;;
        esac
    fi
    sed -i "s/^difficulty=.*/difficulty=${newdifficulty}/" "$installdir/server.properties"
    echo "游戏难度已经修改为: $newdifficulty"
    echo ""
}
set_onlinemode() {
    read -p "是否开启正版验证 [0=关闭,1=开启]: " newonlinemode
    case "$newonlinemode" in
        0)
            newonlinemode=false
            ;;
        1)
            newonlinemode=true
            ;;
        *)
            echo "输入错误，请输入有效值"
            continue
            ;;
    esac
    sed -i "s/^online-mode=.*/online-mode=${newonlinemode}/" "$installdir/server.properties"
    echo "正版验证已经修改为: $newonlinemode"
    echo ""
}
set_port() {
    read -p "请输入新的端口号: " newport
    if [ $versionbj -le 13 ]; then
        sed -i "s/^server\-port=.*/server-port=${newport}/" "$installdir/server.properties"
    else
        sed -i "s/^query\.port=.*/query.port=${newport}/" "$installdir/server.properties"
    fi
    echo "端口号已经修改为: $newport"
    echo ""
}
set_jvm() {
    check_xmx
    check_xms
    echo "最小运行内存已经修改为: ${xms}GB"
    echo "最大运行内存已经修改为: ${xmx}GB"
}

echo ""
echo ""
echo ""
echo ""
echo "欢迎使用零贰的Minecraft Forge版服务器快速部署脚本。"
echo "在继续使用和安装前，请您给予足够的权限运行该脚本并仔细阅读并同意以下用户协议："
echo ""
echo "-----------------------------------------------"
echo "【Minecraft服务器部署脚本用户协议】"
echo "1. 脚本仅供个人或团队部署私有Minecraft服务器使用，禁止用于任何商业用途。"
echo "2. 脚本及其相关文件属于原创作品，禁止复制、修改、二次发布、转售等。"
echo "3. 脚本提供免费工具，不对任何技术性问题负责，使用者需自行承担责任。"
echo "4. 使用者应遵守Mojang的《Minecraft最终用户许可协议》(EULA)及相关法律。"
echo "5. 禁止利用本脚本从事违法活动或侵犯他人权益的行为。"
echo "6. 如违反条款，开发者有权采取措施。"
echo "7. 本脚本可能会更新，开发者保留随时修改或终止脚本的权利。"
echo "-----------------------------------------------"
echo ""
echo "继续安装即表示您已阅读并同意上述协议内容。"
echo ""
echo "[1] 同意并继续安装 | [2] 不同意并退出安装"
read -p "请输入1或2: " agreechoice
case "$agreechoice" in
    1)
        echo "您已同意协议，继续安装..."
        ;;
    2)
        echo "您不同意协议，安装已退出。"
        sleep 10
        exit 0
        ;;
    *)
        echo "无效输入，安装已退出。"
        sleep 10
        exit 0
        ;;
esac
check=0
while [ "$check" != "8" ]; do
echo ""
echo "是否需要自定义安装目录"
echo "[1] 是 | [2] 否"
read -p "请输入1或2: " dirchoice
case "$dirchoice" in
    1)
        while [ "$check" != "02" ]; do
            read -p "请输入自定义安装目录 例[/opt/***]: " installdir
            installdir="${installdir%/}"
            if [ -f "$installdir/eula.txt" ] && [ -f "$installdir/server.properties" ]; then
                echo "已有服务器相关文件，请检查是否存在相关文件，即将退出安装"
                sleep 10
                exit 0
            else
                mkdir -p $installdir
            fi
            if [ -d $installdir ]; then
                echo "你的安装路径是：${installdir}/"
                echo ""
                echo "是否在这个目录下安装 [1]是 [2]不是"
                read -p "请输入1或2: " check
                if [ "$check" -eq "1" ]; then
                    check=02
                else
                    echo ""
                fi
            else
                echo "错误：'$installdir' 不是一个有效目录。请重新输入一个有效的目录路径。"
            fi
        done
        check=8
        ;;
    2)
        installdir=/opt/minecraft
        if [ -f "$installdir/eula.txt" ] && [ -f "$installdir/server.properties" ]; then
            echo "已有服务器相关文件，请检查是否存在相关文件，即将退出安装"
            sleep 10
            exit 0
        else
            mkdir -p $installdir
            check=8
        fi
        ;;
    *)
        echo "ERROR 错误的值"
        continue
        ;;
esac
done
check=0
#初始内存文件模块
check_xmx
check_xms
#下载模块
#curl -s "https://files.minecraftforge.net/net/minecraftforge/forge/" | awk '/<\/span>/ {gsub(/.*<\/span>|<\/a>.*/, ""); if ($0 ~ /^[[:space:]]*1\.1$/) {print; exit} else if (NF > 0) print}'
retry=0
retry_max=10
echo "正在获取版本列表，请稍等..."
while [ -z "$check_version" ]; do
    check_versions=$(curl -s "https://files.minecraftforge.net/net/minecraftforge/forge/" | awk '/<a href=/ {gsub(/.*<a href="[^"]+">|<\/a>.*/, ""); if ($0 ~ /^[0-9]+\.[0-9]+(\.[0-9]+)?$/) print}' | awk '{ split($0, ver, "."); if (ver[1] == 1 && ver[2] >= 6) print }')
    ((retry++))
    if [ $retry -gt $retry_max ]; then
        echo "重试多次无法获取版本列表，请检测网络连接情况，即将退出安装"
        sleep 10
        exit 0
    fi
    if [ -n "$check_versions" ]; then
        break
    fi
    echo "获取失败，正在尝试重试获取，第${retry}次"
    sleep 5
done
retry=0
while [ "$check" != "1" ]; do
    read -p "选择要安装的版本,若不清楚请输入0查看版本列表: " version
    if [ "$version" == "0" ]; then
        echo "---版本列表---"
        echo "$check_versions"
        #curl -s "https://files.minecraftforge.net/net/minecraftforge/forge/" | awk '/<a href=/ {gsub(/.*<a href="[^"]+">|<\/a>.*/, ""); if ($0 ~ /^[0-9]+\.[0-9]+(\.[0-9]+)?$/) print}' | awk '{ split($0, ver, "."); if (ver[1] == 1 && ver[2] >= 6) print }'
        echo "---- END ----"
    else
        #check_versions=$(curl -s "https://files.minecraftforge.net/net/minecraftforge/forge/" | awk '/<a href=/ {gsub(/.*<a href="[^"]+">|<\/a>.*/, ""); if ($0 ~ /^[0-9]+\.[0-9]+(\.[0-9]+)?$/) print}' | awk '{ split($0, ver, "."); if (ver[1] == 1 && ver[2] >= 6) print }')
        if echo "$check_versions" | grep -q "^$version$"; then
            check=1
        else
            echo "无效的版本号，请重新输入。"
            echo ""
        fi
    fi
done
echo "即将开始下载相关文件"
versionbj=$(echo "$version" | awk -F '.' '{print $2}')
check=0
retry_max=3
while [ $check != "22" ]; do
    serverjarlink=$(curl -s https://files.minecraftforge.net/net/minecraftforge/forge/index_{$version}.html | grep -oP '(?<=url=)[^&]+' | sed 's/".*//' | head -n 1)
    md5=$(curl -s https://files.minecraftforge.net/net/minecraftforge/forge/index_{$version}.html | grep "MD5:" | head -n 2 | tail -n 1 | sed 's/.*<strong>MD5:<\/strong> //;s/<br>//')
    sha=$(curl -s https://files.minecraftforge.net/net/minecraftforge/forge/index_{$version}.html | grep "SHA1:" | head -n 2 | tail -n 1 | sed 's/.*<strong>SHA1:<\/strong> //')
    curl -o $installdir/server.jar "$serverjarlink"
    check_md5=$(md5sum $installdir/server.jar | awk '{print $1}')
    check_sha=$(sha1sum $installdir/server.jar | awk '{print $1}')
    if [ -f "$installdir/server.jar" ] && [ "$check_md5" == "$md5" ] && [ "$check_sha" == "$sha" ]; then
        check=22
    elif [ $retry -gt $retry_max ]; then
        echo "重试多次下载 server.jar 失败，请检测网络连接情况，即将退出安装"
        sleep 10
        exit 0
    else
        ((retry++))
        echo "第${retry}次下载失败，正在重试"
    fi
done
retry=0
while [ $check != "33" ]; do
    if [ $versionbj -le 20 ]; then
        jdkversion=jdk17
        jdkdownload=$(curl -s https://jdk.java.net/java-se-ri/17-MR1 | grep -Eo '[^"]*linux-x64_bin.tar.gz' | head -n 1)
    else
        jdkversion=jdk23
        jdkdownload=$(curl -s https://jdk.java.net/23/ | grep -Eo '[^"]*linux-x64_bin.tar.gz' | head -n 1)
    fi
    sha=$(curl -s $jdkdownload.sha256)
    curl -o $installdir/$jdkversion.tar.gz "$jdkdownload"
    check_sha=$(sha256sum $installdir/$jdkversion.tar.gz | awk '{print $1}')
    if [ -f "$installdir/$jdkversion.tar.gz" ] && [ "$check_sha" == "$sha" ]; then
        check=33
    elif [ $retry -gt $retry_max ]; then
        echo "重试多次下载 ${jdkversion}.tar.gz 失败，请检测网络连接情况，即将退出安装"
        sleep 10
        exit 0
    else
        ((retry++))
        echo "第${retry}次下载失败，正在重试"
    fi
done
retry=0
tar -xvzf $installdir/$jdkversion.tar.gz -C $installdir/
rm -rf $installdir/$jdkversion.tar.gz
jdktardir=$(ls $installdir | grep -E 'java|jdk')
mkdir -p $installdir/$jdkversion
mv $installdir/$jdktardir/* $installdir/$jdkversion
rm -rf $installdir/$jdktardir
cat << EOF > $installdir/run.sh
cd $installdir/
$installdir/$jdkversion/bin/java -jar $installdir/server.jar nogui --installServer
EOF
chmod +x $installdir/run.sh
cd $installdir/
sh $installdir/run.sh
sleep 5
jarname=$(ls /opt/minecraft/ | grep forge*)
#1.16及以下版本重新安装配置jdk7/8环境
if [ $versionbj -le 16 ]; then
    rm -rf $installdir/$jdkversion
    while [ $check != "02" ]; do
        if [ $versionbj -ge 7 -a $version != "1.7.2" ]; then
            jdkversion=jdk8
            jdkdownload=$(curl -s https://jdk.java.net/java-se-ri/8-MR6 | grep -Eo '[^"]*linux-x64.tar.gz' | head -n 1)
        else
            jdkversion=jdk7
            jdkdownload=$(curl -s https://jdk.java.net/java-se-ri/7 | grep -Eo '[^"]*.tar.gz' | head -n 1)
        fi
        sha=$(curl -s $jdkdownload.sha256)
        curl -o $installdir/$jdkversion.tar.gz "$jdkdownload"
        check_sha=$(sha256sum $installdir/$jdkversion.tar.gz | awk '{print $1}')
        if [ -f "$installdir/$jdkversion.tar.gz" ] && [ "$check_sha" == "$sha" ]; then
            check=02
        elif [ $retry -gt $retry_max ]; then
            echo "重试多次下载 ${jdkversion}.tar.gz 失败，请检测网络连接情况，即将退出安装"
            sleep 10
            exit 0
        else
            ((retry++))
            echo "第${retry}次下载失败，正在重试"
        fi
    done
    tar -xvzf $installdir/$jdkversion.tar.gz -C $installdir/
    rm -rf $installdir/$jdkversion.tar.gz
    jdktardir=$(ls $installdir | grep -E 'java|jdk')
    mkdir -p $installdir/$jdkversion
    mv $installdir/$jdktardir/* $installdir/$jdkversion
    rm -rf $installdir/$jdktardir
    cat << EOF > $installdir/run.sh
cd $installdir/
$installdir/$jdkversion/bin/java -Xmx${xmx}G -Xms${xms}G -jar $installdir/$jarname nogui
EOF
    chmod +x $installdir/run.sh
    if [ $versionbj -ge 7 -a $version != "1.7.2" ]; then
        cd $installdir/
        sh $installdir/run.sh
    fi
fi
#>1.16.5预先配置使用内存与重置run.sh
if [ $versionbj -ge 17 ]; then
    if [ -f "$installdir/user_jvm_args.txt" ]; then
        cat << EOF || tee -a $installdir/user_jvm_args.txt

-Xmx${xmx}G
-Xms${xms}G
-XX:SoftMaxHeapSize=${xmx}G
-XX:+UnlockExperimentalVMOptions
-XX:+UseZGC
EOF
    fi
    ugadir=$(ls $installdir/libraries/net/minecraftforge/forge/)
    if [ $versionbj -gt 20 -o $version == "1.20.3" -o $version == "1.20.4" ]; then
        cat << EOF > $installdir/run.sh
#!/usr/bin/env sh
# Add custom JVM arguments (such as RAM allocation) to the user_jvm_args.txt

$installdir/$jdkversion/bin/java -jar $installdir/$jarname --onlyCheckJava || exit 1

# Add custom program arguments (such as nogui) to the next line before the "\$@" or pass them to this script directly
$installdir/$jdkversion/bin/java @user_jvm_args.txt @libraries/net/minecraftforge/forge/$ugadir/unix_args.txt nogui "\$@"
EOF
    else
        cat << EOF > $installdir/run.sh
#!/usr/bin/env sh
cd $installdir/
# Forge requires a configured set of both JVM and program arguments.
# Add custom JVM arguments to the user_jvm_args.txt
# Add custom program arguments {such as nogui} to this file in the next line before the "\$@" or
#  pass them to this script directly
$installdir/$jdkversion/bin/java @user_jvm_args.txt @libraries/net/minecraftforge/forge/$ugadir/unix_args.txt nogui "\$@"
EOF
    fi
fi
#eula协议
if [ $versionbj -ge 7 -a $version != "1.7.2" ]; then
    attempts=0  
    max_attempts=3
    while [ ! -f "$installdir/eula.txt" ] && [ $attempts -lt $max_attempts ]; do
        if [ -f "$installdir/eula.txt" ]; then
            break
        else
            sh $installdir/run.sh
            ((attempts++))
            sleep 5
        fi
    done
    if [ -f "$installdir/eula.txt" ]; then
        sed -i 's/eula=false/eula=true/g' $installdir/eula.txt  
        echo "set eula=true"
    else
        echo "安装失败：无法生成 eula.txt 文件"
        exit 0
    fi
fi

#安装screen
if command -v yum &> /dev/null; then
    osversion=$(rpm -E %{rhel})
    baseurl="http://mirrors.aliyun.com/centos/${osversion}/os/x86_64/"
    touch /etc/yum.repos.d/screen.repo
    cat <<EOL > /etc/yum.repos.d/screen.repo
[screen]
name=Screen Package for CentOS
baseurl=$baseurl
enabled=1
gpgcheck=0
EOL
    yum clean all
    yum makecache
    yum install -y screen
elif command -v apt &> /dev/null; then
    apt-get update
    apt-get install -y screen
else
    echo "未检测到 yum 或 apt，无法安装软件包"
fi
#后台启动生成配置文件并重置world
echo "正在等待生成配置文件"
screen -dmS minecraft /opt/minecraft/run.sh
wait=0
while [ $check != 02 ]; do
    if [ -f "$installdir/server.properties" ]; then
        check=02
    fi
    sleep 1
    ((wait++))
    if [ $wait -gt 30 ]; then
        echo "文件生成时间超过30秒，即将退出脚本，请检查运行情况后手动修改配置文件"
        sleep 10
        exit 0
    fi
done
sleep 5
check=0
screen -S minecraft -p 0 -X stuff "stop$(printf \\r)"
echo "服务器配置文件已生成，继续执行操作"
rm -rf $installdir/world/*
if command -v yum &> /dev/null; then
    yum remove -y screen
    rm -rf /etc/yum.repos.d/screen.repo
    yum clean all
    yum makecache
elif command -v apt &> /dev/null; then
    apt-get remove -y screen
else
    echo "无法卸载screen，不影响后续操作"
fi
bss
echo "是否需要修改游戏基本信息"
echo "[1] 修改 | [2] 暂不修改"
read -p "请输入1或2: " setchoice
echo ""
case "$setchoice" in
    1)
        while [ "$check" != "02" ]; do
            echo "选择要修改的项目"
            echo "[1]游戏模式 [2]游戏难度 [3]正版验证 [4]端口号 [5]内存大小 [6]全部修改 [0]退出修改"
            read -p "请输入0-6，一次输入一项: " setting
                case "$setting" in
                    0) break ;;
                    1) set_gamemode ;;
                    2) set_difficulty ;;
                    3) set_onlinemode ;;
                    4) set_port ;;
                    5) set_jvm;;
                    6) set_gamemode; set_difficulty; set_onlinemode; set_port; set_jvm ;;
                    *) continue ;;
                esac
            bss
            while true; do
                echo ""
                echo "是否继续修改: "
                read -p "[1]是 | [2]否: " setagain
                echo ""
                if [[ "$setagain" == "1" || "$setagain" == "2" ]]; then
                    break
                else
                    echo "请输入 1 或 2"
                    echo ""
                fi
            done
            if [ "$setagain" == "2" ]; then
                check=02
            fi
        done
        check=0
        ;;
    *) ;;
esac
###
if [ $versionbj -ge 17 ]; then
    if [ -f "$installdir/user_jvm_args.txt" ]; then
        cat << EOF > $installdir/user_jvm_args.txt
# Xmx and Xms set the maximum and minimum RAM usage, respectively.
# They can take any number, followed by an M or a G.
# M means Megabyte, G means Gigabyte.
# For example, to set the maximum to 3GB: -Xmx3G
# To set the minimum to 2.5GB: -Xms2500M

# A good default for a modded server is 4GB.
# Uncomment the next line to set it.
# -Xmx4G
-Xmx${xmx}G
-Xms${xms}G
-XX:SoftMaxHeapSize=${xmx}G
-XX:+UnlockExperimentalVMOptions
-XX:+UseZGC
EOF
    fi
    ugadir=$(ls $installdir/libraries/net/minecraftforge/forge/)
    if [ $versionbj -gt 20 -o $version == "1.20.3" -o $version == "1.20.4" ]; then
        cat << EOF > $installdir/run.sh
#!/usr/bin/env sh
# Add custom JVM arguments (such as RAM allocation) to the user_jvm_args.txt

$installdir/$jdkversion/bin/java -jar $installdir/$jarname --onlyCheckJava || exit 1

# Add custom program arguments (such as nogui) to the next line before the "\$@" or pass them to this script directly
$installdir/$jdkversion/bin/java @user_jvm_args.txt @libraries/net/minecraftforge/forge/$ugadir/unix_args.txt nogui "\$@"
EOF
    else
        cat << EOF > $installdir/run.sh
#!/usr/bin/env sh
cd $installdir/
# Forge requires a configured set of both JVM and program arguments.
# Add custom JVM arguments to the user_jvm_args.txt
# Add custom program arguments {such as nogui} to this file in the next line before the "\$@" or
#  pass them to this script directly
$installdir/$jdkversion/bin/java @user_jvm_args.txt @libraries/net/minecraftforge/forge/$ugadir/unix_args.txt nogui "\$@"
EOF
    fi
else
    cat << EOF > $installdir/run.sh
cd $installdir/
$installdir/$jdkversion/bin/java -Xmx${xmx}G -Xms${xms}G -jar $installdir/$jarname nogui
EOF
fi
echo "-----------------------------"
echo "是否现在启动服务器"
echo "[1] 启动 | [2] 暂不启动"
read -p "请输入1或2: " runchoice
echo ""
case "$runchoice" in
    1)
        echo "如果需要修改服务器配置选项请自行修改，配置文件路径为${installdir}/server.properties"
        echo "服务器模组请存放在${installdir}/mods文件夹下"
	echo "后续需要启动服务器时请执行[cd ${installdir}/ && sh ${installdir}/run.sh]即可启动服务器，无需再次执行部署脚本"
        echo "!!!---10秒后将以上面的配置启动服务器---!!!"
        sleep 10
        sh $installdir/run.sh
        ;;
    *)
        echo "如果需要修改服务器配置选项请自行修改，配置文件路径为${installdir}/server.properties"
	echo "服务器模组请存放在${installdir}/mods文件夹下"
        echo "后续需要启动服务器时请执行[cd ${installdir}/ && sh ${installdir}/run.sh]即可启动服务器，无需再次执行部署脚本"
        cd
        sleep 10
        exit 0
        ;;
esac
#check_xmx() { while [ "$check" != "02" ]; do read -p "输入游戏服务器允许使用的最大内存[单位GB 例如1GB则输入1]: " xmx;echo "";if [[ -z "$xmx" || ! "$xmx" =~ ^[0-9]+$ ]]; then echo "请输入有效的数字。";echo "";continue;fi; while [ "$xmx" -gt "$total_mem" ]; do echo "输入的内存值超过了本机可用内存。本机最大可用内存为 ${total_mem}GB，请重新输入。";echo "";read -p "输入游戏服务器允许使用的最大内存[单位GB 例如1GB则输入1]: " xmx;if [[ -z "$xmx" || ! "$xmx" =~ ^[0-9]+$ ]]; then echo "请输入有效的数字。";echo "";continue;fi; done; if [[ "$xmx" -gt 0 && "$xmx" -le "$total_mem" ]]; then check=02;fi; done; check=0;};check_xms() { while [ "$check" != "02" ]; do echo"";read -p "输入游戏服务器的初始启动内存[单位GB 例如1GB则输入1]: " xms;echo "";if [[ -z "$xms" || ! "$xms" =~ ^[0-9]+$ ]]; then echo "请输入有效的数字。";echo "";continue;fi; while [ "$xms" -gt "$xmx" ]; do echo "初始启动内存不能大于最大内存，请重新输入。";echo "";read -p "输入游戏服务器的初始启动内存[单位GB 例如1GB则输入1]: " xms;if [[ -z "$xms" || ! "$xms" =~ ^[0-9]+$ ]]; then echo "请输入有效的数字。";echo "";continue;fi; done; if [[ "$xms" -gt 0 && "$xms" -le "$xmx" ]]; then check=02;fi; done; check=0; };check_xmx;check_xms;cat << EOF > $installdir/run.sh
