#!/bin/bash
# HOKAGEVPN RESTORE SCRIPT
# ==========================================
# Color
RED='\033[0;31m'
NC='\033[0m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
LIGHT='\033[0;37m'
# ==========================================

clear
echo -e "${GREEN}==========================================${NC}"
echo -e "${GREEN}       HOKAGE VPN RESTORE TOOL         ${NC}"
echo -e "${GREEN}==========================================${NC}"
echo ""

# Check root
if [ "$(id -u)" != "0" ]; then
   echo -e "${RED}Error: Script must be run as root${NC}" 1>&2
   exit 1
fi

# Restore source selection
echo -e "${ORANGE}Select restore source:${NC}"
echo "1) Local file in /root directory"
echo "2) Google Drive download link"
read -p "Choose option (1/2): " source_option

case $source_option in
    1)
        # Local file restore
        echo -e "${GREEN}Available backup files in /root:${NC}"
        ls -lh /root/*.zip | awk '{print "- " $9 " (" $5 ")"}'
        echo ""
        read -p "Enter backup filename (e.g. backup-2023-01-01.zip): " backup_file
        backup_path="/root/$backup_file"
        ;;
    2)
        # Google Drive restore
        read -p "Enter Google Drive share link: " 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}"
            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}"
        exit 1
        ;;
esac

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

# Confirmation
echo -e "${RED}WARNING: This will overwrite existing system files!${NC}"
read -p "Are you sure you want to proceed? (y/n) " confirm
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
    echo -e "${ORANGE}Restore cancelled.${NC}"
    rm -f "$backup_path" 2>/dev/null
    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"
    exit 1
fi

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

# Restore functions
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/
}

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
}

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
}

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

# 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 "${GREEN}==========================================${NC}"
echo -e "${GREEN}   RESTORE COMPLETED SUCCESSFULLY   ${NC}"
echo -e "${GREEN}==========================================${NC}"
echo -e "${ORANGE}Recommended actions:${NC}"
echo "1. Restart Xray: systemctl restart xray"
echo "2. Restart web server: systemctl restart nginx/apache2"
echo "3. Verify services are running properly"
echo ""
