#!/bin/sh
# ============================================================
# fallback_boot.sh -- Structured failure capture when KVM absent
#
# Called by init when hardware virtualization is unavailable
# and hiperiso_fallback is not "1".  Writes structured session
# metadata (session.json, status.json, FAILURE.txt) so the boot
# attempt is captured even though no QEMU guest was launched,
# then warns the user and offers to reboot.
# ============================================================

. /hiperiso-lib.sh

# ── Capture structured failure data ───────────────────────────
if [ -n "${LOG_DIR:-}" ] && [ -d "$LOG_DIR" ]; then
    hiperiso_log "FALLBACK: KVM unavailable -- capturing failure session"

    hiperiso_write_session_meta "failed"
    hiperiso_write_status "failed" "fallback_no_kvm"

    {
        printf '========================================\n'
        printf '  hiperiso FAILURE: KVM Unavailable\n'
        printf '========================================\n\n'
        printf 'ISO:       %s\n' "${HIPERISO_ISO_PATH:-unknown}"
        printf 'Session:   %s\n' "$(hiperiso_session_id)"
        printf 'Timestamp: %s\n' "$(hiperiso_utc_iso)"
        printf 'Reason:    KVM not detected (VT-x/AMD-V unavailable)\n\n'
        printf 'No QEMU guest was launched.  No serial.log or\n'
        printf 'trace.bin was produced for this session.\n'
        printf '========================================\n'
    } > "${LOG_DIR}/FAILURE.txt"
    sync "${LOG_DIR}/FAILURE.txt" 2>/dev/null || sync

    hiperiso_run_report
    # Finalize so the failed session gets a canonical manifest and an
    # entry in the global JSONL index (failed sessions are indexed too).
    hiperiso_finalize_session "failed" "" "fallback_no_kvm"
fi

# Clear the terminal
printf '\033[2J\033[H'

printf '\n'
printf '======================================================\n'
printf '  WARNING: Hardware Virtualization (KVM) Unavailable   \n'
printf '======================================================\n'
printf '\n'
printf '  hiperiso requires KVM (Intel VT-x or AMD-V) to run\n'
printf '  an ISO as a guest virtual machine.\n'
printf '\n'
printf '  KVM was NOT detected on this system, so this ISO\n'
printf '  cannot be booted in hypervisor mode.\n'
printf '\n'
printf '  Possible reasons:\n'
printf '    1. The CPU does not support VT-x / AMD-V\n'
printf '    2. Virtualization is disabled in BIOS/UEFI\n'
printf '    3. KVM kernel modules failed to load\n'
printf '\n'
printf '  How to proceed:\n'
printf '    - Reboot, enable VT-x/AMD-V in BIOS/UEFI, retry\n'
printf '    - Use a direct-boot tool (e.g. Rufus, balenaEtcher) for this ISO\n'
printf '    - Reboot with hiperiso_fallback=1 on the kernel\n'
printf '      command line to force TCG emulation (very slow)\n'
printf '\n'
if [ -n "${LOG_DIR:-}" ] && [ -d "$LOG_DIR" ]; then
    printf '  Failure session captured: %s\n' "$LOG_DIR"
    printf '\n'
fi
printf '======================================================\n'
printf '\n'

printf '  Press ENTER to reboot to the boot menu,\n'
printf '  or type "shell" for a recovery shell: '

read -r _choice
case "$_choice" in
    shell|sh)
        printf '\nDropping to recovery shell...\n'
        exec /bin/sh
        ;;
    *)
        printf '\nRebooting...\n'
        sync
        if command -v reboot >/dev/null 2>&1; then
            reboot -f
        else
            # Fallback: magic SysRq
            printf 'b' > /proc/sysrq-trigger 2>/dev/null
        fi
        # Should not reach here
        exit 0
        ;;
esac
