#!/bin/bash
shopt -s extglob

# For the allwinner H3 patch I took from fedora:
# Apply it to a fresh kernel.org kernel like this:
# cp -fa linux.x linux.x.orig
# cd linux.x.orig
# git apply -v /tmp/AllWinner-net-emac.patch
# then diff the original against the new one and save the output.
# This is so that the patch file works with the library in slackkit which
# doesn't accomodate git format patches that well.
# I chopped some of the arm64 stuff out of that patch since it didn't apply
# and wasn't relevant.

# Apply patches to the kernel source before running 'make oldconfig'
# since some of the patches change the kernel config files.

source /usr/share/slackdev/buildkit.sh

# NOTE: This script is called from the 'do-src' script within source/k/sources
# and this is what $CWD should expand to.

ls $CWD/patches/!(*debian*|*no-autopatch*)xz* > /dev/null 2>&1 || {
  echo "Note: No patches found to apply"
  exit 0 ;}

# List out the patches for the benefit of the build log:
for i in $CWD/patches/!(*debian*|*no-autopatch*)xz* ; do
   echo $i
done

export TMPBUILD=/tmp/papplier
rm -rf $TMPBUILD
mkdir -vpm755 $TMPBUILD

rm -f /tmp/plist-applied

for i in $CWD/patches/!(*debian*|*no-autopatch*)xz* ; do
   echo "*** Applying: $i ****" | tee -a /tmp/plist-applied
   auto_apply_patch $i || exit 1
done

# Apply the H3 ones:
#for i in ../patches/H3/* ; do xzcat $i | patch --verbose -p1 ; done

#tar xvvf ../patches/linux*debian*
#for i in debian/patches/features/arm/* ; do
#  auto_apply_patch $i || exit 1
#done

rm -rf $TMPBUILD
