#! /bin/sh

set -e

# Hardcoded because it helps cross compiling and there's no reason for it to be anything
# other than m68k
ARCHITECTURE=m68k
PATCHNAME=2.0.36-native.diff.gz
EXTRAPATCH=2.0.36-extra.diff.gz
PATCHDIR=/usr/src/kernel-patches/$ARCHITECTURE

if ! test -d kernel -a -d Documentation ; then
    echo "Not in kernel top level directory. Exiting" >&2
    exit 1
fi

PATCH_VERSION=$(patch -v | head -1 | sed -e 's/[^0-9\.]//g')

if dpkg --compare-versions $PATCH_VERSION \>= 2.2 
then
    PATCH_OPTIONS="-l -s -z .native-orig -p1" 
else
    PATCH_OPTIONS="-l -s -b .native-orig -p1"
fi

if [ -f $PATCHDIR/$EXTRAPATCH ] ; then
  zcat $PATCHDIR/$EXTRAPATCH | patch -R $PATCH_OPTIONS 
fi
zcat $PATCHDIR/$PATCHNAME | patch -R $PATCH_OPTIONS

echo "Removing empty files after unpatching" >&2
find . -type f -size 0 -exec rm {} \; -print
rm -f debian/APPLIED_${ARCHITECTURE}_$PATCHNAME
exit 0
