#!/bin/bash
# ======================================================
#   HOKAGE LEGEND MENU-X (Optimized Fast Render)
# ======================================================

# COLOR VALIDATION
clear
y='\033[1;33m' #yellow
BGX="\033[42m"
CYAN="\033[96m"
z="\033[96m"
f="\033[1;97;41m"
RED='\033[0;31m'
NC='\033[0m'
gray="\e[1;30m"
Blue="\033[0;34m"
green='\033[0;32m'
grenbo="\e[92;1m"
purple="\033[1;95m"
YELL='\033[0;33m'
white='\033[1;37m'

# DEFINISI WARNA GRADASI MANUAL
G1="\033[1;31m" # Merah Terang
G2="\033[1;33m" # Kuning Terang

# ==================================================
# 1. GRADASI HEADER FAST (EMAS -> BIRU -> UNGU)
# ==================================================
print_gradient() {
    local text="$1"
    awk -v text="$text" 'BEGIN {
        len = length(text);
        if (len <= 1) { print text; exit; }
        # Emas -> Biru -> Ungu
        rs=255; gs=215; bs=0;
        rm=0;   gm=128; bm=255;
        re=138; ge=43;  be=226;

        for (i=0; i<len; i++) {
            ratio = i / (len-1);
            if (ratio <= 0.5) {
                f = ratio * 2;
                r = int(rs + (rm - rs) * f);
                g = int(gs + (gm - gs) * f);
                b = int(bs + (bm - bs) * f);
            } else {
                f = (ratio - 0.5) * 2;
                r = int(rm + (re - rm) * f);
                g = int(gm + (ge - gm) * f);
                b = int(bm + (be - bm) * f);
            }
            printf "\033[38;2;%d;%d;%dm%s", r, g, b, substr(text, i+1, 1);
        }
        printf "\033[0m\n";
    }'
}

# ==================================================
# 2. GRADASI FOOTER FAST (UNGU -> BIRU -> EMAS)
# ==================================================
print_gradient_footer() {
    local text="$1"
    awk -v text="$text" 'BEGIN {
        len = length(text);
        if (len <= 1) { print text; exit; }
        # Ungu -> Biru -> Emas
        rs=138; gs=43;  bs=226;
        rm=0;   gm=128; bm=255;
        re=255; ge=215; be=0;

        for (i=0; i<len; i++) {
            ratio = i / (len-1);
            if (ratio <= 0.5) {
                f = ratio * 2;
                r = int(rs + (rm - rs) * f);
                g = int(gs + (gm - gs) * f);
                b = int(bs + (bm - bs) * f);
            } else {
                f = (ratio - 0.5) * 2;
                r = int(rm + (re - rm) * f);
                g = int(gm + (ge - gm) * f);
                b = int(bm + (be - bm) * f);
            }
            printf "\033[38;2;%d;%d;%dm%s", r, g, b, substr(text, i+1, 1);
        }
        printf "\033[0m\n";
    }'
}

# ------------------------------------------
# SYSTEM INFO (OPTIMIZED)
MYIP=$(curl -sS ipv4.icanhazip.com)
IPVPS=$MYIP

ISP=$(cat /root/.info/.isp 2>/dev/null)
CITY=$(cat /root/.info/.city 2>/dev/null)
NS=$(cat /etc/xray/dns 2>/dev/null)
domain=$(cat /etc/xray/domain 2>/dev/null)
RAM=$(free -m | awk 'NR==2 {print $2}')
LOADCPU=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}')

MODEL=$(cat /etc/os-release | grep -w PRETTY_NAME | head -n1 | sed 's/=//g' | sed 's/"//g' | sed 's/PRETTY_NAME//g')
CORE=$(grep -c cpu[0-9] /proc/stat)
DATEVPS=$(date +'%d-%m-%Y')
TIMEZONE=$(date +'%H-%M-%S')
SERONLINE=$(uptime -p | cut -d " " -f 2-10000)

echo ""
izinsc="https://raw.githubusercontent.com/hokagelegend9999/ijin/refs/heads/main/alpha"

# USERNAME CHECK (Cached)
if [ -f /usr/bin/user ]; then
    username=$(cat /usr/bin/user)
    valid=$(cat /usr/bin/e)
else
    USER_DATA=$(curl -s ${izinsc} | grep $MYIP)
    username=$(echo "$USER_DATA" | awk '{print $2}')
    valid=$(echo "$USER_DATA" | awk '{print $3}')
    echo "$username" >/usr/bin/user
    echo "$valid" >/usr/bin/e
fi

# DETAIL ORDER
oid=$(cat /usr/bin/ver 2>/dev/null)
exp=$valid

# CERTIFICATE STATUS
today=$(date +"%Y-%m-%d")
d1=$(date -d "$valid" +%s)
d2=$(date -d "$today" +%s)
certifacate=$(((d1 - d2) / 86400))

# Status Expired Or Active
if [[ "$d1" -ge "$d2" ]]; then
    sts="${green}Active${NC}"
else
    sts="${RED}Expired${NC}"
fi

