#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

fix_t2="$ROOT/install/hardware/apple/fix-t2.sh"
other_packages="$ROOT/install/omarchy-other.packages"
migration="$ROOT/migrations/1785944594.sh"

grep -Fq 'KERNEL_CMDLINE[default]+=" intel_iommu=on iommu=pt pm_async=off mem_sleep_default=deep"' "$fix_t2" ||
  fail "T2 setup installs the suspend kernel parameters"
! grep -q 'pcie_ports=compat' "$fix_t2" ||
  fail "T2 setup drops the obsolete PCIe compatibility parameter"
(( $(grep -Ec '^\[Fan[12]\]$' "$fix_t2") == 2 )) ||
  fail "T2 setup configures both possible MacBook fans"
(( $(grep -c '^speed_curve=linear$' "$fix_t2") == 2 )) ||
  fail "T2 setup preserves the tuned linear curve for both fans"
! grep -q 'tiny-dfr' "$fix_t2" ||
  fail "T2 setup leaves optional Touch Bar customization uninstalled"
! grep -qx 'tiny-dfr' "$other_packages" ||
  fail "the ISO no longer caches tiny-dfr"
pass "fresh T2 setup uses t2bce-compatible suspend, fan, and Touch Bar defaults"

test_tmp=$(mktemp -d)
trap 'rm -rf "$test_tmp"' EXIT

stub_bin="$test_tmp/bin"
calls="$test_tmp/calls.log"
mkdir -p "$stub_bin"
: >"$calls"

cat >"$stub_bin/lspci" <<'SH'
#!/bin/bash

# Chatty like real lspci: keep writing well past the pipe buffer after the T2
# match, so a grep -q consumer would kill this stub with SIGPIPE and pipefail
# would read that as "no T2 hardware" (#6608).
if (( ${T2_HARDWARE:-0} == 1 )); then
  echo '01:00.0 Bridge [0680]: Apple Inc. T2 Security Chip [106b:1801]'
fi
for _ in {1..4096}; do
  echo '02:00.0 Host bridge [0600]: Filler Device [ffff:0000]'
done
SH

cat >"$stub_bin/sudo" <<'SH'
#!/bin/bash

printf 'sudo' >>"$TEST_LOG"
printf '\t%s' "$@" >>"$TEST_LOG"
printf '\n' >>"$TEST_LOG"
"$@"
SH

cat >"$stub_bin/systemctl" <<'SH'
#!/bin/bash

printf 'systemctl' >>"$TEST_LOG"
printf '\t%s' "$@" >>"$TEST_LOG"
printf '\n' >>"$TEST_LOG"
SH

cat >"$stub_bin/omarchy-pkg-present" <<'SH'
#!/bin/bash

(( ${TINY_DFR_INSTALLED:-0} == 1 ))
SH

cat >"$stub_bin/omarchy-pkg-drop" <<'SH'
#!/bin/bash

printf 'omarchy-pkg-drop' >>"$TEST_LOG"
printf '\t%s' "$@" >>"$TEST_LOG"
printf '\n' >>"$TEST_LOG"
SH

cat >"$stub_bin/limine-mkinitcpio" <<'SH'
#!/bin/bash

echo 'limine-mkinitcpio' >>"$TEST_LOG"
SH

