#!/bin/bash

# ==========================================
# MENU MANAGEMENT REINSTALL
# ==========================================
clear
echo "======================================"
echo "    MENU MANAJEMEN REINSTALL OS       "
echo "======================================"
echo "Pilih OS Windows yang ingin diinstall:"
echo "1) Windows 12 (w12)"
echo "2) Windows 16 (w16)"
echo "3) Windows 19 (w19)"
echo "4) Custom Link"
read -p "Masukkan pilihan [1-4] (default: 1): " os_choice

case "$os_choice" in
    2)
        OS_LINK="https://archive.org/download/wins16/w16.gz"
        ;;
    3)
        OS_LINK="https://archive.org/download/wins19/w19.gz"
        ;;
    4)
        read -p "Masukkan URL Custom (kosongkan untuk default w12): " custom_link
        if [ -z "$custom_link" ]; then
            OS_LINK="https://archive.org/download/w12r2/w12r2.gz"
        else
            OS_LINK="$custom_link"
        fi
        ;;
    *)
        # Default pilihan 1 atau input tidak valid
        OS_LINK="https://archive.org/download/w12r2/w12r2.gz"
        ;;
esac

echo ""
read -p "Masukkan RDP Port (default: 6969): " RDP_PORT
RDP_PORT=${RDP_PORT:-6969}

echo ""
read -p "Masukkan RDP Password (default: Warning1@): " RDP_PASS
RDP_PASS=${RDP_PASS:-Warning1@}

echo ""
echo "======================================"
echo "          KONFIRMASI PILIHAN          "
echo "======================================"
echo "OS Link      : $OS_LINK"
echo "RDP Port     : $RDP_PORT"
echo "RDP Password : $RDP_PASS"
echo "======================================"
read -p "Apakah data di atas sudah benar? Lanjutkan instalasi? (y/n, default: y): " confirm
confirm=${confirm:-y}

if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
    echo "Instalasi dibatalkan oleh pengguna."
    exit 0
fi
echo "Memulai proses instalasi..."
echo "======================================"

# ==========================================
# ORIGINAL SCRIPT PROCESS
# ==========================================

sudo apt update && apt upgrade -y
set -e

echo "===== DOWNLOAD REINSTALL SCRIPT ====="
rm -f reinstall.sh
curl -O https://raw.githubusercontent.com/kripul/reinstall/main/reinstall.sh


echo "===== PATCH DISK DETECTION ====="
sed -i '/^find_main_disk()/,/^}/c\
find_main_disk() {\
\
    mapper=$(mount | awk '\''$3=="/boot" {print $1}'\'' | grep . || mount | awk '\''$3=="/" {print $1}'\'')\
\
    xda=$(lsblk -no PKNAME "$mapper" 2>/dev/null | head -n1)\
\
    if [ -z "$xda" ]; then\
        xda=$(lsblk -rn -o NAME,TYPE | awk '\''$2=="disk"{print $1; exit}'\'')\
    fi\
\
    if [ -z "$xda" ]; then\
        error_and_exit "Unable detect main disk"\
    fi\
\
    info "Main disk detected: /dev/$xda"\
}\
' reinstall.sh


echo "===== PATCH EXTLINUX ====="

# BYPASS ALL EXTLINUX OPERATIONS
sed -i '/extlinux_cfg=/c\
extlinux_cfg="/tmp/extlinux.conf"\
touch /tmp/extlinux.conf\
extlinux_dir="/tmp"\
' reinstall.sh

sed -i 's/sed -i "\/\^MENU HIDDEN\/d" \$extlinux_cfg/echo "skip extlinux config"/' reinstall.sh

sed -i 's/extlinux --once=reinstall \$extlinux_dir/echo "GRUB2 mode skip extlinux"/' reinstall.sh


echo "===== PATCH TRANS.SH ====="

# AUTO PATCH main_disk ERROR INSIDE ALPINE
sed -i '/wget.*trans.sh/a\
sed -i '\''/if \\[ -z "\\$main_disk" \\]; then/,/fi/c\\\\\nmain_disk=sda\\\\\nxda=sda\\\\\ninfo "Force xda=\\/dev\\/sda"\\\\\n'\'' /trans.sh\
' reinstall.sh


chmod +x reinstall.sh


echo "===== START WINDOWS DD PREPARE ====="
bash reinstall.sh dd \
  --rdp-port "$RDP_PORT" \
  --password "$RDP_PASS" \
  --img "$OS_LINK"


echo "===== CREATE GRUB2 ENTRY ====="
# Menggunakan <<EOF tanpa tanda kutip tunggal agar variabel $OS_LINK dan $RDP_PORT terbaca
# $0 di-escape menjadi \$0 agar tidak dibaca sebagai variabel script induk
cat >/etc/grub.d/40_custom <<EOF
#!/bin/sh
exec tail -n +3 \$0

menuentry "Reinstall Windows" {linux /reinstall-vmlinuz main_disk=/dev/sda alpine_repo=http://dl-cdn.alpinelinux.org/alpine/v3.21/main modloop=http://dl-cdn.alpinelinux.org/alpine/v3.21/releases/x86_64/netboot/modloop-virt console=ttyS0,115200n8 console=tty0 finalos_distro=dd finalos_img=$OS_LINK finalos_img_type=raw finalos_img_type_warp=gzip extra_confhome=https://raw.githubusercontent.com/kripul/reinstall/main extra_rdp_port=$RDP_PORT
    initrd /reinstall-initrd
}
EOF

chmod +x /etc/grub.d/40_custom


echo "===== UPDATE GRUB ====="
update-grub


echo "===== SET NEXT BOOT ====="
grub-reboot "Reinstall Windows"


echo "===== REBOOT ====="
reboot