#!/bin/bash

rm -rf /root/update.sh >/dev/null 2>&1

BASE_URL="https://autoscript.interflazz.my.id/v2"

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"
white="\e[38;5;255m"
bold_white="\e[1;37m"

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" "$neutral"
}

box_middle() {
    update_box_width

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

box_bottom() {
    update_box_width

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

box_empty() {
    update_box_width

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

box_center() {
    update_box_width

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

    if [ "$length" -gt "$available" ]; then
        printf "%b│%b %b%s%b %b│%b\n" \
            "$orange" \
            "$neutral" \
            "$color" \
            "$text" \
            "$neutral" \
            "$orange" \
            "$neutral"
        return
    fi

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

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

box_left() {
    update_box_width

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

    if [ "$length" -gt "$available" ]; then
        printf "%b│%b %b%s%b %b│%b\n" \
            "$orange" \
            "$neutral" \
            "$color" \
            "$text" \
            "$neutral" \
            "$orange" \
            "$neutral"
        return
    fi

    local right=$((available - length))

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

show_access_denied() {
    local reason="$1"

    clear

    box_top
    box_center "INTERFLAZZ.MY.ID" "$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 ~ https://wa.me/6285956131443" "$green"
    box_left "Telegram ~ https://t.me/interflazz" "$blue"
    box_bottom
}

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

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."
    exit 1
fi

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

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

res1() {
    menu_dir="${BASE_URL}/Files/menu.zip"

    echo -e "${yellow}  [ ${green}•${yellow} ] ${white}Mengunduh menu.zip...${neutral}"

    if ! wget -q "$menu_dir" -O /tmp/menu.zip; then
        echo -e "${red}  [ ✘ ] Gagal mengunduh menu.zip!${neutral}"
        return 1
    fi

    echo -e "${yellow}  [ ${green}•${yellow} ] ${white}Memeriksa paket ekstraksi...${neutral}"

    if ! command -v 7z >/dev/null 2>&1; then
        apt update -y >/dev/null 2>&1
        apt install p7zip-full -y >/dev/null 2>&1
    fi

    echo -e "${yellow}  [ ${green}•${yellow} ] ${white}Mengekstrak file menu...${neutral}"

    rm -rf /tmp/menu >/dev/null 2>&1

    cd /tmp || {
        echo -e "${red}  [ ✘ ] Gagal membuka direktori /tmp!${neutral}"
        return 1
    }

    if ! 7z x -y -p@RSCoders45rs menu.zip >/dev/null 2>&1; then
        echo -e "${red}  [ ✘ ] Gagal mengekstrak menu.zip!${neutral}"
        rm -f /tmp/menu.zip
        cd >/dev/null 2>&1
        return 1
    fi

    cd >/dev/null 2>&1 || true

    if [ ! -d "/tmp/menu" ]; then
        echo -e "${red}  [ ✘ ] Direktori menu hasil ekstraksi tidak ditemukan!${neutral}"
        rm -f /tmp/menu.zip
        return 1
    fi

    echo -e "${yellow}  [ ${green}•${yellow} ] ${white}Menghapus file versi lama...${neutral}"

    for file in /tmp/menu/*; do
        [ -e "$file" ] || continue

        filename=$(basename "$file")

        if [ -f "/usr/bin/$filename" ]; then
            rm -f "/usr/bin/$filename"
        fi
    done

    echo -e "${yellow}  [ ${green}•${yellow} ] ${white}Memasang file versi terbaru...${neutral}"

    chmod +x /tmp/menu/* >/dev/null 2>&1

    if ! mv /tmp/menu/* /usr/bin/; then
        echo -e "${red}  [ ✘ ] Gagal memasang file menu terbaru!${neutral}"
        rm -rf /tmp/menu
        rm -f /tmp/menu.zip
        return 1
    fi

    chmod +x /usr/bin/* >/dev/null 2>&1

    rm -rf /tmp/menu
    rm -f /tmp/menu.zip

    echo -e "${green}  [ ✔ ] ${white}Pembaruan script berhasil diselesaikan!${neutral}"

    return 0
}

clear

box_top
box_center "INTERFLAZZ.MY.ID" "$green"
box_center "AUTOSCRIPT PREMIUM" "$bold_white"
box_middle
box_center "PEMBARUAN SCRIPT" "$yellow"
box_middle
box_left "IP VPS : $vps_ip" "$white"
box_left "Status : Memulai proses pembaruan" "$white"
box_bottom

echo ""

res1
update_status=$?

echo ""

if [ "$update_status" -eq 0 ]; then
    box_top
    box_center "INTERFLAZZ.MY.ID" "$green"
    box_middle
    box_center "PEMBARUAN BERHASIL" "$green"
    box_middle
    box_center "Semua file menu berhasil diperbarui." "$white"
    box_center "Versi terbaru siap digunakan." "$yellow"
    box_middle
    box_left "WhatsApp ~ https://wa.me/6285956131443" "$green"
    box_left "Telegram ~ https://t.me/interflazz" "$blue"
    box_bottom
else
    box_top
    box_center "INTERFLAZZ.MY.ID" "$green"
    box_middle
    box_center "PEMBARUAN GAGAL" "$red"
    box_middle
    box_center "Terjadi kesalahan saat memperbarui script." "$yellow"
    box_center "Silakan periksa koneksi atau file server." "$yellow"
    box_middle
    box_left "WhatsApp ~ https://wa.me/6285956131443" "$green"
    box_left "Telegram ~ https://t.me/interflazz" "$blue"
    box_bottom
fi

echo ""

read -n 1 -s -r -p "Tekan tombol apa saja untuk kembali..."

echo ""

rs dashboard