#!/bin/bash

echo -e "+ TEMP_DIR=$(mktemp -d)"
TEMP_DIR=$(mktemp -d)
echo -e "\e[36m○ Before installing Jexactyl SuperDaemon, you must know that this is BETA software. By using this script, you understand the risks of this software.\e[0m"
echo -e "\e[36m○ If you want to cancel, you can always Ctrl-C to quit.\e[0m"
echo -e "+ sleep 10"
sleep 10
if [[ ! -f /usr/local/bin/wings ]]; then
    echo -e "\e[31m● Pterodactyl Wings must be installed to install Jexactyl SuperDaemon. Download it here: https://pterodactyl.io/wings/1.0/installing.html. Exiting.\e[0m"
    exit 1
fi
if command -v go &>/dev/null; then
    echo -e "\033[92m● Go is already installed.\e[0"
else
    echo -e "\e[36m○ Installing Go...\e[0m"
    echo -e '+ curl -L "https://golang.org/dl/go1.23.0.linux-amd64.tar.gz" | tar -C "$TEMP_DIR" -xz'
    curl -L "https://golang.org/dl/go1.23.0.linux-amd64.tar.gz" | tar -C "$TEMP_DIR" -xz
    echo -e '+ export PATH="$TEMP_DIR/go/bin:$PATH"'
    export PATH="$TEMP_DIR/go/bin:$PATH"
    echo -e "\033[92m● Go successfully installed!\e[0m"
fi
echo -e "\e[36m○ Downloading Jexactyl SuperDaemon...\e[0m"
echo -e '+ git clone --branch develop https://github.com/Jexactyl/SuperDaemon.git "$TEMP_DIR/wings"'
git clone --branch develop https://github.com/Jexactyl/SuperDaemon.git "$TEMP_DIR/wings"
echo -e '+ cd "$TEMP_DIR/wings"'
cd "$TEMP_DIR/wings"
echo -e "\033[92m● Successfully downloaded Jexactyl SuperDaemon!\e[0m"
echo -e "\e[36m○ Building the binary and replacing Wings with SuperDaemon...\e[0m"
echo -e "+ go build"
go build
echo -e "+ sudo systemctl stop wings"
sudo systemctl stop wings
echo -e "+ sudo mv wings /usr/local/bin/"
sudo mv wings /usr/local/bin/
echo -e "+ sudo systemctl start wings"
sudo systemctl start wings
echo -e "\033[92m● Successfully installed Jexactyl SuperDaemon! Have fun!\e[0m"
exit 0