#!/bin/bash

BASE_URL="https://autoscript.interflazz.my.id/v2"
BRAND="INTERFLAZZ.MY.ID"
WHATSAPP="https://wa.me/6285956131443"
TELEGRAM="https://t.me/interflazz"

green="\e[38;5;82m"
red="\e[38;5;196m"
neutral="\e[0m"
orange="\e[38;5;130m"
blue="\e[38;5;39m"
yellow="\e[38;5;226m"
purple="\e[38;5;141m"
bold_white="\e[1;37m"
reset="\e[0m"
pink="\e[38;5;205m"
white="\e[38;5;255m"

update_box_width() {
    local cols

    cols=$(tput cols 2>/dev/null || echo 80)

    if [ "$cols" -gt 72 ]; then
        BOX_WIDTH=72
    elif [ "$cols" -lt 30 ]; then
        BOX_WIDTH=30
    else
        BOX_WIDTH=$((cols - 2))
    fi

    BOX_INNER=$((BOX_WIDTH - 4))
}

repeat_char() {
    local count="$1"
    local char="$2"
    local line

    printf -v line '%*s' "$count" ""
    printf '%s' "${line// /$char}"
}

box_top() {
    update_box_width

    printf "%b┌" "$orange"
    repeat_char $((BOX_WIDTH - 2)) "─"
    printf "┐%b\n" "$reset"
}

box_middle() {
    update_box_width

    printf "%b├" "$orange"
    repeat_char $((BOX_WIDTH - 2)) "─"
    printf "┤%b\n" "$reset"
}

box_bottom() {
    update_box_width

    printf "%b└" "$orange"
    repeat_char $((BOX_WIDTH - 2)) "─"
    printf "┘%b\n" "$reset"
}

box_empty() {
    update_box_width

    printf "%b│%b" "$orange" "$reset"
    printf "%*s" $((BOX_WIDTH - 2)) ""
    printf "%b│%b\n" "$orange" "$reset"
}

