#!/bin/bash

# Versions: 
#   Install script: 1.7.0
#   Lastest change: Tweaked stuff to install things in the correct place (Hi XDG USER DIRS)
#   Launcher: 2023.06

# Colors
RED='\033[0;31m'
LIGHT_RED='\033[1;31m'
GREEN='\033[0;32m'
LIGHT_GREEN='\033[1;32m'
ORANGE='\033[0;33m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
LIGHT_BLUE='\033[1;34m'
PURPLE='\033[0;35m'
LIGHT_PURPLE='\033[1;35m'
CYAN='\033[0;36m'
LIGHT_CYAN='\033[1;36m'
WHITE='\033[1;37m'

# Check if said folder exist, if not, create it
if [ ! -d ~/.local/share/applications ]; then
    mkdir -p ~/.local/share/applications
fi
if [ ! -d ~/.local/share/icons ]; then
    mkdir -p ~/.local/share/icons
fi
if [ ! -d ~/.backtobeta ]; then
    mkdir ~/.backtobeta/
fi
if [ ! -d ~/.backtobeta/tmp ]; then
    mkdir ~/.backtobeta/tmp/
fi
if [ ! -d ~/.backtobeta/Launcher ]; then
    mkdir ~/.backtobeta/Launcher/
fi

# Download java JRE 1.8 & Extract it
echo -e "${LIGHT_BLUE}Downloading java...${BLUE}"
if [ -d ~/.backtobeta/Launcher/java ]; then
    # EDIT THIS LINE ONLY IF YOU KNOW WHAT YOU'RE DOING !
    rm -rf ~/.backtobeta/Launcher/java
fi

mkdir ~/.backtobeta/Launcher/java
curl -o ~/.backtobeta/tmp/java.tar.gz https://btb.krafting.net/dl/data/java.tar.gz
tar -xf ~/.backtobeta/tmp/java.tar.gz 
mv -fvu openlogic-openjdk-jre-8u372-b07-linux-x64/* ~/.backtobeta/Launcher/java
rm -rf openlogic-openjdk-jre-8u372-b07-linux-x64/

echo -e "${YELLOW}Downloading the launcher and adding a Menu entry...${ORANGE}"

# Download the launcher itself
curl -o ~/.backtobeta/tmp/LauncherBTB.jar https://btb.krafting.net/dl/data/LauncherBTB.jar
mv -fv ~/.backtobeta/tmp/LauncherBTB.jar ~/.backtobeta/Launcher/

# Download the uninstall script
curl -o ~/.backtobeta/tmp/uninstall.sh https://btb.krafting.net/dl/data/uninstall.sh
mv -fv ~/.backtobeta/tmp/uninstall.sh ~/.backtobeta/Launcher/

# Download the .desktop file & icon and move it on the ~/.local/share/applications/
curl -o ~/.backtobeta/tmp/btb.png https://btb.krafting.net/dl/data/logo.png
mv -fv ~/.backtobeta/tmp/btb.png ~/.local/share/icons/

# Downloading & Moving of the menu entry
curl -o ~/.backtobeta/tmp/BackToBeta.desktop https://btb.krafting.net/dl/data/BTB.desktop
mv -fv ~/.backtobeta/tmp/BackToBeta.desktop ~/.local/share/applications/
printf "\nIcon=$HOME/.local/share/icons/btb.png" >> ~/.local/share/applications/BackToBeta.desktop

# Create the launch script for better control of the .desktop
echo "~/.backtobeta/Launcher/java/bin/java -jar ~/.backtobeta/Launcher/LauncherBTB.jar" > ~/.backtobeta/Launcher/launch.sh
chmod +x ~/.backtobeta/Launcher/launch.sh
chmod +x ~/.backtobeta/Launcher/LauncherBTB.jar
chmod +x ~/.local/share/applications/BackToBeta.desktop

if [ -d ~/.backtobeta/tmp ]; then
    rm -rf ~/.backtobeta/tmp/
fi
echo ""
echo ""
echo -e "${LIGHT_GREEN}Back To Beta a été installé avec succès. Vous pouvez le lancer depuis votre menu d'applications."
echo ""
echo ""
