#!/bin/bash
# HOKAGEVPN Backup Script
# Only sends Google Drive link to Telegram (no file upload)
# ==========================================
# Color Codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
# ==========================================

# Check requirements
check_requirements() {
    for cmd in rclone zip curl; do
        if ! command -v $cmd &> /dev/null; then
            echo -e "${RED}Error: $cmd is not installed${NC}"
            exit 1
        fi
    done
}

# Get configuration
get_config() {
    CHATID=$(grep -E "^#bot# " "/etc/bot/.bot.db" | cut -d ' ' -f 3)
    KEY=$(grep -E "^#bot# " "/etc/bot/.bot.db" | cut -d ' ' -f 2)
    [ -z "$CHATID" ] || [ -z "$KEY" ] && {
        echo -e "${RED}Error: Telegram bot config not found${NC}"
        exit 1
    }

    IP=$(curl -4 -sS icanhazip.com) || {
        echo -e "${RED}Error: Failed to get IP address${NC}"
        exit 1
    }
    
    domain=$(cat /etc/xray/domain 2>/dev/null)
    date=$(date +"%Y-%m-%d")
    backup_file="backup-$IP-$date.zip"
    temp_dir="/tmp/backup_$date"
}

# Create backup
create_backup() {
    echo -e "${YELLOW}Creating backup...${NC}"
    
    mkdir -p "$temp_dir" || {
        echo -e "${RED}Error: Failed to create temp directory${NC}"
        exit 1
    }

    # System files
    cp -a /etc/passwd /etc/group /etc/shadow /etc/gshadow /etc/crontab "$temp_dir/"

    # Service configurations
    [ -d "/etc/xray" ] && cp -a /etc/xray "$temp_dir/"
    [ -d "/var/lib/kyt" ] && cp -a /var/lib/kyt "$temp_dir/"
    [ -d "/var/www/html" ] && cp -a /var/www/html "$temp_dir/"

    # Compress
    echo -e "${YELLOW}Compressing backup...${NC}"
    if ! zip -rq "/root/$backup_file" "$temp_dir"; then
        echo -e "${RED}Error: Failed to create zip file${NC}"
        exit 1
    fi

    rm -rf "$temp_dir"
}

# Upload to Google Drive and get link
upload_to_drive() {
    echo -e "${YELLOW}Uploading to Google Drive...${NC}"
    if ! rclone copy "/root/$backup_file" "dr:backup/"; then
        echo -e "${RED}Error: Google Drive upload failed${NC}"
        exit 1
    fi
    drive_link=$(rclone link "dr:backup/$backup_file")
    [ -z "$drive_link" ] && {
        echo -e "${RED}Error: Failed to get Google Drive link${NC}"
        exit 1
    }
}

# Send only Google Drive link to Telegram
send_to_telegram() {
    echo -e "${YELLOW}Sending notification to Telegram...${NC}"
    
    message="<b>HOKAGEVPN Backup Notification</b>
<code>────────────────────</code>
<b>🖥️ IP VPS:</b> <code>$IP</code>
<b>🌐 Domain:</b> <code>${domain:-Not Set}</code>
<b>📅 Date:</b> <code>$date</code>
<code>────────────────────</code>
<b>📥 Download Backup:</b>
<code>$drive_link</code>
<code>────────────────────</code>
<i>Size: $(du -h "/root/$backup_file" | cut -f1)</i>"

    curl -s -X POST "https://api.telegram.org/bot$KEY/sendMessage" \
        -d chat_id="$CHATID" \
        -d text="$message" \
        -d parse_mode="HTML" >/dev/null || {
        echo -e "${RED}Warning: Failed to send Telegram notification${NC}"
    }
}

# Main execution
check_requirements
get_config
create_backup
upload_to_drive
send_to_telegram

# Cleanup and show result
echo -e "${GREEN}Backup completed successfully!${NC}"
echo -e "Backup file: ${GREEN}/root/$backup_file${NC}"
echo -e "Google Drive: ${GREEN}$drive_link${NC}"