chmod +x "$stub_bin"/*

limine_conf="$test_tmp/t2-mac.conf"
fan_conf="$test_tmp/t2fand.conf"
running_cmdline="$test_tmp/cmdline"
repair_marker="$test_tmp/t2-repair-complete"

cat >"$limine_conf" <<'EOF'
# Generated by Omarchy installer for T2 Mac support
KERNEL_CMDLINE[default]+=" intel_iommu=on iommu=pt pcie_ports=compat"
EOF

cat >"$fan_conf" <<'EOF'
[Fan1]
low_temp=55
high_temp=75
speed_curve=linear
always_full_speed=false
EOF

echo 'quiet splash intel_iommu=on iommu=pt pcie_ports=compat' >"$running_cmdline"

PATH="$stub_bin:$PATH" \
  TEST_LOG="$calls" \
  T2_HARDWARE=1 \
  TINY_DFR_INSTALLED=1 \
  OMARCHY_T2_LIMINE_CONF="$limine_conf" \
  OMARCHY_T2_FAN_CONF="$fan_conf" \
  OMARCHY_T2_RUNNING_CMDLINE="$running_cmdline" \
  OMARCHY_T2_REPAIR_MARKER="$repair_marker" \
  bash -euo pipefail "$migration" >/dev/null

grep -Fq 'KERNEL_CMDLINE[default]+=" intel_iommu=on iommu=pt pm_async=off mem_sleep_default=deep"' "$limine_conf" ||
  fail "T2 migration updates the Limine suspend parameters"
! grep -q 'pcie_ports=compat' "$limine_conf" ||
  fail "T2 migration removes the obsolete PCIe compatibility parameter"
(( $(grep -Ec '^[[:space:]]*\[Fan2\][[:space:]]*$' "$fan_conf") == 1 )) ||
  fail "T2 migration adds exactly one second-fan section"
grep -Fq $'systemctl\tdisable\t--now\ttiny-dfr.service' "$calls" ||
  fail "T2 migration disables tiny-dfr"
grep -Fq $'omarchy-pkg-drop\ttiny-dfr' "$calls" ||
  fail "T2 migration removes tiny-dfr"
grep -Fxq 'limine-mkinitcpio' "$calls" ||
  fail "T2 migration rebuilds the boot image"
[[ -f $repair_marker ]] || fail "T2 migration records the machine-wide repair"
pass "T2 migration repairs existing installs"

: >"$calls"

PATH="$stub_bin:$PATH" \
  TEST_LOG="$calls" \
  T2_HARDWARE=1 \
  TINY_DFR_INSTALLED=0 \
  OMARCHY_T2_LIMINE_CONF="$limine_conf" \
  OMARCHY_T2_FAN_CONF="$fan_conf" \
  OMARCHY_T2_RUNNING_CMDLINE="$running_cmdline" \
  OMARCHY_T2_REPAIR_MARKER="$repair_marker" \
  bash -euo pipefail "$migration" >/dev/null

(( $(grep -Ec '^[[:space:]]*\[Fan2\][[:space:]]*$' "$fan_conf") == 1 )) ||
  fail "T2 migration remains idempotent"
[[ ! -s $calls ]] || fail "an already repaired T2 install is left unchanged" "$(cat "$calls")"
pass "T2 migration is machine-idempotent before reboot"

rm -f "$repair_marker"
: >"$calls"

PATH="$stub_bin:$PATH" \
  TEST_LOG="$calls" \
  T2_HARDWARE=1 \
  TINY_DFR_INSTALLED=0 \
  OMARCHY_T2_LIMINE_CONF="$limine_conf" \
  OMARCHY_T2_FAN_CONF="$fan_conf" \
  OMARCHY_T2_RUNNING_CMDLINE="$running_cmdline" \
  OMARCHY_T2_REPAIR_MARKER="$repair_marker" \
  bash -euo pipefail "$migration" >/dev/null

grep -Fxq 'limine-mkinitcpio' "$calls" ||
  fail "T2 migration retries an interrupted boot image rebuild"
[[ -f $repair_marker ]] || fail "a retried T2 repair records completion"
! grep -Eq $'^(sudo\t)?(sed|tee|systemctl|omarchy-pkg-drop)(\t|$)' "$calls" ||
  fail "T2 rebuild retry leaves completed repair steps alone" "$(cat "$calls")"
pass "T2 migration retries an interrupted boot image rebuild"

cat >"$limine_conf" <<'EOF'
KERNEL_CMDLINE[default]+=" intel_iommu=on iommu=pt pcie_ports=compat"
EOF
printf '[Fan1]\n' >"$fan_conf"
: >"$calls"

PATH="$stub_bin:$PATH" \
  TEST_LOG="$calls" \
  T2_HARDWARE=0 \
  TINY_DFR_INSTALLED=1 \
  OMARCHY_T2_LIMINE_CONF="$limine_conf" \
  OMARCHY_T2_FAN_CONF="$fan_conf" \
  OMARCHY_T2_RUNNING_CMDLINE="$running_cmdline" \
  OMARCHY_T2_REPAIR_MARKER="$repair_marker" \
  bash -euo pipefail "$migration" >/dev/null

grep -q 'pcie_ports=compat' "$limine_conf" || fail "non-T2 Limine configuration is unchanged"
! grep -q '\[Fan2\]' "$fan_conf" || fail "non-T2 fan configuration is unchanged"
[[ ! -s $calls ]] || fail "non-T2 systems skip the repair" "$(cat "$calls")"
pass "T2 migration skips unrelated hardware"

# The previous block left the fixtures looking like an install the SIGPIPE bug
# skipped: stale Limine parameters, one fan section, and no repair marker. The
# rerun migration must complete the repair the original was marked as done for.
rerun_migration="$ROOT/migrations/1786137597.sh"
rm -f "$repair_marker"
: >"$calls"

PATH="$stub_bin:$PATH" \
  TEST_LOG="$calls" \
  T2_HARDWARE=1 \
  TINY_DFR_INSTALLED=0 \
  OMARCHY_PATH="$ROOT" \
  OMARCHY_T2_LIMINE_CONF="$limine_conf" \
  OMARCHY_T2_FAN_CONF="$fan_conf" \
  OMARCHY_T2_RUNNING_CMDLINE="$running_cmdline" \
  OMARCHY_T2_REPAIR_MARKER="$repair_marker" \
  bash -euo pipefail "$rerun_migration" >/dev/null

grep -Fq 'pm_async=off mem_sleep_default=deep' "$limine_conf" ||
  fail "T2 rerun migration updates the Limine suspend parameters"
(( $(grep -Ec '^[[:space:]]*\[Fan2\][[:space:]]*$' "$fan_conf") == 1 )) ||
  fail "T2 rerun migration adds the second-fan section"
grep -Fxq 'limine-mkinitcpio' "$calls" ||
  fail "T2 rerun migration rebuilds the boot image"
[[ -f $repair_marker ]] || fail "T2 rerun migration records the machine-wide repair"
pass "T2 rerun migration repairs installs the broken hardware check skipped"
