#! /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.33pl1.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

if ! test -f debian/APPLIED_$ARCHITECTURE_$PATCHNAME ; then
   exit 0 
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


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
