#!/bin/bash

# Build RPi Kernel fork packages and deposit them within the correct
# directory ready for publishing.
# mozes@slackware.com, July 2023.

source /usr/share/slackdev/buildkit.sh

# Move new packages in to place unless instructed otherwise:
function mvpkgs() {
[ -z "$1" ] && {
   pushd ${tmpdir} || exit 1
   find . -path './kernels' -prune -o -type f -print0 | xargs -0i mv -fv '{}' ${publishdir}/ ;}
   popd
}

tmpdir=$TMP/packages-rpi/rpi-kernel-fork
rm -rf $tmpdir
mkdir -vpm755 $tmpdir

publishdir=${publishdir:-$HOME/armedslack/experimental/${SLACKWAREVERSION}/rpi-kernel-fork/}
[ ! -d "${publishdir}" ] && { echo "ERROR: can't find ${publishdir}" ; exit 1 ;}
echo "Publishing dir: ${publishdir}"

# Display the Kernel version available in the online repo, and our existing package set
# within the local publishing directory.  This enables us to check if we're up to date.
./build_altsrc_kernel.sh -D
echo
[ -z "$1" ] && {
   ls ${publishdir}
   read -p "Press ENTER to continue" ;}

# Setup the location to receive the new Kernel packages:
rm -rf "${publishdir}"
mkdir -vpm755 "${publishdir}"

# Build the RPi4 Kernel packages:
./build_altsrc_kernel.sh -H rpi4 -WP ${tmpdir} || failmake
mvpkgs

# Build the RPi5 Kernel packages:
./build_altsrc_kernel.sh -H rpi5 -WP ${tmpdir} || failmake
mvpkgs

# Re-install the generic Kernel, otherwise this build machine won't be able to boot because
# the RPi Kernel modules will be present within /lib/modules.  RPi's are not build machines.
upgradepkg --reinstall --install-new $PKGSTORE/a/kernel*arm*z
