#!/bin/bash
recover_backup=YES
backup_file="/Cloud/Backup/Minecraft/Minecraft_ubuntu.tar.xz"

function print_centered {
     [[ $# == 0 ]] && return 1

     declare -i TERM_COLS="$(tput cols)"
     declare -i str_len="${#1}"
     [[ $str_len -ge $TERM_COLS ]] && {
          echo "$1";
          return 0;
     }

     declare -i filler_len="$(( (TERM_COLS - str_len) / 2 ))"
     [[ $# -ge 2 ]] && ch="${2:0:1}" || ch=" "
     filler=""
     for (( i = 0; i < filler_len; i++ )); do
          filler="${filler}${ch}"
     done

     printf "%s%s%s" "$filler" "$1" "$filler"
     [[ $(( (TERM_COLS - str_len) % 2 )) -ne 0 ]] && printf "%s" "${ch}"
     printf "\n"

     return 0
}

function backup_tool {
echo -e "\n$(tput setaf 10)Backup tool:$(tput setaf 7)"
if [ -f /tmp/mcbackup.sh ];then rm /tmp/mcbackup.sh;fi
cat <<'EOF'>> /tmp/mcbackup.sh
#!/bin/bash
####################################
#
# Backup minecraft world to a
# specified folder.
#
####################################
set -e
#sudo systemctl stop watchdog >/dev/null
version=3.0
#day_raw=$(date +%A_%d_%m_%y)
#archive_file="minecraft_ubuntu_$day_raw"
archive_file="Minecraft_ubuntu"
output_folder="/Cloud/Backup/Minecraft"

echo -e "\e[95mMinecraft auto-backup version $version\e[0m\n"

function backup_ {
echo "Backup/Compress Files:"
cd ~
if [ -d temp_arc ]
then
rm -r -f temp_arc
fi
mkdir temp_arc
cd temp_arc
cp -r -f ~/Minecraft/world* .
cp -r -f ~/Minecraft/ops.json .
cp -r -f ~/Minecraft/permissions.yml .
cp -r -f ~/Minecraft/server.properties .
cp -r -f ~/Minecraft/usercache.json .
cp -r -f ~/Minecraft/whitelist.json .
tar cf - * --checkpoint=.100 | pixz > $archive_file.tar.xz
echo ""
md5sum $archive_file.tar.xz > $archive_file.md5
cp $archive_file.tar.xz $output_folder
cp $archive_file.md5 $output_folder
}

function Upload_freebox {
echo -e "\n\e[97mUpload on Freebox:\e[0m"
echo "- Mounting Nas -"
sudo mount -t cifs //192.168.1.254/Disque\ dur/  /media/Nas/ -o user=freebox,password=password,uid=1000,gid=1000,rw,vers=1.0
sleep 1
sudo cp -v $archive_file.tar.xz /media/Nas/mc_backup
sudo cp -v $archive_file.md5 /media/Nas/mc_backup
sleep 1
sudo umount /media/Nas
echo "- Unmounting Nas -"
echo ""
}

function Upload_ftp {
echo -e "\e[97mUpload on FTP:\e[0m"
ncftpput -v -u "wareck" -p "zorn692611" ftpperso.free.fr minecraft_bkp/ $archive_file.tar.xz
sleep 1
ncftpput -v -u "wareck" -p "zorn692611" ftpperso.free.fr minecraft_bkp/ $archive_file.md5
sleep 1
}

backup_
Upload_freebox
Upload_ftp
EOF
sudo chmod +x /tmp/mcbackup.sh
sudo cp /tmp/mcbackup.sh /usr/local/bin/
echo "Done"
}

function recovery {
if [ $recover_backup = "YES" ]
then
echo -e "\n$(tput setaf 10)Restore Backup:$(tput setaf 7)"
if [ -r /home/wareck/temp_arc ]
then
rm -r /home/wareck/temp_arc
fi
mkdir /home/wareck/temp_arc
cd /home/wareck/temp_arc
cp $backup_file .
pixz -x < $backup_file | tar xf - --checkpoint=.300
echo ""
rm Mine*.tar.xz
cp -r /home/wareck/temp_arc/* /home/wareck/Minecraft/
sudo rm -r /home/wareck/temp_arc
fi
echo -e
}

print_centered "$(tput setaf 10)"
print_centered " ⠀⠀⡰⠉⠉⣷⡄⣀⣎⠉⢹⡎⠉⢉⡞⠉⠉⣧⡎⠉⠉⡞⠉⠉⠉⠉⠉⣿⠉⠉⠉⠉⠉⢹⡏⠋⠉⠉⠉⠙⡏⠉⣉⠉⣉⠉⢿⠉⠉⠉⢉⠉⢱⡏⠉⠉⠉⢉⠉⡄"
print_centered "⠀⢀⠃⠀⠀⠘⠛⠁⠀⠀⣼⠀⠀⣸⠃⠀⠀⠘⠃⠀⢰⡇⠀⠀⠛⠛⠛⣿⠀⠀⢸⣿⣿⣿⡇⠀⠈⠃⠀⣠⣿⠀⠉⣶⣍⠀⢸⡆⠀⠘⠛⠛⠛⣿⣿⡆⠀⠈⡟⠁"
print_centered "⠀⡜⣀⢀⣶⣀⣰⠆⠀⢀⡏⠀⠀⣿⠀⠀⣸⣄⠤⠄⢸⠃⠀⢰⣶⣶⣶⣿⠀⠀⢸⣉⣉⣉⡇⡀⠀⣶⠀⠀⢹⠀⠀⢀⡀⢀⠀⣇⠀⠀⢶⣶⣶⢾⣿⣿⡀⠀⢱⠀"
print_centered "⢰⠀⠀⢸⣿⣦⠈⠀⠀⢼⠁⠀⢰⡇⠀⠀⣿⣿⠀⠀⢼⠀⡄⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⢸⡇⠂⠀⣿⠀⠀⢸⡆⠀⢘⡧⠀⠀⣿⠀⠀⢸⠛⠃⠘⣿⣿⡇⠀⠀⡆"
print_centered "⠀⠑⢤⣀⣙⡿⠉⠱⣄⣠⣷⣄⣀⣹⣄⣀⣘⡏⢆⣀⣘⣧⣀⣀⣀⣀⣀⣿⣀⣀⣀⣀⣀⣸⣇⣀⣰⣏⣀⣠⣿⣀⣀⣾⣁⣀⣴⣋⣀⡠⠃⠀⠀⠀⢿⣋⣀⡤⠋⠀"
print_centered "$(tput setaf 7)"
print_centered "Minecraft Ubuntu Server Builder/install v2.1"
print_centered "$(tput setaf 7)"
sleep 2

echo -e "$(tput setaf 10)Installation java:$(tput setaf 7)"
if ! [ -x "$(command -v java)" ]
then
sudo apt-get update
sudo apt-get install curl wget git ncftp -y
if ! [ -f openjdk-18_linux-x64_bin.tar.gz ]
then
echo -e
curl -O https://download.java.net/java/GA/jdk18/43f95e8614114aeaa8e8a5fcf20a682d/36/GPL/openjdk-18_linux-x64_bin.tar.gz
fi
cat <<'EOF'>> openjdk-18_linux-x64_bin.tar.gz.md5
f3fc42bd35c3f5e9aff967c9862a153a  openjdk-18_linux-x64_bin.tar.gz
EOF
if md5sum -c openjdk-18_linux-x64_bin.tar.gz.md5
then
tar xf openjdk-18_linux-x64_bin.tar.gz --checkpoint=.300
echo -e
rm openjdk-18_linux-x64_bin.tar.gz
rm openjdk-18_linux-x64_bin.tar.gz.md5
else
echo "erreur"
fi
if [ -d /opt/jdk-18 ]
then
sudo rm -r /opt/jdk-18/
fi
sudo mv jdk-18 /opt/
if  [ -f /etc/profile.d/jdk18.sh ]
then
sudo rm /etc/profile.d/jdk18.sh
fi
sudo tee /etc/profile.d/jdk18.sh <<EOF
export JAVA_HOME=/opt/jdk-18
export PATH=\$PATH:\$JAVA_HOME/bin
EOF
source /etc/profile.d/jdk18.sh
fi
version=$(java -version 2>&1 | head -1 | cut -d'"' -f2 | sed '/^1\./s///' | cut -d'.' -f1)
if [ $version -ge 18 ]
then
echo -e "$(tput setaf 6)Java version $version =>> OK$(tput setaf 7)"
else
echo "error !"
exit 1
fi

echo -e "\n$(tput setaf 10)Download/install mcrcon:$(tput setaf 7)"
cd /home/wareck/
git clone https://github.com/Tiiffi/mcrcon.git
cd /home/wareck/mcrcon
make
sudo make install
cd ..
rm -r -f /home/wareck/mcrcon

echo -e "$(tput setaf 10)Download Minecraft server structure:$(tput setaf 7)"
cd /home/wareck/
wget -c http://folivier.homelinux.org/cube/pack/minecraft.tar.xz
tar xfJ minecraft.tar.xz
rm minecraft.tar.xz
cp -r minecraft Minecraft
rm -r -f minecraft
cd Minecraft
rsync --append --progress --recursive * /home/wareck/Minecraft/


echo -e "\n$(tput setaf 10)Download BuildTools:$(tput setaf 7)"
if ! [ -d build ]; then mkdir build ;fi
cd build
wget -c https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar

echo -e "\n$(tput setaf 10)Building Spigot:$(tput setaf 7)"
java -jar BuildTools.jar
cp spigot* /home/wareck/Minecraft/

echo -e "\n$(tput setaf 10)minecraft.service file build/install:$(tput setaf 7)"

if [ -f minecraft.service ];then rm minecraft.service;fi

version=`ls -a /home/wareck/Minecraft/spigot*.jar | sed "s/\/home\/wareck\/Minecraft\///g"`
short_v=`echo $version | sed "s/spigot-//g" | sed "s/.jar//g"`

echo "Version builed : $version"

cat <<'EOF'>> minecraft.service
#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          minecraft
# Required-Start:    networking
# Default-Start:     3 4 5
# Default-Stop:      0 6
### END INIT INFO
cd /home/wareck/Minecraft/
su wareck -c "screen -dmS minecraft /opt/jdk-18/bin/java -jar -Xmx2048M -Xms2048M XXXX nogui"
exit 0
EOF
sed -i "s/XXXX/$version/g" minecraft.service
sed -i "s/XXXX/$version/g" /home/wareck/Minecraft/start.sh
sed -i "s/XXXX/$short_v/g" /home/wareck/Minecraft/server.properties
more minecraft.service

chmod +x minecraft.service
sudo cp minecraft.service /etc/init.d/
sudo sudo update-rc.d minecraft.service defaults

backup_tool
recovery
echo -e "\nDone"
