#!/bin/bash

# Download the RPi vendor fork so that we can poke around it.
alt_srcstore=/tmp/rpi-kfork
rm -rf $alt_srcstore
mkdir -p $alt_srcstore
chown -R nobody:nobody $alt_srcstore

altkernelbranch=rpi-6.18.y
alt_web_repo=https://raw.githubusercontent.com/raspberrypi/linux/${altkernelbranch}
alt_git_repo=https://github.com/raspberrypi/linux.git

echo "Downloading alt Kernel fork..."
cd $alt_srcstore || exit 1
su - nobody \
-s /bin/bash \
-c "cd $alt_srcstore&& git clone --depth=1 --branch ${altkernelbranch} ${alt_git_repo} linux-${altkernelbranch}" || exit 1

find . -name '.git*' -print0 | xargs -0 rm -rf
# Rename the Kernel source directory back to the standard mainline naming convention:
mv -fv linux-* linux-$( echo "$(sed -ne's/^VERSION *= *//p' linux-*/Makefile).$(sed -ne's/^PATCHLEVEL *= *//p' linux-*/Makefile).$(sed -ne's/^SUBLEVEL *= *//p' linux-*/Makefile)" )
cd linux*/ || exit 1

# This uses the default configuration for the SoC the RPi5 uses
make ARCH=arm64 bcm2712_defconfig || exit 1
# Switch the 'local version' to the standard Slackware ARM uses:
sed -i 's?^CONFIG_LOCALVERSION=.*?CONFIG_LOCALVERSION="-armv8"?g' .config
sed -i 's?^# CONFIG_LOCALVERSION_AUTO.*?CONFIG_LOCALVERSION_AUTO=y?g' .config
# Empty the Kernel cmdline settings so that the Slackware ARM standards are in use.
# By default the serial setting is set to ttyAMA0 where as Slackware uses ttyS0:
sed -i 's/^CONFIG_CMDLINE="[^"]*"/CONFIG_CMDLINE=""/' .config
