#!/bin/bash

# Versions: 
# Install script: 1.5.3
# Lastest change: Remove installation of cURL, pretend it is installed by default + Rename java folder for easier upgrades + COLORS!
# Launcher: 2021.12

# 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 ~/.icons ]; then
    mkdir ~/.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://www.krafting.net/0/backtobeta/dl/data/java.tar.gz
tar -xf ~/.backtobeta/tmp/java.tar.gz 
mv -fvu openlogic-openjdk-jre-8u292-b10-linux-x64/* ~/.backtobeta/Launcher/java
rm -rf openlogic-openjdk-jre-8u292-b10-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://www.krafting.net/0/backtobeta/dl/data/LauncherBTB.jar
mv -fv ~/.backtobeta/tmp/LauncherBTB.jar ~/.backtobeta/Launcher/

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

# Downloading & Moving of the menu entry
curl -o ~/.backtobeta/tmp/BackToBeta.desktop https://www.krafting.net/0/backtobeta/dl/data/BTB.desktop
mv -fv ~/.backtobeta/tmp/BackToBeta.desktop ~/.local/share/applications/
printf "\nIcon=$HOME/.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 ""