#!/bin/bash
# HOKAGEVPN RESTORE SCRIPT
# ==========================================
# Modern UI Design
# Color Palette
BLACK='\033[0;30m'
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
MAGENTA='\033[1;35m'
CYAN='\033[1;36m'
WHITE='\033[1;37m'
NC='\033[0m'

# UI Elements
DIVIDER="$(printf '%*s\n' "$(tput cols)" '' | tr ' ' '═')"
SPACER=""

# ==========================================

clear
echo -e "${BLUE}${DIVIDER}${NC}"
echo -e "${WHITE}   ██╗  ██╗ ██████╗ ██╗  ██╗ █████╗  ██████╗ ███████╗${NC}"
echo -e "${WHITE}   ██║  ██║██╔═══██╗██║ ██╔╝██╔══██╗██╔════╝ ██╔════╝${NC}"
echo -e "${WHITE}   ███████║██║   ██║█████╔╝ ███████║██║  ███╗█████╗  ${NC}"
echo -e "${WHITE}   ██╔══██║██║   ██║██╔═██╗ ██╔══██║██║   ██║██╔══╝  ${NC}"
echo -e "${WHITE}   ██║  ██║╚██████╔╝██║  ██╗██║  ██║╚██████╔╝███████╗${NC}"
echo -e "${WHITE}   ╚═╝  ╚═╝ ╚═════╝ ╚═╝  ╚═╝╚═╝  ╚═╝ ╚═════╝ ╚══════╝${NC}"
echo -e "${BLUE}${DIVIDER}${NC}"
echo -e "${CYAN}           VPN CONFIGURATION RESTORE TOOL${NC}"
echo -e "${BLUE}${DIVIDER}${NC}"
echo -e "${YELLOW}✪ Note: This script will restore your VPN configuration${NC}"
echo -e "${YELLOW}✪ Ensure you have a complete backup before proceeding${NC}"
echo -e "${BLUE}${DIVIDER}${NC}"
echo ""

# Check root
if [ "$(id -u)" != "0" ]; then
   echo -e "${RED}✘ Error: This script requires root privileges${NC}" 1>&2
   echo -e "${BLUE}${DIVIDER}${NC}"
   exit 1
fi

# Restore source selection
echo -e "${MAGENTA}▓▓ RESTORE SOURCE SELECTION ▓▓${NC}"
echo -e "${WHITE}Please choose where to restore from:${NC}"
echo -e "${CYAN}1) ${WHITE}Local backup file in /root directory${NC}"
echo -e "${CYAN}2) ${WHITE}Google Drive download link${NC}"
echo -e "${BLUE}${DIVIDER}${NC}"
read -p "$(echo -e "${YELLOW}→ Choose option (1/2): ${NC}")" source_option

case $source_option in
    1)
        # Local file restore
        echo -e "${GREEN}✔ Available backup files in /root:${NC}"
        echo -e "${WHITE}$(ls -lh /root/*.zip | awk '{print "  " $9 " (" $5 ")"}')${NC}"
        echo -e "${BLUE}${DIVIDER}${NC}"
        read -p "$(echo -e "${YELLOW}→ Enter backup filename (e.g. backup-2023-01-01.zip): ${NC}")" backup_file
        backup_path="/root/$backup_file"
        ;;
    2)
        # Google Drive restore
        echo -e "${GREEN}✔ Google Drive Restore${NC}"
        read -p "$(echo -e "${YELLOW}→ Enter Google Drive share link: ${NC}")" drive_link
        file_id=$(echo "$drive_link" | grep -o '[a-zA-Z0-9_-]\{28,\}')
        if [ -z "$file_id" ]; then
            echo -e "${RED}✘ Invalid Google Drive link!${NC}"
            echo -e "${BLUE}${DIVIDER}${NC}"
            exit 1
        fi
        backup_path="/root/restore_$file_id.zip"
        echo -e "${GREEN}⏳ Downloading from Google Drive...${NC}"
        wget --quiet --show-progress "https://drive.google.com/uc?export=download&id=$file_id" -O "$backup_path"
        ;;
    *)
        echo -e "${RED}✘ Invalid option!${NC}"
        echo -e "${BLUE}${DIVIDER}${NC}"
        exit 1
        ;;
esac

# Verify backup file
if [ ! -f "$backup_path" ]; then
    echo -e "${RED}✘ Backup file not found!${NC}"
    echo -e "${BLUE}${DIVIDER}${NC}"
    exit 1
fi

