#!/usr/bin/env bash
# One-shot Legacy BIOS installer for a Freeosk test VM/host.
#
# Meant to be run from the NixOS installer live environment via:
#
#   nix-shell -p git gh curl mkpasswd --run 'bash -c "$(curl -fsSL https://iot.kioskterminal.net/freeosk.sh)"'
#
# By default this targets /dev/vda (a QEMU/libvirt VM). To install onto
# real hardware (a spare laptop, etc.) instead, run `lsblk` on the live
# environment first to find the right device, then set FREEOSK_DISK,
# e.g.:
#
#   FREEOSK_DISK=/dev/sda nix-shell -p git gh curl mkpasswd --run 'bash -c "$(curl -fsSL https://iot.kioskterminal.net/freeosk.sh)"'
#
# See README.md and INSTALL.md for the UEFI (example-kiosk) path and the
# full manual install walkthrough -- this script only covers the Legacy
# BIOS (example-kiosk-bios) profile, matching scripts/install-kiosk.sh.
#
# Do NOT run this as `curl ... | bash` -- piping steals stdin, which
# breaks the interactive `gh auth login` device-code flow, the
# confirmation prompts below, and the admin-password prompts in
# scripts/install-kiosk.sh. The `bash -c "$(curl ...)"` form above avoids
# that: command substitution captures the script text without touching
# this process's own stdin, so prompts still reach your terminal.
set -euo pipefail

echo
echo "=== Freeosk Legacy BIOS installer ==="
echo

echo "Checking GitHub authentication..."
if ! gh auth status >/dev/null 2>&1; then
  gh auth login
fi

gh auth status

echo
echo "Cloning Freeosk repo..."
cd /tmp
rm -rf freeosk
gh repo clone o-hervey/freeosk freeosk
cd /tmp/freeosk

echo
echo "Checking required files..."
test -x scripts/install-kiosk.sh
test -x scripts/rebuild-kiosk.sh
test -f nixos/hosts/example-kiosk-bios/admin-access.nix
test -f nixos/hosts/example-kiosk-bios/configuration.nix
test -f nixos/hosts/example-kiosk-bios/disko.nix
test -f nixos/hosts/example-kiosk-bios/hardware-configuration.nix

echo
echo "Detected disks:"
lsblk

echo
echo "Configured install target in BIOS disko file:"
grep -n 'device =' nixos/hosts/example-kiosk-bios/disko.nix

echo
echo "STOP AND CHECK:"
echo "This install will erase the disk configured in:"
echo "  nixos/hosts/example-kiosk-bios/disko.nix"
echo
if [ -n "${FREEOSK_DISK:-}" ]; then
  echo "FREEOSK_DISK is set: target will be changed to $FREEOSK_DISK."
  echo "Only continue if lsblk shows that $FREEOSK_DISK is definitely the disk you want to erase --"
  echo "if this is real hardware with more than one drive, double-check before continuing."
else
  echo "No FREEOSK_DISK set, so this uses whatever's already configured above (/dev/vda for the VM)."
  echo "Only continue if lsblk shows that is definitely the disk you want to erase."
  echo "Testing on real hardware instead? Re-run with FREEOSK_DISK=/dev/sda (or whichever device lsblk shows)."
fi
echo
read -r -p "Type YES to wipe the target disk and install Freeosk BIOS profile: " CONFIRM

if [ "$CONFIRM" != "YES" ]; then
  echo "Aborted. No install was run."
  exit 1
fi

echo
echo "Starting Freeosk BIOS install wrapper..."
INSTALL_ARGS=(example-kiosk-bios)
if [ -n "${FREEOSK_DISK:-}" ]; then
  INSTALL_ARGS+=(--disk "$FREEOSK_DISK")
fi
if [ -n "${FREEOSK_SSH_KEY:-}" ]; then
  INSTALL_ARGS+=(--ssh-key "$FREEOSK_SSH_KEY")
fi
sudo ./scripts/install-kiosk.sh "${INSTALL_ARGS[@]}"

echo
echo "Install finished."
echo "Record the admin password you entered before shutting down."
echo
read -r -p "Type POWEROFF once you have recorded the admin password: " POWER_CONFIRM

if [ "$POWER_CONFIRM" = "POWEROFF" ]; then
  sudo poweroff
else
  echo "Not powering off. Run this manually when ready:"
  echo "  sudo poweroff"
fi
