#!/bin/bash

rm -rf "$0"
rm -f install.sh

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_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"
}

clear

box_top
box_center "INTERFLAZZ.MY.ID" "$green"
box_center "AUTOSCRIPT PREMIUM" "$bold_white"
box_middle
box_center "INSTALASI BOT VPN" "$yellow"
box_middle
box_center "Proses instalasi sedang berjalan..." "$white"
box_bottom

echo ""

download_status() {
    local target="$1"
    local url="$2"
    local filename

    filename=$(basename "$target")

    echo -ne "${yellow}  [ ${green}•${yellow} ] ${white}Mengunduh $filename... ${neutral}"

    wget \
        -4 \
        -q \
        --no-check-certificate \
        --secure-protocol=TLSv1_2 \
        -O "$target" \
        "$url"

    if [[ -s "$target" ]]; then
        chmod +x "$target"
        sed -i 's/\r$//' "$target"

        echo -e "${green}[TERSIMPAN]${neutral}"
    else
        echo -e "${red}[GAGAL]${neutral}"

        echo -ne "${yellow}  [ ${green}•${yellow} ] ${white}Mencoba ulang $filename... ${neutral}"

        wget \
            -4 \
            -q \
            --no-check-certificate \
            -O "$target" \
            "$url"

        if [[ -s "$target" ]]; then
            chmod +x "$target"
            sed -i 's/\r$//' "$target"

            echo -e "${yellow}[BERHASIL DIPULIHKAN]${neutral}"
        else
            echo -e "${red}[FILE MASIH KOSONG]${neutral}"
        fi
    fi
}

res1() {
    mkdir -p \
        /usr/local/sbin \
        /usr/bin \
        /etc/xray \
        /root/telebotvpn

    cd /usr/local/sbin || exit 1

    echo -e "${blue}  [ + ] ${white}Memasang file pengelola akun VPN...${neutral}"
    echo ""

    files_sbin=(
        "trialsshbot"
        "addsshbot"
        "trialvmessbot"
        "addvmessbot"
        "trialvlessbot"
        "addvlessbot"
        "trialtrojanbot"
        "addtrojanbot"
        "renewsshbot"
        "renewvmessbot"
        "renewvlessbot"
        "renewtrojanbot"
        "deletesshbot"
        "deletevmessbot"
        "deletevlessbot"
        "deletetrojanbot"
        "totalmemberbot"
        "locksshbot"
        "lockvmessbot"
        "lockvlessbot"
        "locktrojanbot"
        "unlocksshbot"
        "unlockvmessbot"
        "unlockvlessbot"
        "unlocktrojanbot"
        "autobackupbot"
    )

    for file in "${files_sbin[@]}"; do
        download_status \
            "/usr/local/sbin/$file" \
            "${BASE_URL}/Api/$file"
    done

    echo ""
    echo -ne "${yellow}  [ ${green}•${yellow} ] ${white}Menyimpan konfigurasi Bot Telegram... ${neutral}"

    tokentelebotserver="8340701286:AAHQC8NMaW9WtWCAyw0lR99dzDxJjNmOM80"
    tokentelebotbackup="8453599516:AAF4qPI1BjHXhWmw39SR5s4QuluX0Oq1JFg"
    idteleowner="6348824977"
    idtelegrub="@tokovpnserver"

    echo "$tokentelebotserver" > /root/telebotvpn/tokentelebotserver
    echo "$tokentelebotbackup" > /root/telebotvpn/tokentelebotbackup
    echo "$idteleowner" > /root/telebotvpn/idteleowner
    echo "$idtelegrub" > /root/telebotvpn/idtelegrub

    chmod 600 \
        /root/telebotvpn/tokentelebotserver \
        /root/telebotvpn/tokentelebotbackup \
        /root/telebotvpn/idteleowner \
        /root/telebotvpn/idtelegrub

    echo -e "${green}[BERHASIL]${neutral}"
}

netfilter-persistent >/dev/null 2>&1

res1
install_status=$?

echo ""

if [ "$install_status" -eq 0 ]; then
    box_top
    box_center "INTERFLAZZ.MY.ID" "$green"
    box_center "AUTOSCRIPT PREMIUM" "$bold_white"
    box_middle
    box_center "INSTALASI BOT BERHASIL" "$green"
    box_middle
    box_center "Seluruh file Bot VPN berhasil dipasang." "$white"
    box_center "Bot VPN 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_center "AUTOSCRIPT PREMIUM" "$bold_white"
    box_middle
    box_center "INSTALASI BOT GAGAL" "$red"
    box_middle
    box_center "Terjadi kesalahan saat memasang Bot VPN." "$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 ""