box_center() {
    update_box_width

    local text="$1"
    local color="${2:-$white}"
    local length=${#text}
    local available="$BOX_INNER"

    if [ "$length" -gt "$available" ]; then
        box_left "$text" "$color"
        return
    fi

    local left=$(((available - length) / 2))
    local right=$((available - length - left))

    printf "%b│%b " "$orange" "$reset"
    printf "%*s" "$left" ""
    printf "%b%s%b" "$color" "$text" "$reset"
    printf "%*s" "$right" ""
    printf " %b│%b\n" "$orange" "$reset"
}

box_left() {
    update_box_width

    local text="$1"
    local color="${2:-$white}"
    local available="$BOX_INNER"
    local length=${#text}
    local offset=0
    local part
    local part_length
    local right

    if [ "$length" -eq 0 ]; then
        box_empty
        return
    fi

    while [ "$offset" -lt "$length" ]; do
        part="${text:$offset:$available}"
        part_length=${#part}
        right=$((available - part_length))

        printf "%b│%b " "$orange" "$reset"
        printf "%b%s%b" "$color" "$part" "$reset"
        printf "%*s" "$right" ""
        printf " %b│%b\n" "$orange" "$reset"

        offset=$((offset + available))
    done
}

show_access_denied() {
    local reason="$1"

    clear

    box_top
    box_center "$BRAND" "$green"
    box_center "AUTOSCRIPT PREMIUM" "$bold_white"
    box_middle
    box_center "AKSES DITOLAK!" "$red"
    box_middle
    box_left "IP VPS : $vps_ip" "$yellow"
    box_left "$reason" "$yellow"
    box_middle
    box_center "HUBUNGI ADMIN" "$bold_white"
    box_left "WhatsApp ~ $WHATSAPP" "$green"
    box_left "Telegram ~ $TELEGRAM" "$blue"
    box_bottom

    exit 1
}

vps_ip=$(wget -qO- ipv4.icanhazip.com)

if [ -z "$vps_ip" ]; then
    vps_ip=$(curl -4 -s ipv4.icanhazip.com)
fi

if [ -z "$vps_ip" ]; then
    vps_ip="Tidak diketahui"
fi

data_acc=$(
    curl -s "${BASE_URL}/access" |
        grep "### $vps_ip " |
        awk '{print $3}'
)

if [ -z "$data_acc" ]; then
    show_access_denied "VPS belum memiliki izin akses script."
fi

expiry_date=$(date -d "$data_acc" +%s 2>/dev/null)
current_date=$(date +%s)

if [ -z "$expiry_date" ]; then
    show_access_denied "Data masa aktif script tidak valid."
fi

if [ "$expiry_date" -le "$current_date" ]; then
    show_access_denied "Masa aktif akses script telah berakhir."
fi

remaining_days=$(((expiry_date - current_date) / 86400))

if [ -f "/root/.vars" ]; then
    source "/root/.vars"
fi

ipv4="$vps_ip"

execute_backup() {
    local backup_name
    local backup_dir
    local archive
    local web_archive

    backup_name="${ipv4}-$(date +%d%m%Y-%H%M%S)"
    backup_dir="/root/${backup_name}"
    archive="/root/${backup_name}.tar.gz"
    web_archive="/var/www/html/${backup_name}.tar.gz"

    rm -rf "$backup_dir"
    rm -f "$archive"

    mkdir -p \
        "$backup_dir/xray" \
        "$backup_dir/ssh"

    clear

    box_top
    box_center "$BRAND" "$green"
    box_center "BACKUP SERVER" "$bold_white"
    box_middle
    box_center "Memulai proses backup..." "$yellow"
    box_bottom

    echo ""

    if [ -d "/etc/xray/vmess" ]; then
        cp -a /etc/xray/vmess "$backup_dir/xray/" >/dev/null 2>&1
        echo -e "${green}[OK]${reset} VMess"
    fi

    if [ -d "/etc/xray/vless" ]; then
        cp -a /etc/xray/vless "$backup_dir/xray/" >/dev/null 2>&1
        echo -e "${green}[OK]${reset} VLESS"
    fi

    if [ -d "/etc/xray/trojan" ]; then
        cp -a /etc/xray/trojan "$backup_dir/xray/" >/dev/null 2>&1
        echo -e "${green}[OK]${reset} Trojan"
    fi

    if [ -d "/etc/xray/shadowsocks" ]; then
        cp -a /etc/xray/shadowsocks "$backup_dir/xray/" >/dev/null 2>&1
        echo -e "${green}[OK]${reset} Shadowsocks"
    fi

    if [ -f "/etc/xray/domain" ]; then
        cp -a /etc/xray/domain "$backup_dir/xray/" >/dev/null 2>&1
    fi

    if [ -f "/etc/xray/dns" ]; then
        cp -a /etc/xray/dns "$backup_dir/xray/" >/dev/null 2>&1
    fi

    if [ -f "/etc/xray/city" ]; then
        cp -a /etc/xray/city "$backup_dir/xray/" >/dev/null 2>&1
    fi

    if [ -f "/etc/ssh/.ssh.db" ]; then
        cp -a /etc/ssh/.ssh.db "$backup_dir/ssh/" >/dev/null 2>&1
        echo -e "${green}[OK]${reset} Database SSH"
    fi

    if [ -f "/etc/issue.net" ]; then
        cp -a /etc/issue.net "$backup_dir/" >/dev/null 2>&1
    fi

    if ! tar -czf "$archive" -C /root "$backup_name"; then
        rm -rf "$backup_dir"

        echo ""

        box_top
        box_center "$BRAND" "$green"
        box_middle
        box_center "BACKUP GAGAL" "$red"
        box_middle
        box_left "Gagal membuat arsip backup." "$yellow"
        box_bottom

        exit 1
    fi

    mkdir -p /var/www/html

    cp "$archive" "$web_archive"

    rm -rf "$backup_dir"

    clear

    box_top
    box_center "$BRAND" "$green"
    box_center "BACKUP SERVER" "$bold_white"
    box_middle
    box_center "BACKUP BERHASIL" "$green"
    box_middle
    box_left "Nama File : ${backup_name}.tar.gz" "$white"
    box_left "Lokasi    : $archive" "$white"
    box_left "Web       : https://${ipv4}:81/${backup_name}.tar.gz" "$cyan"
    box_middle
    box_left "WhatsApp ~ $WHATSAPP" "$green"
    box_left "Telegram ~ $TELEGRAM" "$blue"
    box_bottom

    exit 0
}

execute_restore() {
    local restore_archive="/root/restore.tar.gz"
    local restore_root="/root/restore"
    local source_root

    if [ ! -f "$restore_archive" ]; then
        clear

        box_top
        box_center "$BRAND" "$green"
        box_center "RESTORE SERVER" "$bold_white"
        box_middle
        box_center "FILE BACKUP TIDAK DITEMUKAN" "$red"
        box_middle
        box_left "Upload file backup ke:" "$yellow"
        box_left "/root/restore.tar.gz" "$white"
        box_bottom

        exit 1
    fi

    rm -rf "$restore_root"
    mkdir -p "$restore_root"

    clear

    box_top
    box_center "$BRAND" "$green"
    box_center "RESTORE SERVER" "$bold_white"
    box_middle
    box_center "Mengekstrak file backup..." "$yellow"
    box_bottom

    if ! tar -xzf "$restore_archive" -C "$restore_root"; then
        rm -rf "$restore_root"

        echo ""

        box_top
        box_center "$BRAND" "$green"
        box_middle
        box_center "RESTORE GAGAL" "$red"
        box_middle
        box_left "File backup rusak atau tidak valid." "$yellow"
        box_bottom

        exit 1
    fi

    source_root=$(find "$restore_root" -mindepth 1 -maxdepth 1 -type d | head -1)

    if [ -z "$source_root" ]; then
        rm -rf "$restore_root"

        box_top
        box_center "$BRAND" "$green"
        box_middle
        box_center "RESTORE GAGAL" "$red"
        box_middle
        box_left "Struktur file backup tidak valid." "$yellow"
        box_bottom

        exit 1
    fi

    if [ -d "$source_root/xray/vmess" ]; then
        rm -rf /etc/xray/vmess
        cp -a "$source_root/xray/vmess" /etc/xray/
    fi

    if [ -d "$source_root/xray/vless" ]; then
        rm -rf /etc/xray/vless
        cp -a "$source_root/xray/vless" /etc/xray/
    fi

    if [ -d "$source_root/xray/trojan" ]; then
        rm -rf /etc/xray/trojan
        cp -a "$source_root/xray/trojan" /etc/xray/
    fi

    if [ -d "$source_root/xray/shadowsocks" ]; then
        rm -rf /etc/xray/shadowsocks
        cp -a "$source_root/xray/shadowsocks" /etc/xray/
    fi

    if [ -f "$source_root/xray/domain" ]; then
        cp -a "$source_root/xray/domain" /etc/xray/domain
    fi

    if [ -f "$source_root/xray/dns" ]; then
        cp -a "$source_root/xray/dns" /etc/xray/dns
    fi

    if [ -f "$source_root/xray/city" ]; then
        cp -a "$source_root/xray/city" /etc/xray/city
    fi

    if [ -f "$source_root/ssh/.ssh.db" ]; then
        mkdir -p /etc/ssh
        cp -a "$source_root/ssh/.ssh.db" /etc/ssh/.ssh.db
    fi

    if [ -f "$source_root/issue.net" ]; then
        cp -a "$source_root/issue.net" /etc/issue.net
    fi

    systemctl daemon-reload >/dev/null 2>&1

    systemctl restart vmess@config >/dev/null 2>&1 || true
    systemctl restart vless@config >/dev/null 2>&1 || true
    systemctl restart trojan@config >/dev/null 2>&1 || true
    systemctl restart shadowsocks@config >/dev/null 2>&1 || true
    systemctl restart ssh >/dev/null 2>&1 || systemctl restart sshd >/dev/null 2>&1 || true

    rm -rf "$restore_root"
    rm -f "$restore_archive"

    clear

    box_top
    box_center "$BRAND" "$green"
    box_center "RESTORE SERVER" "$bold_white"
    box_middle
    box_center "RESTORE BERHASIL" "$green"
    box_middle
    box_center "Konfigurasi server berhasil dipulihkan." "$white"
    box_center "Silakan periksa seluruh layanan VPN." "$yellow"
    box_middle
    box_left "WhatsApp ~ $WHATSAPP" "$green"
    box_left "Telegram ~ $TELEGRAM" "$blue"
    box_bottom

    exit 0
}

server_backup_restore() {
    clear

    box_top
    box_center "$BRAND" "$green"
    box_center "BACKUP / RESTORE SERVER" "$bold_white"
    box_middle
    box_left "Backup melalui antarmuka web" "$green"
    box_left "Restore melalui antarmuka web" "$green"
    box_left "Kelola file backup server" "$green"
    box_middle
    box_left "Alamat Server :" "$yellow"
    box_left "http://${ipv4}:5000" "$cyan"
    box_middle
    box_center "Tekan CTRL+C untuk menghentikan server." "$yellow"
    box_bottom

    echo ""

    if [ ! -f "/usr/bin/backuprestore.js" ]; then
        box_top
        box_center "FILE SERVER TIDAK DITEMUKAN" "$red"
        box_middle
        box_left "/usr/bin/backuprestore.js tidak tersedia." "$yellow"
        box_bottom
        exit 1
    fi

    if ! command -v node >/dev/null 2>&1; then
        box_top
        box_center "NODE.JS TIDAK DITEMUKAN" "$red"
        box_middle
        box_left "Node.js belum terpasang pada server." "$yellow"
        box_bottom
        exit 1
    fi

    node /usr/bin/backuprestore.js

    echo ""

    box_top
    box_center "$BRAND" "$green"
    box_middle
    box_center "SERVER BACKUP / RESTORE BERHENTI" "$yellow"
    box_bottom
}

case "${1:-}" in
    backup)
        execute_backup
        ;;
    restore)
        execute_restore
        ;;
    server)
        server_backup_restore
        ;;
    *)
        clear

        box_top
        box_center "$BRAND" "$green"
        box_center "BACKUP / RESTORE SERVER" "$bold_white"
        box_middle
        box_center "PERINTAH TIDAK VALID" "$red"
        box_middle
        box_left "Gunakan salah satu perintah berikut:" "$yellow"
        box_left "backuprestore backup" "$white"
        box_left "backuprestore restore" "$white"
        box_left "backuprestore server" "$white"
        box_middle
        box_left "WhatsApp ~ $WHATSAPP" "$green"
        box_left "Telegram ~ $TELEGRAM" "$blue"
        box_bottom

        exit 1
        ;;
esac