set -euo pipefail

sudo apt update
sudo apt install -y \
  git build-essential bc bison flex libelf-dev libssl-dev \
  libncurses-dev fakeroot dwarves pahole rsync xz-utils cpio kmod wget

mkdir -p ~/src
cd ~/src

if [ ! -f linux-7.0.tar.xz ]; then
  wget -c https://cdn.kernel.org/pub/linux/kernel/v7.x/linux-7.0.tar.xz
fi

rm -rf linux-7.0
tar -xf linux-7.0.tar.xz
cd linux-7.0

cp -v /boot/config-$(uname -r) .config
make listnewconfig || true
yes "" | make olddefconfig

scripts/config --set-str LOCALVERSION "-huey-core-7.0"
scripts/config --disable DEBUG_INFO
scripts/config --disable DEBUG_INFO_BTF
scripts/config --disable KASAN
scripts/config --disable UBSAN
scripts/config --disable KCOV
scripts/config --disable FUNCTION_TRACER
scripts/config --enable ZSTD
scripts/config --enable RD_ZSTD
scripts/config --enable EFI
scripts/config --enable EFI_STUB
make olddefconfig

make -j"$(nproc)" bindeb-pkg
cd ..
ls -1 ./*.deb
sudo dpkg -i ./linux-headers-7.0.0*-huey-core-7.0*_amd64.deb \
             ./linux-image-7.0.0*-huey-core-7.0*_amd64.deb
sudo update-grub

echo
echo "Next step:"
echo "Rebuild and stage the iMac 5K 2017 audio-fix modules for the new kernel before first full adoption."
