#!/bin/sh
# run.sh - run the DF-0789 harness (userspace, deterministic, no root needed)
#
# Usage:
#   ./run.sh              # buggy walk (all modes)
#   ./run.sh apply_fix    # fixed walk (proposed fix applied)
#   ./run.sh live         # + live mount test (REQUIRES ROOT, panics/hangs the guest)
cd "$(dirname "$0")"

FIX_ARG="${1:-}"

echo "=== BUGGY walk (kernel behaviour on default GENERIC #0) ==="
for mode in clean oob_short oob_fill oob_infinite; do
    ./harness "$mode" 2>&1
done

echo ""
echo "=== FIXED walk (proposed fix: bound by runlen) ==="
for mode in clean oob_short oob_fill oob_infinite; do
    ./harness "$mode" apply_fix 2>&1
done

if [ "$FIX_ARG" = "live" ]; then
    echo ""
    echo "=== LIVE mount test (root only, EXPECTS PANIC/HANG) ==="
    if [ "$(id -u)" -ne 0 ]; then
        echo "  (skipped: not root)" >&2
    else
        vnconfig -c vn0 ./ntfs_0789.img 2>/dev/null || true
        mkdir -p /mnt/ntfs
        echo "  mount_ntfs -o ro /dev/vn0 /mnt/ntfs  (expect panic/hang)"
        mount_ntfs -o ro /dev/vn0 /mnt/ntfs 2>&1
        echo "  MOUNT_RC=$? (if you see this, mount returned without crashing)"
    fi
fi
