#!/bin/bash
# DD Reinstall Script - Interactive Debian 13 Reinstaller
# Author: mango082888-bit

set -e

# Colors
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
BLUE="\033[0;34m"
CYAN="\033[0;36m"
NC="\033[0m"

# Default values
DEFAULT_PWD='D6g9vuLrb&Nd&gC6gqcG2EKu4'
DEFAULT_PORT="22"
DEFAULT_TIMEZONE="Asia/Hong_Kong"
DEFAULT_SWAP="1024"

clear
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}   DD Reinstall - Debian 13 Installer${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

# Get hostname
echo -e "${CYAN}[1/4] Hostname${NC}"
read -p "Enter hostname: " MYHOST
if [ -z "$MYHOST" ]; then
    echo -e "${RED}[X] Hostname cannot be empty${NC}"
    exit 1
fi
echo -e "${GREEN}[OK] Hostname: $MYHOST${NC}"
echo ""

# Get password
echo -e "${CYAN}[2/4] Root Password${NC}"
echo -e "${YELLOW}Default: $DEFAULT_PWD${NC}"
read -p "Enter password (press Enter for default): " MYPWD
if [ -z "$MYPWD" ]; then
    MYPWD="$DEFAULT_PWD"
    echo -e "${GREEN}[OK] Using default password${NC}"
else
    echo -e "${GREEN}[OK] Using custom password${NC}"
fi
echo ""

# Get SSH port
echo -e "${CYAN}[3/4] SSH Port${NC}"
echo -e "${YELLOW}Default: $DEFAULT_PORT${NC}"
read -p "Enter SSH port (press Enter for default): " MYPORT
if [ -z "$MYPORT" ]; then
    MYPORT="$DEFAULT_PORT"
fi
echo -e "${GREEN}[OK] SSH Port: $MYPORT${NC}"
echo ""

# Get timezone
echo -e "${CYAN}[4/4] Timezone${NC}"
echo -e "${YELLOW}Default: $DEFAULT_TIMEZONE${NC}"
read -p "Enter timezone (press Enter for default): " MYTZ
if [ -z "$MYTZ" ]; then
    MYTZ="$DEFAULT_TIMEZONE"
fi
echo -e "${GREEN}[OK] Timezone: $MYTZ${NC}"
echo ""

# Summary
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}   Configuration Summary${NC}"
echo -e "${BLUE}========================================${NC}"
echo -e "System:    ${GREEN}Debian 13${NC}"
echo -e "Hostname:  ${GREEN}$MYHOST${NC}"
echo -e "Password:  ${GREEN}(set)${NC}"
echo -e "SSH Port:  ${GREEN}$MYPORT${NC}"
echo -e "Timezone:  ${GREEN}$MYTZ${NC}"
echo -e "Swap:      ${GREEN}${DEFAULT_SWAP}MB${NC}"
echo -e "BBR:       ${GREEN}Enabled${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

# Confirm
echo -e "${RED}WARNING: This will ERASE all data!${NC}"
read -p "Continue? (y/N): " CONFIRM
if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then
    echo -e "${YELLOW}[!] Cancelled${NC}"
    exit 0
fi

echo ""
echo -e "${BLUE}==> Downloading InstallNET.sh${NC}"
wget --no-check-certificate -qO InstallNET.sh 'https://raw.githubusercontent.com/leitbogioro/Tools/master/Linux_reinstall/InstallNET.sh'
chmod a+x InstallNET.sh

echo -e "${BLUE}==> Starting reinstall...${NC}"
echo -e "${YELLOW}[!] Server will reboot automatically${NC}"
echo ""

bash InstallNET.sh -debian 13 \
    -port "$MYPORT" \
    -pwd "$MYPWD" \
    -hostname "$MYHOST" \
    -timezone "$MYTZ" \
    -swap "$DEFAULT_SWAP" \
    --bbr

reboot
