#!/bin/bash
source /usr/share/slackdev/buildkit.sh

# Kernel dev status:
# https://web.git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git/

# Prepare updated Kernel config files for a new Kernel release.
#
# Run this script from source/k/sources
# <mozes@slackware.com>

TMP=/tmp/kernel-update
# If >8GB RAM available in /dev/shm, use that.
# This requires /dev/shm being mounted without 'noexec' option.
# Note: we don't use the 'shm_tmp' function from slackkit because that uses a
# global directory, and we often run this script whilst packages are building.
if ! findmnt -o options /dev/shm | grep -q noexec; then
   [ $( df --output=avail /dev/shm | tail -n1 ) -gt $((8 * 1024 * 1024)) ] && TMP=/dev/shm${TMP}
fi
echo "Temporary space: ${TMP}"

rm -rf $TMP
mkdir -pm755 $TMP
export CWD=$PWD

ARMBUILDSCRIPT=$CWD/../arm/build

#xz -vd linux-*tar.xz || exit 1

# Obtain existing kernel version from the arm/build script:
eval $( grep '^export VERSION=.*' $ARMBUILDSCRIPT | sed -e 's?export VERSION=? armbuildVER=?' ) || { echo "Failed to find VERSION in arm/build.. running this script from the wrong dir?" ; exit 1;}
armbuildVER="${armbuildVER/-/_}" # handle version numbers with hyphens that are switched with underscores within the SlackBuild
echo "Existing kernel version (set in 'arm/build' script): $armbuildVER"
echo "Unpacking Kernel source..."

cd $TMP
mkdir srcunpack
pushd srcunpack
  # Back these up incase:
  cp -fa $CWD/../configs .
  tar xf $CWD/linux-*.tar.?z || exit 1
  cd linux-*/ || exit 1
  # The Kernel archive version doesn't necessarily match the version
  # exported by the Kernel itself:
  NEWKERNVER="$( sed -ne's/^VERSION *= *//p' Makefile).$(sed -ne's/^PATCHLEVEL *= *//p' Makefile).$(sed -ne's/^SUBLEVEL *= *//p' Makefile )"
  echo "New Kernel version: $NEWKERNVER"
#  ../../patches/applier
  $CWD/patches/applier || { echo "ERROR: patch application failure" ; exit 1;}

  for i in $CWD/../configs/config* ; do
    # The Kernel uses 'arm' and 'arm64' rather than 'aarch64',
    # so let's match the last segment of our config file names to the base platform type:
    # e.g. "config-armv7", "config-armv8"
    case ${i##*/*-} in
      armv7) LINUXKERNELARCH=arm ;;
      armv8) LINUXKERNELARCH=arm64 ;;
    esac

    echo "==== Kernel config file: $i "
    echo "==== Architecture..... : $LINUXKERNELARCH "

    cp -fav $i .config || exit 1

    make ARCH=$LINUXKERNELARCH oldconfig || { echo "ERROR: make oldconfig" ; exit 1;}

    # Apply x86 makeoldconfig issue:
    # Isn't applied by "patches/applier" script, since this edits the .config file
    # which is copied later.
    # Btw, the reason we copy the config later, is because some config options are provided by
    # patches.  If we apply the matches _after_ make oldconfig, those options are removed from .config
    #
    # Dec-2019: I realised how fast it is to do this on ARM now, so I'll do the Kernel stuff natively.
    # and we can dump this patch.
#    xzcat ../../patches/no-autopatch-makeoldconfig-x86.patch.xz | patch --verbose -p0 || exit

    cp -fav .config $i

    # Menuconfig? .config changes will need to be copied manually
    # I don't think this will work - untested.
    if [ "$1" = "M" ]; then
       make ARCH=$LINUXKERNELARCH menuconfig || { echo "ERROR: menuconfig failed" ; exit 1;}
    fi
  done || exit 1
popd

echo "Updating arm/build script with new Kernel version"
sed -i '/^export VERSION/ s?'"$armbuildVER"'?'"$NEWKERNVER"'?g' $ARMBUILDSCRIPT || \
  { echo "ERROR: failed to update version in ${ARMBUILDSCRIPT}" ; exit 1;}

if [ "$1" = "z" ]; then
   read -p "Now press ctrl+z to suspend, edit, then fg will delete src & exit this script"
   echo "Deleting extracted source, then exiting."
   pwd
   rm -rf srcunpack
   exit
fi

# Repack with LZMA and extreme compression (kernel.org doesn't use
# extreme compression):
cd $CWD
#echo "Repacking Linux source - will take some time.."
#xz -vvez9 linux*.tar

if [ ! -z "$1" ]; then
   echo "You can now wipe srcunpack when you're ready: $TMP"
 else
   rm -rf $TMP
#   rm -rf srcunpack
fi
