#!/usr/bin/env bash
# Run inside the Omarchy live ISO when it was booted from a partition on the
# disk you are installing to. The installer hides the disk it booted from,
# which leaves an empty disk list ("gum: no options provided").
# This drops that filter for this live session only, then restarts the installer.
set -euo pipefail

configurator=/root/configurator

[[ $EUID -eq 0 ]] || { echo "Run as root." >&2; exit 1; }
[[ -d /run/archiso && -f $configurator ]] || { echo "Not an Omarchy live ISO." >&2; exit 1; }
# The installer's entry script silently exits anywhere but tty1. Under
# `curl | bash` stdin is the pipe, and /dev/tty reports itself as "/dev/tty",
# so find the real console from stderr and hand that to the installer.
console=$(readlink -f /proc/$$/fd/2)
[[ $console == /dev/tty1 ]] || { echo "Run this on tty1 (now on $console): press Alt+F1, then run it again." >&2; exit 1; }

sed -i 's/exclude_disk=$(get_root_disk "$boot_source")/exclude_disk=/' "$configurator"
grep -qE '^[[:space:]]*exclude_disk=$' "$configurator" ||
  { echo "Patch did not apply: this installer version differs. Nothing was changed." >&2; exit 1; }

cat <<'EOF'
Installer patched: the boot disk is now selectable.

In the installer choose "Free space install".
Do NOT choose the full-disk install: it wipes every partition on the disk,
including your other OS and the live media this installer runs from.

EOF
read -rp "Press Enter to start the installer, or Ctrl+C to stop. " <"$console"
exec bash /root/.automated_script.sh <"$console"
