#!/bin/bash
 printf '%*s\n' "$(tput cols)" '' | tr ' ' '#' 
echo "Minecraft Server management Installation - start"
 printf '%*s\n' "$(tput cols)" '' | tr ' ' '#' 

#--------------------DEPENDENCIES---------------------------------

#sudo apt update
#sudo apt install screen -y
#sudo apt install openjdk-25-jre -y

#--------------------Configurabilities---------------------------------

echo " "
echo "Name des Servers:"
read server_name

server_id=$(date +%Y)

printf '%*s\n' "$(tput cols)" '' | tr ' ' '-' 

server_path=$(pwd)
echo "server_path: $server_path"

server_archiv_path="$server_path/server_archive"
echo "server_archiv_path: $server_archiv_path"

server_plugins_path="$server_path/static_plugins"
echo "server_plugins_path: $server_plugins_path"

server_datapacks_path="$server_path/static_datapacks"
echo "server_datapacks_path: $server_datapacks_path"

printf '%*s\n' "$(tput cols)" '' | tr ' ' '-' 

echo "Server Reinstall Tag (Day of Month) bsp: 14 (14. Tag im Monat)"
read server_reinstall_dayofmonth

printf '%*s\n' "$(tput cols)" '' | tr ' ' '-'

echo "Server Reinstall Monat bsp: 8 (August)"
read server_reinstall_month

echo "Server Reinstall Uhrzeit (0-23)"
read server_reinstall_time

printf '%*s\n' "$(tput cols)" '' | tr ' ' '-' 

echo "Serversoftware (Verfügbar: [vanilla] [purpur])"
read serversoftware

printf '%*s\n' "$(tput cols)" '' | tr ' ' '-' 

echo "RAM in GB"
read ram

printf '%*s\n' "$(tput cols)" '' | tr ' ' '-' 

echo "Discord Webhook URL"
read discordwebhook

printf '%*s\n' "$(tput cols)" '' | tr ' ' '-' 
printf '%*s\n' "$(tput cols)" '' | tr ' ' '#' 
echo "Server Name: $server_name"
echo "Server ID: $server_id"
echo "Reinstall Datum: "$server_reinstall_dayofmonth"".""$server_reinstall_month""
echo "Serversoftware: "$serversoftware""
echo "RAM: "$ram"GB"
printf '%*s\n' "$(tput cols)" '' | tr ' ' '#' 
echo "Sind diese Informationen korrekt? [ENTER] weiter"
read enter

#----------------------------------INSTALLER------------------------------
sleep 1

echo "[Install-Info]: Ordner werden erstellt..."
cd $server_path
mkdir "$server_name"
mkdir "$server_archiv_path"
mkdir "$server_plugins_path"
mkdir "$server_datapacks_path"
echo "[Install-Info]: Ordner wurden erfolgreich erstellt."

echo "[Install-Info]: server_name wird gespeichert"
echo "$server_name" > server_name
echo "[Install-Info]: server_name wurde gespeichert"

echo "[Install-Info]: ram allocation wird gespeichert"
echo "$ram" > ram
echo "[Install-Info]: ram allocation wurde gespeichert"

echo "[Install-Info]: server id wird gespeichert"
date +%Y > server_id
echo "[Install-Info]: server id wurde gespeichert"

