#!/bin/bash

set -e

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

LOG_FILE="/var/log/pterodactyl-remover.log"
REMOVED_COUNT=0

count_entries() {
    local target="$1"
    if [[ -d "$target" && ! -L "$target" ]]; then
        sudo find "$target" 2>/dev/null | wc -l
    elif [[ -e "$target" || -L "$target" ]]; then
        echo 1
    else
        echo 0
    fi
}

remove_path() {
    local target="$1"
    local count
    count=$(count_entries "$target")
    sudo rm -rf "$target" || true
    REMOVED_COUNT=$((REMOVED_COUNT + count))
}

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}"
    remove_path /var/www/pterodactyl
    sleep 2

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

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

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

    echo -e "${GREEN}Removing Pterodactyl Game Files & Backups...${NC}"
    remove_path /var/lib/pterodactyl
    sleep 2

    echo -e "${GREEN}Removing Wings Config...${NC}"
    remove_path /etc/pterodactyl
    sleep 2

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

    echo -e "${GREEN}Removing Wings Daemon Files...${NC}"
    remove_path /etc/systemd/system/wings.service
    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}"
    # Count the regular files that the NGINX packages installed, before purging
    NGINX_COUNT=$(dpkg -L nginx nginx-common 2>/dev/null | while read -r f; do [ -f "$f" ] && echo "$f"; done | wc -l)
    REMOVED_COUNT=$((REMOVED_COUNT + NGINX_COUNT))
    sudo apt purge nginx nginx-common || true
    sleep 1
    sudo apt autoremove || true
    sleep 2

    echo -e "${YELLOW}Pterodactyl has been removed successfully!${NC}"

    # Build the summary and broadcast it
    SUMMARY="Pterodactyl removal finished on $(hostname) at $(date '+%F %T'): ${REMOVED_COUNT} files/folders removed."
    echo -e "${GREEN}${SUMMARY}${NC}"

    echo "$SUMMARY" | sudo tee -a "$LOG_FILE" >/dev/null || true
    sudo logger -t pterodactyl-remover "$SUMMARY" || true
    echo "$SUMMARY" | sudo wall || true
else
    echo -e "${YELLOW}Pterodactyl Removing Process Aborted!${NC}"
fi