clear
# Getting RAM Info
tram=$( free -m | awk 'NR==2 {print $2}' )

# Status Check
ssh_status=$(systemctl is-active --quiet ssh && echo "Running" || echo "Dead")

##### INFO AKUN XRAY
vlx=$(grep -c -E "^#& " "/etc/xray/config.json" 2>/dev/null || echo 0)
let vla=$vlx/2
vmc=$(grep -c -E "^### " "/etc/xray/config.json" 2>/dev/null || echo 0)
let vma=$vmc/2
ssh1=$(awk -F: '$3 >= 1000 && $1 != "nobody" {print $1}' /etc/passwd | wc -l)
trx=$(grep -c -E "^#! " "/etc/xray/config.json" 2>/dev/null || echo 0)
let trb=$trx/2
ssx=$(grep -c -E "^#ss# " "/etc/xray/config.json" 2>/dev/null || echo 0)
let ssa=$ssx/2

###########
KANAN="\033[1;32m<\033[1;33m<\033[1;31m<\033[1;31m$NC"
KIRI="\033[1;32m>\033[1;33m>\033[1;31m>\033[1;31m$NC"
########
r="\033[1;31m"  #REDTERANG
a=" ${CYAN}ACCOUNT PREMIUM" 
clear

# ==================================================
# HEADER (GOLD -> BLUE -> PURPLE)
# ==================================================
echo -e " "
print_gradient " ╭══════════════════════════════════════════════════╮"
print_gradient " │     HOKAGE LEGEND STORE AUTOSCRIPT PREMIUM       │"
print_gradient " ╰══════════════════════════════════════════════════╯"
echo -e " ${YELL}╭══════════════════════════════════════════════════╮${NC}"
echo -e "  ${z}$NC [${white}01${NC}]$grenbo MENU DELL USER EXP$NC      ${z} $NC [${white}08${NC}]$purple RENNEW DOMAIN$NC"  
echo -e "  ${z}$NC [${white}02${NC}]$grenbo MENU AUTO REBOOT$NC        ${z} $NC [${white}09${NC}]$purple RUNNING$NC" 
echo -e "  ${z}$NC [${white}03${NC}]$grenbo MENU RESTART SERVICE$NC     ${z}$NC [${white}10${NC}]$purple REGIST IP$NC"
echo -e "  ${z}$NC [${white}04${NC}]$grenbo MENU BOT TELEGRAM$NC       ${z} $NC [${white}11${NC}]$purple UPDATE SCRIPT$NC"
echo -e "  ${z}$NC [${white}05${NC}]$grenbo CHANGE BANNER$NC           ${z} $NC [${white}12${NC}]$purple FIX CERT SSL$NC"
echo -e "  ${z}$NC [${white}06${NC}]$grenbo INFO PORT$NC               ${z} $NC [${white}13${NC}]$purple INSTALL UDP$NC"
echo -e "  ${z}$NC [${white}07${NC}]$grenbo BACKUP/ RESTORE$NC         ${z} $NC [${white}14${NC}]$purple DOWNGRADE X-RAY$NC"
echo -e "  ${z}$NC [${white}15${NC}]$grenbo INSTALL ZIVPN$NC           ${z} $NC"
echo -e " ${YELL}╰══════════════════════════════════════════════════╯${NC}"

# ==================================================
# FOOTER (PURPLE -> BLUE -> GOLD)
# ==================================================
print_gradient_footer " ╭════════════════════════════════════════════════╮"
print_gradient_footer "    PILIH MENU DENGAN CARA KETIK 1/15 LALU ENTER "
print_gradient_footer " ╰════════════════════════════════════════════════╯"

echo -e "    ${Blue}╭════════════════════════════════════════════╮${NC}"
echo -e "    ${Blue}   $NC${G1}Dev${G2}eloper${NC}      ${z}=${NC} HOKAGE LEGEND"
echo -e "    ${Blue}   $NC${G1}Tel${G2}egram${NC}       ${z}=${NC} @HookageLegend"
echo -e "    ${Blue}   $NC${G1}IP ${G2}ADDRESS${NC}     ${z}=${NC} $IPVPS${NC}"
echo -e "    ${Blue}   $NC${G1}Us${G2}er${NC}           ${z}=${NC} $username"
echo -e "    ${Blue}   $NC${G1}Exp${G2} Script${NC}     ${z}=${NC} $exp $certifacate Days$NC $sts"
echo -e "    ${Blue}╰═════════════════════════════════════════════╯${NC}"
echo -e "   \033[1;31m>\033[1;33m>\033[1;32m>\033[5m${CYAN} THANKS YOU FOR USING HOKAGE LEGEND STORE \033[0m\033[1;32m<\033[1;33m<\033[1;31m<${NC}"
echo -e ""

# --- LOGIKA MENU ---
read -p " PILIH OPSI MENU [ 1-15 atau x] ➤ : " opt
echo -e ""
case $opt in
1 | 01)
    clear
    xp
    echo ""
    echo -e " ${GREEN} Back to menu in 1 sec ${NC}"
    sleep 1
    menu-x
    ;;
