#!/bin/bash

set -e

RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
LIGHT_BLUE='\033[1;34m'
NC='\033[0m'

prompt() {
    local PROMPT_MESSAGE=$1
    local DEFAULT_VALUE=$2
    read -p "$PROMPT_MESSAGE [$DEFAULT_VALUE]: " INPUT
    echo "${INPUT:-$DEFAULT_VALUE}"
}

confirm_clean_install() {
    echo -e ${LIGHT_BLUE}"=================================================================${NC}"
    echo -e ${LIGHT_BLUE}"               Pterodactyl Wings Installer for Debian 12 by VS-Shop               ${NC}"
    echo -e ${LIGHT_BLUE}"=================================================================${NC}"
    echo -e "${YELLOW}This installation script requires a clean installation of Debian 12 (Bookworm).${NC}"
    echo -e "${YELLOW}Please confirm you are running it on a fresh system.${NC}"
    echo -e ${LIGHT_BLUE}"=================================================================${NC}"
    
    while true; do
        read -p "Is this a clean installation of Debian 12? (Y/N): " response
        case $response in
            [Yy]* ) break;;
            [Nn]* ) echo -e "${RED}Please reinstall Debian 12 and run the script again.${NC}"; exit;;
            * ) echo -e "${RED}Please answer with 'Y' (Yes) or 'N' (No).${NC}";;
        esac
    done
}

confirm_clean_install

echo -e "${GREEN}Updating system and installing dependencies...${NC}"
apt update && apt upgrade -y
apt install -y curl sudo gnupg lsb-release ca-certificates

echo -e "${GREEN}Installing Docker (from official source)...${NC}"
curl -fsSL https://get.docker.com | bash

echo -e "${GREEN}Enabling Docker...${NC}"
systemctl enable --now docker

echo -e "${GREEN}Installing Wings...${NC}"
mkdir -p /etc/pterodactyl
ARCH=$(uname -m)
ARCH_TYPE=$([[ "$ARCH" == "x86_64" ]] && echo "amd64" || echo "arm64")

curl -L -o /usr/local/bin/wings "https://github.com/pterodactyl/wings/releases/latest/download/wings_linux_${ARCH_TYPE}"
chmod +x /usr/local/bin/wings

echo -e "${GREEN}Creating systemd service file for Wings...${NC}"
cat > /etc/systemd/system/wings.service <<EOF
[Unit]
Description=Pterodactyl Wings Daemon
After=docker.service
Requires=docker.service

[Service]
User=root
WorkingDirectory=/etc/pterodactyl
LimitNOFILE=4096
ExecStart=/usr/local/bin/wings
Restart=on-failure
StartLimitInterval=180
StartLimitBurst=30
RestartSec=5s

[Install]
WantedBy=multi-user.target
EOF

echo -e "${GREEN}Enabling Wings service...${NC}"
systemctl enable --now wings

confirm_wings_configuration() {
    echo -e ${LIGHT_BLUE}"=================================================================${NC}"
    echo -e ${LIGHT_BLUE}"                Pterodactyl Wings Configuration Reminder                 ${NC}"
    echo -e ${LIGHT_BLUE}"=================================================================${NC}"
    echo -e "${YELLOW}1. Log in to your Pterodactyl Panel and create a Location and a Node.${NC}"
    echo -e "${YELLOW}2. Navigate to Nodes > Your Node > Configuration tab.${NC}"
    echo -e "${YELLOW}3. Download or copy the config and paste it to: /etc/pterodactyl/config.yml${NC}"
    echo -e "${YELLOW}4. Save and come back to continue.${NC}"
    echo -e ${LIGHT_BLUE}"=================================================================${NC}"

    while true; do
        read -p "Have you completed the Node Configuration? (Y/N): " response
        case $response in
            [Yy]* ) break;;
            [Nn]* ) echo -e "${RED}Please complete the Node Configuration and run the script again.${NC}"; exit;;
            * ) echo -e "${RED}Please answer with 'Y' (Yes) or 'N' (No).${NC}";;
        esac
    done
}

confirm_wings_configuration

echo -e "${GREEN}Pterodactyl Wings Configuration completed successfully!${NC}"
echo -e "${GREEN}Pterodactyl Wings is now installed and running on Debian 12.${NC}"
