#!/usr/bin/env bash
#
# Build and installs whatever is in /usr/src. Run step 1, reboot, then step 2.
set -euo pipefail
IFS=$'\n\t'
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cores=$(sysctl -n hw.ncpu)

if sudo etcupdate status | grep -qE '^  C '; then
    >&2 echo 'Conflicts remain in etcupdate. Run `etcupdate resolve` to fix them first.'
    exit 1
fi

cd /usr/src

make -j "$cores" clean
make -j "$cores" buildworld buildkernel
sudo make installkernel

echo "FreeBSD update step 1 done. Please reboot."