#!/bin/sh
# DF-2425 -- fix validation (run on HOST).
# 1. apply fix.diff to the guest /usr/src copy
# 2. rebuild the rig module (includes the patched production amd.c)
# 3. rebuild + install the GENERIC kernel (amd is compiled in) and reboot
set -e
cd "$(dirname "$0")"
SSH="ssh -F ../../../dfbsd-qemu/config -o ConnectTimeout=8 -o BatchMode=yes dfbsd /bin/sh"
SCP="scp -F ../../../dfbsd-qemu/config"

echo "== 0. (re)create rig module dir =="
printf 'mkdir -p /usr/src/sys/modules/amdrig\n' | $SSH
$SCP -q Makefile amdrig.c dfbsd:/usr/src/sys/modules/amdrig/

echo "== 1. apply fix.diff to guest /usr/src =="
$SCP -q fix.diff dfbsd:/tmp/fix.diff
printf 'cd /usr/src && patch -p1 --dry-run < /tmp/fix.diff && patch -p1 < /tmp/fix.diff\n' | $SSH 2>&1 | tee fix-apply.log

echo "== 2. rebuild rig module against patched source =="
printf 'cd /usr/src/sys/modules/amdrig && make cleandir >/dev/null 2>&1; make 2>&1 | tail -2\n' | $SSH 2>&1 | tee fix-module-build.log

echo "== 3. rebuild + install GENERIC kernel (amd is compiled in) =="
printf 'cd /usr/src && make -j6 buildkernel KERNCONF=X86_64_GENERIC 2>&1 | tail -3 && make installkernel KERNCONF=X86_64_GENERIC 2>&1 | tail -3\n' | $SSH 2>&1 | tee fix-kernel-build.log