version=$(curl https://files.hummelsvrs.de/minecraft/latest-version.txt)
echo "Version: $version"

printf '%*s\n' "$(tput cols)" '' | tr ' ' '-' 
echo "difficulty: (peaceful / easy / normal / hard)"
read difficulty
printf '%*s\n' "$(tput cols)" '' | tr ' ' '-' 
echo "max-players:"
read playercount
printf '%*s\n' "$(tput cols)" '' | tr ' ' '-' 
echo "simulation-distance:"
read simulation_distance
printf '%*s\n' "$(tput cols)" '' | tr ' ' '-' 
echo "view-distance:"
read render_distance
printf '%*s\n' "$(tput cols)" '' | tr ' ' '-' 
echo "spawn-protection:"
read spawn_protection

echo "
#Server Properties
difficulty=$difficulty
max-players=$playercount
simulation-distance=$simulation_distance
view-distance=$render_distance
spawn-protection=$spawn_protection
white-list=true
"

echo "
#Server Properties
difficulty=$difficulty
max-players=$playercount
simulation-distance=$simulation_distance
view-distance=$render_distance
spawn-protection=$spawn_protection
white-list=true
" > server.properties

printf '%*s\n' "$(tput cols)" '' | tr ' ' '-' 

echo "Minecraft EULA: https://aka.ms/MinecraftEULA"
echo "EULA Akzeptieren? schreibe [true]"
read EULA

date=$(date)
echo "
#$date
eula=$EULA" > eula.txt

#-----------------------SERVER INSTALL---------------------
echo "[Install-Info]: Server Installation"
cd "$server_name"

echo "[Install-Info]: Get server.properties"
cp ../server.properties server.properties
echo "motd=§2$server_name - $server_id" >> server.properties
echo "[Install-Info]: Get eula.txt"
cp ../eula.txt eula.txt
echo "[Install-Info]: Get ram info"
cp ../ram ram
echo "[Install-Info]: Get server name"
cp ../server_name server_name
echo "[Install-Info]: Downloading server.jar for version $version"
wget -O server.jar "https://files.hummelsvrs.de/minecraft/mcsvr/$serversoftware/$version.jar"

echo "[Install-Info]: Generating start.sh file"
echo '#!/bin/bash
ram_number=$(cat ram)
ram=""$ram_number""G""
server_name=$(cat server_name)

screen -dmS "$server_name" bash -c "java -Xmx$ram -jar server.jar nogui"
' > start.sh

chmod +x start.sh

#-------------------------------Minecraft Server Manager Config---------------------------
cd "$server_path"
echo "[Install-Info]: Erstelle Konfigurationsdatei"
echo "#Konfigurationsdatei Minecraft Server Manager
#Erstellt am $date

server_name=""$server_name""
server_path=""$server_path""
server_archive_path=""$server_archiv_path""
server_plugins_path=""$server_plugins_path""
server_datapacks_path=""$server_datapacks_path""



server_reinstall_dayofmonth=""$server_reinstall_dayofmonth""
server_reinstall_month=""$server_reinstall_month""

ram="""$ram""G"""
serversoftware=""$serversoftware""

discordwebhook=""$discordwebhook""


" > config.sh

echo 'server_id=$(cat "server_id")
previous_server_id=$(($server_id - 1))
version=$(curl https://files.hummelsvrs.de/minecraft/latest-version.txt)'>>config.sh

echo "[Install-Info]: Konfigurationsdatei erstellt"
echo "[Install-Info]: Sende Discord Testnachricht"
if [ -n "$discordwebhook" ]; then
    curl -H "Content-Type: application/json" \
         -d "{\"content\":\"Dieser Bot wurde erfolgreich zum Minecraft Server Manager hinzugefügt!\"}" \
         "$discordwebhook"
fi

echo "[Install-Info]: Lade Minecraft Server Manager herunter:"
wget "https://files.hummelsvrs.de/minecraft/server-manager/minecraft_server_manager.sh"


#------------------------------------Crontab Autostart / Autocheck--------------------------------------

echo "[Install-Info]: Konfiguriere Autostart:"

(crontab -l 2>/dev/null; echo "@reboot cd "$server_path/$server_name" && bash start.sh") | crontab -
(crontab -l 2>/dev/null; echo "0 $server_reinstall_time * * * cd "$server_path" && bash "minecraft_server_manager.sh" auto") | crontab -

echo "[Install-Info]: Autostart Konfiguriert"

printf '%*s\n' "$(tput cols)" '' | tr ' ' '#' 

echo "[Install-Info]: Installation abgeschlossen!"
echo "[Install-Info]: Viel Spaß mit dem Minecraft Server Manager!"
rm minecraft_server_management_installation.sh