# Config file: slackwareaarch64-<ver>/source/k/build_altsrc_kernel.conf/sc8280xp
# 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>
# Modified by Brenton Earl <el0226@slackware.com> for SoC sc8280xp
#
# 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'
export altkernelbranch=lenovo-x13s-linux-6.6.y 
export alt_web_repo=https://github.com/steev/linux.git
export alt_git_repo=/data/sc8280xp-lenovo-thinkpad-x13s/gentoo/linux
export pkgbuildtag=1_sc8280xp # kernel_armv8-6.x.y-aarch64-1_sc8280xp.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 ;}

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
   
   # patch kernel with debian/mobian patches
   # reies on -A being set to point to the series file
   # We patch beeore make defconfig because there are defconfig and dtb related changes within
   # the patch set that must be ran before make rockchip_defconfig. i.e. the rockchip_defconfig
   # is patched into the 6.1 kernel, as it is upstream in later kernel versions.
   # At the time of this writing, kernel 6.1.y was the default kernel in
   # slackware aarch64 post-stable. 
   #cat ${alt_base}/series | while read line ; do patch -p1 < ${alt_base}/$line ; done 

   # This uses the debian/mobian defconfig for rockchip SoC 
   make ARCH=arm64 laptop_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 ;}

# Keep this as it could come in handy.
# 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} && cp -fv ${alt_src}/linux-*/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}" ) ;}