2 | 02) clear ; autoreboot ;;
3 | 03) clear ; restart ; reboot ;;
4 | 04) clear ; m-bot ;;
5 | 05)
    clear
    echo -e "Edit Banner SSH Message " | lolcat
    sleep 2
    echo -e "Loading..........⏳" | lolcat
    sleep 2 # Dikurangi biar cepat
    nano /etc/kyt.txt
    clear
    echo -e "${green}ANDA YAKIN UNTUK MEMASANG BANNER INI ? $NC"
    echo -e ""
    read -p "SILAHKAN PILIH OPSI BERIKUT [Y/N]:" arg
    if [[ $arg == 'Y' || $arg == 'y' ]]; then
      echo -e "${Blue}Tindakan Diteruskan! $NC"
      clear
      echo -e "${Blue}START . . .$NC"
      service ssh restart
      service dropbear restart
    else
      echo -e "${RED}Tindakan Dihentikan! $NC"
      sleep 1
      clear
      menu
      exit 0
    fi
    clear
    menu-x # Kembali ke menu-x bukan reboot
    ;;
6 | 06) clear ; prot ;;
7 | 07) clear ; menu-backup ;;
8 | 08) clear ; addhost ;;
9 | 09) clear ; run ;;
10)
    clear
    echo -e "${green}Checking Permission Data...${NC}"
    data_mentah=$(curl -sS ${izinsc} | grep $MYIP)
    cek_user=$(echo "$data_mentah" | awk '{print $2}')
    cek_exp=$(echo "$data_mentah" | awk '{print $3}')
    cek_reg=$(echo "$data_mentah" | awk '{print $4}') 
    if [[ -z "$cek_reg" ]]; then cek_reg="Tidak Tersedia"; fi

    now_date=$(date -d "0 days" +"%Y-%m-%d")
    d1=$(date -d "$cek_exp" +%s)
    d2=$(date -d "$now_date" +%s)
    sisa_hari=$(( (d1 - d2) / 86400 ))

    clear
    echo -e " "
    echo -e "    ${Blue}╭════════════════════════════════════════════╮${NC}"
    echo -e "    ${Blue}│$NC${f}              USER LICENSE STATUS             $NC${Blue}│$NC"
    echo -e "    ${Blue}╰════════════════════════════════════════════╯${NC}"
    echo -e "    ${Blue}╭════════════════════════════════════════════╮${NC}"
    echo -e "    ${Blue}│$NC${y} IP VPS         $NC: $MYIP"
    echo -e "    ${Blue}│$NC${y} Username       $NC: $cek_user"
    echo -e "    ${Blue}│$NC${y} Registered     $NC: $cek_reg"
    echo -e "    ${Blue}│$NC${y} Expired Date $NC: $cek_exp"
    echo -e "    ${Blue}│$NC${y} Validity       $NC: $sisa_hari Days"
    echo -e "    ${Blue}│$NC${y} Status         $NC: $sts"
    echo -e "    ${Blue}╰════════════════════════════════════════════╯${NC}"
    echo -e " "
    read -n 1 -s -r -p " Press any key to back on menu"
    menu-x
    ;;
11)
    clear
    rm -f /root/update.sh
    wget -q https://raw.githubusercontent.com/hokagelegend9999/alpha.v2/refs/heads/main/update.sh
    chmod +x update.sh
    ./update.sh
    ;;
12) clear ; fixcert ;;
13)
    clear
    echo -e "${green}ANDA YAKIN UNTUK MEMASANG SCRIPT INI ? $NC"
    echo -e "${green}MINIMAL RAM 2 GB BIAR DISK TIDAK FULL $NC"
    echo -e "${green}OS SUPPORT UBUNTU 20.04 ONLY $NC"
    echo -e ""
    read -p "SUDAH LAPOR KE ADMIN ? [Y/N]:" arg
    if [[ $arg == 'Y' || $arg == 'y' ]]; then
        echo -e "${Blue}Tindakan Diteruskan! $NC"
        clear
        echo -e "${Blue}START . . .$NC"
        UDPX="https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1S3IE25v_fyUfCLslnujFBSBMNunDHDk2' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=1S3IE25v_fyUfCLslnujFBSBMNunDHDk2"
        wget --load-cookies /tmp/cookies.txt ${UDPX} -O install-udp && rm -rf /tmp/cookies.txt && chmod +x install-udp && ./install-udp
        systemctl status udp-custom
    else
        echo -e "${RED}Tindakan Dihentikan! $NC"
        sleep 1
        clear
        menu-x
    fi
    ;;
14)
    clear
    rm xray-downgrade
    wget -q https://github.com/hokagelegend9999/alpha.v2/raw/refs/heads/main/menu/xray-downgrade
    chmod +x xray-downgrade
    ./xray-downgrade
    ;;
15)
    clear
    wget -q https://raw.githubusercontent.com/hokagelegend9999/install/refs/heads/main/zivpn
    chmod +x zivpn
    ./zivpn
    systemctl status zivpn
    ;;
0 | 00) exit ;;
x) clear ; menu ;;
*) clear ; echo -e ; menu-x ;;
esac