#!/bin/bash
# Installs the ML4W Hyprland dotfiles (https://ml4w.com/os/stable) onto the
# VM over SSH, fully unattended.
#
# Two things make this non-trivial compared to just running the upstream
# one-liner (`curl -fsSL https://ml4w.com/os/stable | bash`):
#
#  1. Piping curl into bash consumes stdin with the script's own source, so
#     when the installer later launches `gum confirm` (a TUI libcomponent),
#     gum reads EOF on stdin and silently answers "No" -> "Installation
#     cancelled by user." Fix: download the script to a file on the VM and
#     run it directly (`bash file.sh`), keeping stdin attached to the real
#     pty.
#  2. The installer has exactly one interactive gate -- a single
#     `gum confirm "Do you want to proceed with the installation?"` in
#     lib/utils.sh. Driving that gum TUI through a nested pty (ssh -tt under
#     expect) was unreliable here (it got stuck in a redraw loop). Since
#     everything else in the installer is already non-interactive
#     (`pacman --noconfirm`), we patch that one line to auto-proceed instead
#     of trying to drive the TUI.
#
# sudo password prompts are handled via `expect`, matching both English
# ("password") and German ("Passwort") locales, since sudo localizes its
# prompt to the VM's configured locale.
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")"
source ./00-config.sh

echo ">>> Downloading the ML4W bootstrap script onto the VM"
ssh "${SSH_USER}@${VM_IP}" "curl -fsSL https://ml4w.com/os/stable -o /tmp/ml4w-bootstrap.sh"

BOOTSTRAP_EXPECT=$(mktemp)
trap 'rm -f "$BOOTSTRAP_EXPECT"' EXIT

cat > "$BOOTSTRAP_EXPECT" <<EOF
#!/usr/bin/expect -f
set timeout -1
log_file -a /tmp/ml4w_install_transcript.log
spawn ssh -tt ${SSH_USER}@${VM_IP} {bash /tmp/ml4w-bootstrap.sh}
expect {
  -nocase -re {(password|passwort).*: ?\$} {
    send "${VM_PASSWORD}\r"
    exp_continue
  }
  "Do you want to proceed with the installation?" {
    sleep 1
    send "y"
    sleep 0.3
    send "\r"
    exp_continue
  }
  eof
}
EOF
chmod +x "$BOOTSTRAP_EXPECT"

echo ">>> Running bootstrap (installs prerequisites: git, make, curl, jq, gum,"
echo "    the ml4w-dotfiles-installer binary) -- this run's confirm prompt"
echo "    is expected to show 'Installation cancelled by user.', that's fine,"
echo "    we patch and run the real install next."
rm -f /tmp/ml4w_install_transcript.log
"$BOOTSTRAP_EXPECT"

echo ">>> Patching the installed gum-confirm prompt to auto-proceed"
ssh "${SSH_USER}@${VM_IP}" \
  "sed -i 's/^\(\s*\)if ! gum confirm .*/\1info \"Auto-confirming installation (automated).\"/' \
   ~/.local/share/ml4w-dotfiles-installer/utils.sh"
ssh "${SSH_USER}@${VM_IP}" "grep -n 'Auto-confirming' ~/.local/share/ml4w-dotfiles-installer/utils.sh"

INSTALL_EXPECT=$(mktemp)
trap 'rm -f "$BOOTSTRAP_EXPECT" "$INSTALL_EXPECT"' EXIT
cat > "$INSTALL_EXPECT" <<EOF
#!/usr/bin/expect -f
set timeout -1
log_file -a /tmp/ml4w_install_transcript2.log
spawn ssh -tt ${SSH_USER}@${VM_IP} {export PATH=\$HOME/.local/bin:\$PATH; ml4w-dotfiles-installer --install ${ML4W_DOTINST_URL}}
expect {
  -nocase -re {(password|passwort).*: ?\$} {
    send "${VM_PASSWORD}\r"
    exp_continue
  }
  eof
}
EOF
chmod +x "$INSTALL_EXPECT"

echo ">>> Running the real (patched, non-interactive) install -- this takes a"
echo "    while: it installs the full Hyprland stack (waybar, hyprlock,"
echo "    hypridle, kitty, rofi, swaync, ...) package by package."
rm -f /tmp/ml4w_install_transcript2.log
"$INSTALL_EXPECT"

echo ">>> Done. Checking result:"
grep -aE "Installation successful|not found in cloned repository" /tmp/ml4w_install_transcript2.log || true
echo ">>> Full transcript: /tmp/ml4w_install_transcript2.log"
echo ">>> Reboot the VM to land on the new Hyprland session."
