#!/usr/bin/bash
echo "NOTE: Because this script installs components of x11 there may be some minor changes to your OSes UI! (like changed button designs)"
sleep 3
echo "This script downloads the game and installs every needed module to compile it, then compiles it and runs it. If you don't want to do this or had the script running successfully before press Ctrl+C now to cancel this script. This script will continue in 10 seconds."



mkdir $HOME/.cache &> /dev/null
cat << '**ENDOFFILE**' > $HOME/.cache/countdownfrom10.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
try:
        import time as t
        print("\r10 [==========]", end="\r")
        t.sleep(1)
        print("\r09 [=========-]", end="\r")
        t.sleep(1)
        print("\r08 [========--]", end="\r")
        t.sleep(1)
        print("\r07 [=======---]", end="\r")
        t.sleep(1)
        print("\r06 [======----]", end="\r")
        t.sleep(1)
        print("\r05 [=====-----]", end="\r")
        t.sleep(1)
        print("\r04 [====------]", end="\r")
        t.sleep(1)
        print("\r03 [===-------]", end="\r")
        t.sleep(1)
        print("\r02 [==--------]", end="\r")
        t.sleep(1)
        print("\r01 [=---------]", end="\r")
        t.sleep(1)
        print("\r00 [----------]", end="\r")
        print("\r__ [ EXPIRED! ]", end="\r")
except KeyboardInterrupt:
        print("\rKBD[INTERRUPT!]", end="\r")
except not KeyboardInterrupt:
        print("\r__ [  ERROR!  ]", end="\r")
**ENDOFFILE**

python3 $HOME/.cache/countdownfrom10.py



echo "Updating system..."
sudo apt update -y
sudo apt upgrade -y

echo "Updating/installing git..."
sleep 0.5
sudo apt install git -y

echo "Starting download using git..."
sleep 0.5

git clone --recurse-submodules https://github.com/jdah/minecraft-weekend.git

echo "Opening directory..."
sleep 0.5

cd minecraft-weekend

echo "Installing other needed packages..."
sleep 0.5

sudo apt install make -y
sudo apt install cmake -y
sudo apt install clang -y
sudo apt install libx11-dev -y
sudo apt install libxrandr-dev -y
sudo apt install libxinerama-dev -y
sudo apt install libxcursor-dev -y
sudo apt install libxi-dev -y

echo "Starting to compile: Compiling librarys..."
sleep 0.5

make --ignore-errors libs

echo "Starting to compile: Compiling the game itself..."
sleep 0.5

make --ignore-errors

echo "Creating script for making launching easier later..."
sleep 0.5

cd ../

cat << '**ENDOFFILE**' > run-mc-weekend.sh
prev_path=${pwd}
cd $HOME/minecraft-weekend
bin/game
cd $prev_path
**ENDOFFILE**

chmod +x run-mc-weekend.sh
cd minecraft-weekend

echo "Now let's try to run the game!"
sleep 0.5

bin/game
if [ $? == 127 ];
then
    echo "The game isn't there? Exiting..."
    python3 $HOME/.cache/countdownfrom10.py
    kill -INT $$
fi

echo "Game closed."
sleep 1
echo "To run the game again, simply run 'bin/game' in ~/minecraft-weekend or run './run-mc-weekend.sh' in your home directory (~/)."
sleep 1
echo "If 'run-mc-weekend.sh' is missing, execute 'wget https://dateien.lampe2020.de/dateien/run-mc-weekend.sh' or download the script from that location using a web browser."
sleep 3

echo "Ending script in 10 seconds..."

python3 $HOME/.cache/countdownfrom10.py

rm $HOME/.cache/countdownfrom10.py
