#!/bin/bash

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
NC='\033[0m' # No Color

# Animation function
function animate() {
    local pid=$1
    local delay=0.15
    local spinstr='|/-\'
    while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
        local temp=${spinstr#?}
        printf " [%c]  " "$spinstr"
        local spinstr=$temp${spinstr%"$temp"}
        sleep $delay
        printf "\b\b\b\b\b\b"
    done
    printf "    \b\b\b\b"
}

# Header with Hokage VPN banner
function header() {
    clear
    echo -e "${PURPLE}"
  echo -e "\033[1;31m"  # Red color
    echo " ██╗  ██╗ ██████╗ ██╗  ██╗ █████╗  ██████╗ ███████╗"
    echo " ██║  ██║██╔═══██╗██║  ██║██╔══██╗██╔════╝ ██╔════╝"
    echo " ███████║██║   ██║███████║███████║██║  ███╗█████╗  "
    echo " ██╔══██║██║   ██║██╔══██║██╔══██║██║   ██║██╔══╝  "
    echo " ██║  ██║╚██████╔╝██║  ██║██║  ██║╚██████╔╝███████╗"
    echo " ╚═╝  ╚═╝ ╚═════╝ ╚═╝  ╚═╝╚═╝  ╚═╝ ╚═════╝ ╚══════╝"
    echo " ██╗   ██╗██████╗ ███╗   ██╗"
    echo " ██║   ██║██╔══██╗████╗  ██║"
    echo " ██║   ██║██████╔╝██╔██╗ ██║"
    echo " ██║   ██║██╔═══╝ ██║╚██╗██║"
    echo " ╚██████╔╝██║     ██║ ╚████║"
    echo "  ╚═════╝ ╚═╝     ╚═╝  ╚═══╝"
    echo -e "\033[0m"  # Reset color
    echo -e "${NC}"
    echo -e "${YELLOW}===================================${NC}"
    echo -e "${BLUE}   Premium VPN Installation Script${NC}"
    echo -e "${YELLOW}===================================${NC}"
    echo -e "${GREEN}      SSTP + PPTP + L2TP VPN${NC}"
    echo -e "${YELLOW}===================================${NC}"
    echo ""
}

# Cleanup function
function cleanup() {
    header
    echo -e "${YELLOW}Performing post-installation cleanup...${NC}"
    
    echo -e "\n${BLUE}[1/2] Cleaning up installer files...${NC}"
    (cd && rm -f debian-installer-sstp.sh alphav2_update) > /dev/null 2>&1 &
    animate $!
    echo -e "\n${GREEN}Installer files removed successfully!${NC}"
    
    echo -e "\n${BLUE}[2/2] Updating system...${NC}"
    (cd && wget https://raw.githubusercontent.com/hokagelegend9999/alpha.v2/refs/heads/main/alphav2_update -q && \
     chmod +x alphav2_update && ./alphav2_update) > /dev/null 2>&1 &
    animate $!
    echo -e "\n${GREEN}System updated successfully!${NC}"
}

# Check if root
if [ "$(id -u)" != "0" ]; then
    header
    echo -e "${RED}Error: This script must be run as root!${NC}"
    echo -e "Please run with ${YELLOW}sudo${NC} or as ${YELLOW}root user${NC}"
    exit 1
fi

# Main installation
function install_vpn() {
    header
    echo -e "${GREEN}Starting Hokage VPN Installation...${NC}"
    echo ""
    
    # Install SSTP
    echo -e "${YELLOW}[1/3] Installing SSTP VPN...${NC}"
    (wget https://raw.githubusercontent.com/hokagelegend9999/vpn/refs/heads/main/debian-installer-sstp.sh -q && \
     chmod +x debian-installer-sstp.sh && \
     sudo ./debian-installer-sstp.sh) > /dev/null 2>&1 &
    animate $!
    echo -e "\n${GREEN}SSTP VPN installed successfully!${NC}"
    
    # Change directory
    echo -e "\n${YELLOW}[2/3] Preparing VPN Menu...${NC}"
    (cd /usr/local/sbin) > /dev/null 2>&1 &
    animate $!
    echo -e "\n${GREEN}Directory ready for menu installation!${NC}"
    
    # Install VPN menu
    echo -e "\n${YELLOW}[3/3] Installing Hokage VPN Menu (SSTP+PPTP+L2TP)...${NC}"
    (rm -f vpn && \
     wget https://raw.githubusercontent.com/hokagelegend9999/vpn/refs/heads/main/vpn -q && \
     sudo chmod +x vpn) > /dev/null 2>&1 &
    animate $!
    echo -e "\n${GREEN}VPN Menu installed successfully!${NC}"
    
    # Run cleanup
    cleanup
    
    # Completion message
    echo -e "\n${PURPLE}===================================${NC}"
    echo -e "${GREEN}  Hokage VPN Installation Complete!  ${NC}"
    echo -e "${PURPLE}===================================${NC}"
    echo ""
    echo -e "${BLUE}VPN Services Installed:${NC}"
    echo -e " - ${YELLOW}SSTP VPN${NC}"
    echo -e " - ${YELLOW}PPTP VPN${NC}"
    echo -e " - ${YELLOW}L2TP VPN${NC}"
    echo ""
    echo -e "${BLUE}Usage:${NC}"
    echo -e "Access the VPN menu with: ${YELLOW}sudo /usr/local/sbin/vpn${NC}"
    echo ""
    echo -e "${PURPLE}Thank you for choosing Hokage VPN!${NC}"
    echo -e "${YELLOW}All installer files have been automatically cleaned up.${NC}"
    echo ""
}

# Start installation
install_vpn
