# Config file: slackwareaarch64-<ver>/source/k/build_altsrc_kernel.conf/rpi5
# Used by slackwareaarch64-<ver>/source/k/build_altsrc_kernel.sh
#
# This can be used as a reference for other Hardware Models.
# Stuart Winter <mozes@slackware.com>

# Copyright 2034  Stuart Winter, Earth, Milky Way, "".
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# These can be set with command line parameters: see '--help'
# Pick the one you want from the drop down menu here:
# https://github.com/raspberrypi/linux
export altkernelbranch=rpi-6.18.y
export alt_web_repo=https://raw.githubusercontent.com/raspberrypi/linux/${altkernelbranch}
export alt_git_repo=https://github.com/raspberrypi/linux.git
export pkgbuildtag=1_rpi5 # kernel_armv8-6.6.12-aarch64-1_rpi5.txz

# Parameters passed to 'kernel.SlackBuild'
# --noconfig = Don't use the generic Slackware ARM Kernel configuration.
#              Your alternate Kernel source typically will have its own Kernel config.
# --nopatches = Don't apply the Slackware ARM patch set to the Kernel source.
#               Slackware ARM includes patches for supported Hardware Models where appropriate for
#               application to the official Linux Kernel from kernel.org.
#               For alternate Kernel sources these patches most likely wouldn't apply or wouldn't
#               be required.
alt_kernelslackbuild_opts="--noconfig --nopatches"

# Download alt Kernel fork repo:
# Instead of downloading it, you could edit this function to unpack it into the temporary space instead.
function place_altkernel_fork_repo() {
   # tar xf $CWD/sources/linux-alt-src.tar.xz -C $alt_srcstore/
   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" ) || exit 1 ;}

# The Raspberry Pi Kernel fork is classified as 'dirty' because it contains code
# that isn't committed. Remove the git structure to prevent the RPi Kernel
# being labeled as such (because it breaks the Slackware ARM naming scheme for /lib/modules):
function prepare_altkernel_fork_repo() {
   echo "Preparing and cleaning alt Kernel fork..."
   pushd $alt_srcstore > /dev/null
      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)" )
   popd > /dev/null ;}

# Configure the alt Kernel fork:
function configure_altkernel_fork_repo() {
   echo "Configuring alt Kernel fork..."
   pushd $alt_srcstore/linux-*/ > /dev/null || exit 1

   # If you wanted to store your own local configuration, you could install it.
   # For more information on the '$alt_base' variable, see the notes around the patch function below.
   # install -vpm644 ${alt_base}/configs/config-hwm-rockpi5 .config || exit 1
   # make oldconfig

   # 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

   # Determine the Kernel version which would be reported by 'uname -r'
   # This may be used by the -W command line operator to this script to
   # create a text file containing this version.
   # Automated update scripts can consume this file to determine the currently available package.
   export kver=$( determine_kernelver )$( grep -E '^CONFIG_LOCALVERSION=' .config | cut -d= -f2- | tr -d \" )

   popd > /dev/null ;}

# Apply patches:
# In this example, you'd place your patches within
# slackwareaarch64-current/source/k/build_altsrc_kernel.basedir/patches
# This directory doesn't exist within the Slackware AArch64 tree - you'd create it
# within your local copy.
# You can override the directory location set within the variable '${alt_base}' through the
# command line operators to the 'build_altsrc_kernel.sh' script: -A|--altsrc-base
# This way you could maintain this outside of your local copy of the Slackware AArch64 source tree.
#
#function patch_altkernel_fork_repo() {
#   patch --verbose -p0 < ${alt_base}/patches/fix-whatever.diff || exit 1
#;}

# This is only used for the parameter:
# -D, --discover-altkernel-version
#   Discover the version of the Linux Kernel source from the 'Makefile'
#   within the online repository.  This saves downloading the source in its entirety.
# Note: For other Hardware Models you don't need this, so you can delete this function
# if you won't be using the command line option above.
function determine_kernelver_online() {
   ( cd $alt_tmpdir
     su - nobody \
       -s /bin/bash \
       -c "cd ${alt_tmpdir}&& wget -q ${alt_web_repo}/Makefile || exit 1" || exit 1
     [ ! -f Makefile ] && return 1
     kver=$( echo "$(sed -ne's/^VERSION *= *//p' Makefile).$(sed -ne's/^PATCHLEVEL *= *//p' Makefile).$(sed -ne's/^SUBLEVEL *= *//p' Makefile)" )
     echo "Kernel version in web repo ${alt_web_repo}: ${kver}" ) ;}

