#!/bin/bash

# Check if user is running as root
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root."
   exit 1
fi

echo -e "\033[33m"
cat << "EOF"
  _____        __  __          _____                 _
 |  __ \      / _|/ _|        |  __ \               | |
 | |__) |   _| |_| |_ ___ _ __| |__) |_ _ _ __   ___| |
 |  ___/ | | |  _|  _/ _ \ '__|  ___/ _` | '_ \ / _ \ |
 | |   | |_| | | | ||  __/ |  | |  | (_| | | | |  __/ |
 |_|    \__,_|_| |_| \___|_|  |_|   \__,_|_| |_|\___|_|
EOF
echo -e "\033[0m"

# Prompt user to choose an option
echo "╔══════════════════════════════╗"
echo "║   Please choose an option:   ║"
echo "║ 1. Install PufferPanel       ║"
echo "║ 2. Support                   ║"
echo "║ 3. Remove Pufferpanel        ║"
echo "║ 4. Quit                      ║"
echo "╚══════════════════════════════╝"
read -p "Enter your choice (1 or 4): " choice

# Check user's choice and execute corresponding actions
case "$choice" in
    1)
        # Install PufferPanel
        ;;
    2)
        echo "Need help? Contact me Via discord Pringels.dc or email contact@pringelsdc.de"

        exit 1
        ;;
     3) echo "removing pufferpanel"
         apt purge Pufferpanel       
        exit 1
        ;;
     4) echo "Leaving Script..."
        exit 1
        ;;
     
    *)
        echo "Invalid choice. Please choose either 1 or 4."
        exit 1
        ;;
esac

# Check the operating system and Architecture 

echo "Checking os and Architecture"

if [[ $(uname) == "Linux" ]]; then
  os=$(lsb_release -is)

  case $os in
    Ubuntu)
      version=$(lsb_release -rs)
      if [[ $version == "20.04" || $version == "22.04" ]]; then
        echo "Running on Ubuntu $version..."
        machine=$(uname -m)
        if [[ $machine == "x86_64" ]]; then
          # Add Ubuntu-specific commands for 64-bit here
          echo "64-bit Ubuntu detected."
        else
          echo "Unsupported architecture for Ubuntu!"
          exit 1
        fi
      else
        echo "Unsupported Ubuntu version!"
        exit 1
      fi
      ;;
    Debian)
      version=$(lsb_release -rs)
      if [[ $version == "10" || $version == "11" ]]; then
        echo "Running on Debian $version..."
        machine=$(uname -m)
        if [[ $machine == "x86_64" ]]; then
          # Add Debian-specific commands for 64-bit here
          echo "64-bit Debian detected."
        else
          echo "Unsupported architecture for Debian!"
          exit 1
        fi
      else
        echo "Unsupported Debian version!"
        exit 1
      fi
      ;;
    Raspbian)
      version=$(lsb_release -rs)
      if [[ $version == "10" && $(uname -m) == "armv7l" ]]; then
        echo "Running on Raspbian Buster..."
        # Add Raspbian-specific commands for 32-bit here
        echo "32-bit Raspbian Buster detected."
      elif [[ $version == "11" ]]; then
        machine=$(uname -m)
        if [[ $machine == "x86_64" ]]; then
          echo "64-bit Raspbian detected."
          # Add Raspbian-specific commands for 64-bit here
        elif [[ $machine == "armv7l" ]]; then
          echo "32-bit Raspbian Bullseye detected."
          # Add Raspbian-specific commands for 32-bit here
        else
          echo "Unsupported architecture for Raspbian!"
          exit 1
        fi
      else
        echo "Unsupported Raspbian version!"
        exit 1
      fi
      ;;
    *)
      echo "Unsupported operating system!"
      exit 1
      ;;
  esac

else
  echo "This script can only be run on Linux!"
  exit 1
fi

     
                        
# Print welcome message
echo "##################################
Pufferpanel Installer
this script is not associated with pufferpanel if you need help contact me via discord Pringels.dc
##################################"
sleep 7

clear

# install packages
echo "installing packages"
sleep 5
curl -s https://packagecloud.io/install/repositories/pufferpanel/pufferpanel/script.deb.sh | sudo bash

clear

# Download PufferPanel
echo "installing pufferpanel"
sleep 3
sudo apt-get install pufferpanel

clear

# enable Panel panel 
echo "enabling the panel"
sleep 3
sudo systemctl enable pufferpanel

# Set up the admin account
echo "Now we need a admin Account" 
sleep 5
 sudo pufferpanel user add

# Start Panel
echo "Starting the panel"
sleep 3
sudo systemctl enable --now pufferpanel
sleep 5
echo "PufferPanel installation complete!"
          	              
# Get the IP address of the server
IP=$(ip addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v 127.0.0.1)

# Display the IP address and panel access information
echo "Your server's IP address is: $IP"
echo "Your panel can be accessed at: http://$IP:8080/"