# Confirmation
echo -e "${RED}⚠ WARNING: This will overwrite existing system files!${NC}"
echo -e "${YELLOW}✪ Please verify this is the correct backup before proceeding${NC}"
read -p "$(echo -e "${YELLOW}→ Are you sure you want to proceed? (y/n): ${NC}")" confirm
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
    echo -e "${BLUE}✔ Restore cancelled.${NC}"
    rm -f "$backup_path" 2>/dev/null
    echo -e "${BLUE}${DIVIDER}${NC}"
    exit 0
fi

# Create temp directory
temp_dir="/root/restore_temp"
mkdir -p "$temp_dir"

# Extract backup
echo -e "${GREEN}⏳ Extracting backup...${NC}"
if ! unzip -o "$backup_path" -d "$temp_dir" >/dev/null 2>&1; then
    echo -e "${RED}✘ Error extracting backup file!${NC}"
    rm -rf "$temp_dir"
    echo -e "${BLUE}${DIVIDER}${NC}"
    exit 1
fi

# Verify backup structure
if [ ! -d "$temp_dir/backup" ]; then
    echo -e "${RED}✘ Invalid backup file structure!${NC}"
    rm -rf "$temp_dir"
    echo -e "${BLUE}${DIVIDER}${NC}"
    exit 1
fi

# Restore functions with modern UI
restore_system_files() {
    echo -e "${GREEN}⚙ Restoring system files...${NC}"
    cp "$temp_dir/backup/passwd" /etc/
    cp "$temp_dir/backup/group" /etc/
    cp "$temp_dir/backup/shadow" /etc/
    cp "$temp_dir/backup/gshadow" /etc/
    cp "$temp_dir/backup/crontab" /etc/
    echo -e "${GREEN}✔ System files restored${NC}"
}

restore_xray() {
    echo -e "${GREEN}⚙ Restoring Xray config...${NC}"
    rm -rf /etc/xray
    cp -r "$temp_dir/backup/xray" /etc/
    chmod -R 755 /etc/xray
    chown -R nobody:nogroup /etc/xray
    echo -e "${GREEN}✔ Xray configuration restored${NC}"
}

restore_web() {
    echo -e "${GREEN}⚙ Restoring web files...${NC}"
    rm -rf /var/www/html
    cp -r "$temp_dir/backup/html" /var/www/
    chown -R www-data:www-data /var/www/html
    chmod -R 755 /var/www/html
    echo -e "${GREEN}✔ Web files restored${NC}"
}

restore_kyt() {
    echo -e "${GREEN}⚙ Restoring kyt data...${NC}"
    rm -rf /var/lib/kyt
    cp -r "$temp_dir/backup/kyt" /var/lib/
    echo -e "${GREEN}✔ kyt data restored${NC}"
}

# Execute restores
restore_system_files
[ -d "$temp_dir/backup/xray" ] && restore_xray
[ -d "$temp_dir/backup/html" ] && restore_web
[ -d "$temp_dir/backup/kyt" ] && restore_kyt

# Cleanup
rm -rf "$temp_dir"
if [ "$source_option" -eq 2 ]; then
    rm -f "$backup_path"
fi

# Completion
echo -e "${BLUE}${DIVIDER}${NC}"
echo -e "${GREEN}   ██████╗ ███████╗███████╗████████╗ ██████╗ ██████╗ ███████╗   ${NC}"
echo -e "${GREEN}   ██╔══██╗██╔════╝██╔════╝╚══██╔══╝██╔═══██╗██╔══██╗██╔════╝   ${NC}"
echo -e "${GREEN}   ██████╔╝█████╗  █████╗     ██║   ██║   ██║██████╔╝█████╗     ${NC}"
echo -e "${GREEN}   ██╔══██╗██╔══╝  ██╔══╝     ██║   ██║   ██║██╔══██╗██╔══╝     ${NC}"
echo -e "${GREEN}   ██║  ██║███████╗███████╗   ██║   ╚██████╔╝██║  ██║███████╗   ${NC}"
echo -e "${GREEN}   ╚═╝  ╚═╝╚══════╝╚══════╝   ╚═╝    ╚═════╝ ╚═╝  ╚═╝╚══════╝   ${NC}"
echo -e "${BLUE}${DIVIDER}${NC}"
echo -e "${YELLOW}Recommended actions:${NC}"
echo -e "${CYAN}1. ${WHITE}Restart Xray: ${YELLOW}systemctl restart xray${NC}"
echo -e "${CYAN}2. ${WHITE}Restart web server: ${YELLOW}systemctl restart nginx${NC} or ${YELLOW}systemctl restart apache2${NC}"
echo -e "${CYAN}3. ${WHITE}Verify services: ${YELLOW}systemctl status xray nginx${NC}"
echo -e "${BLUE}${DIVIDER}${NC}"
