#!/bin/bash

set -e

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # Clear Color

echo -e "${YELLOW}WARNING: This script will completely remove your Pterodactyl, NGINX Web-Server Installation & Wings Configuration!${NC}"
read -p "If you would like to proceed, respond with (Y)es or (N)o: " response

if [[ $response =~ ^[Yy]$ ]]; then
    echo -e "${GREEN}Proceeding with the removal.${NC}"
    echo -e "${YELLOW}Welcome to VS-Shop Official Pterodactyl Remover for Ubuntu 18.04${NC}\n"
    sleep 2

    echo -e "${GREEN}Removing Panel & its files...${NC}"
    sudo rm -rf /var/www/pterodactyl || true
    sleep 2

    echo -e "${GREEN}Removing queue worker...${NC}"
    sudo rm /etc/systemd/system/pteroq.service || true
    sleep 2

    echo -e "${GREEN}Removing NGINX Web-Server Configuration...${NC}"
    sudo unlink /etc/nginx/sites-enabled/pterodactyl.conf || true
    sleep 2

    echo -e "${GREEN}Stopping Wings...${NC}"
    sudo systemctl stop wings || true
    sleep 2

    echo -e "${GREEN}Removing Pterodactyl Game Files & Backups...${NC}"
    sudo rm -rf /var/lib/pterodactyl || true
    sleep 2

    echo -e "${GREEN}Removing Wings Config...${NC}"
    sudo rm -rf /etc/pterodactyl || true
    sleep 2

    echo -e "${GREEN}Removing Wings Binary...${NC}"
    sudo rm /usr/local/bin/wings || true
    sleep 2

    echo -e "${GREEN}Removing Wings Daemon Files...${NC}"
    sudo rm /etc/systemd/system/wings.service || true
    sleep 2

    echo -e "${GREEN}Removing Database...${NC}"
    mysql -u root -p <<MYSQL_SCRIPT
    SHOW DATABASES;
    DROP DATABASE panel;
    SELECT User, Host FROM mysql.user;
    DROP USER 'pterodactyl'@'127.0.0.1';
    exit
MYSQL_SCRIPT

    echo -e "${GREEN}Stopping NGINX Web-Server...${NC}"
    sudo systemctl stop nginx || true
    sleep 2

    echo -e "${GREEN}Removing NGINX Web-Server...${NC}"
    sudo apt purge nginx nginx-common || true
    sleep 1
    sudo apt autoremove || true
    sleep 2

    echo -e "${YELLOW}Pterodactyl has been removed successfully!${NC}"
else
    echo -e "${YELLOW}Pterodactyl Removing Process Aborted!${NC}"
fi