#!/bin/bash

# Define colors
GREEN='\033[0;32m'
RED='\033[0;31m'
Lyellow='\033[0;93m'
NC='\033[0m'

# Function to print messages in color
green() { echo -e "\\033[32;1m${*}\\033[0m"; }
red() { echo -e "\\033[31;1m${*}\\033[0m"; }
yellow() { echo -e "\\033[33;1m${*}\\033[0m"; }

# Function to upload file to file.io and get download link
upload_file() {
    local file_path="/root/${backup_filename}"
    local api_url="https://file.io"
    local expiry_duration=$((14 * 24 * 60 * 60)) # 14 days
    local response=$(curl -s -F "file=@$file_path" -F "expiry=$expiry_duration" $api_url)
    local upload_link=$(echo $response | jq -r .link)
    echo $upload_link
}

# Function to send file and note via Telegram
send_file_to_telegram() {
    local file_path="/root/${backup_filename}"
    local note="Date: ${date}"
    local KEY_TELE=$(grep -E "^#bot# " "/etc/bot/.notif.db" | cut -d ' ' -f 2)
    local ID_TELE=$(grep -E "^#bot# " "/etc/bot/.notif.db" | cut -d ' ' -f 3)
    local URL="https://api.telegram.org/bot${KEY_TELE}/sendDocument"

    # Send backup file to Telegram with caption
    curl -s -X POST $URL -F chat_id=$ID_TELE -F document=@$file_path -F caption="$note"
}

# Main logic
backup_and_notify() {
    clear
    echo -e "${Lyellow}┌─────────────────────────────────────┐${NC}"
    echo -e "${Lyellow}│${NC}  [ ${green}INFO${NC} ] • Creating backup...${NC}"
    echo -e "${Lyellow}└─────────────────────────────────────┘${NC}"

    # Define variables
    local IPVPS=$(curl -s ipv4.icanhazip.com)
    local domain=$(cat /etc/xray/domain)
    local date=$(date +"%d-%m-%Y")
    local backup_filename="${domain}-${IPVPS}.zip"

    echo -e "Date: ${date}"

    # Backup process
    rm -rf /root/backup
    mkdir /root/backup
    cp /etc/passwd /root/backup/
    cp /etc/group /root/backup/
    cp /etc/shadow /root/backup/
    cp /etc/gshadow /root/backup/
    cp /etc/crontab /root/backup/
    cp /etc/vmess/.vmess.db /root/backup/
    cp /etc/ssh/.ssh.db /root/backup/
    cp /etc/vless/.vless.db /root/backup/
    cp /etc/trojan/.trojan.db /root/backup/
    cp /etc/bot/.notif.db /root/backup/
    cp /etc/shadowsocks/.shadowsocks.db /root/backup/
    cp -r /etc/biji/limit /root/backup/
    cp -r /etc/vmess /root/backup/
    cp -r /etc/trojan /root/backup/
    cp -r /etc/vless /root/backup/
    cp -r /etc/shadowsocks /root/backup/
    cp -r /var/lib/biji/ /root/backup/kyt
    cp -r /etc/xray /root/backup/xray
    cp -r /var/www/html/ /root/backup/html
    cd /root
    zip -r "${backup_filename}" backup
    clear

    # Upload to file.io and get the link
    local upload_link=$(upload_file)
    clear

    echo -e "${GREEN}Backup uploaded to file.io successfully${NC}"
    sleep 0.5
    echo -e "Backup Link: ${RED}$upload_link${NC}"

    # Send backup file and note via Telegram
    send_file_to_telegram

    # Clean up
    rm -rf /root/backup
    rm -f /root/${backup_filename}

    echo -e "${GREEN}Backup sent successfully to Telegram!${NC}"
    sleep 0.5
    echo "Backup process completed. Exiting..."
}

# Main function
backup_and_notify