#!/bin/bash

# Prepare updated Kernel config files & recompress the Kernel source
# for maximum compression:
#
# Run this script from source/k/sources

ntpdate rolex.ripe.net

TMP=/tmp/kernel-update
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 || 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

    make ARCH=$LINUXKERNELARCH 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
  done || exit 1
popd

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

echo "Updating arm/build script with new Kernel version"
sed -i '/^export VERSION/ s?'"$armbuildVER"'?'"$NEWKERNVER"'?g' $ARMBUILDSCRIPT

# 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 [ "$1" = "n" ]; then
   echo "You can now wipe srcunpack when you're ready: $TMP"
 else
   rm -rf $TMP
#   rm -rf srcunpack
fi
