#! /bin/sh

set -e

#ARCHITECTURE=`dpkg --print-architecture`
#PATCHNAME=2.0.35pl1.diff.gz
PATCHDIR=/usr/src/kernel-patches/i386

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_i386_linux98-2.2.12.diffs.gz ; then
   echo "linux98-2.2.12.diffs.gz not yet applied ???"
   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/linux98-2.2.12.diffs.gz | patch -R $PATCH_OPTIONS


echo "Removing empty files after unpatching" >&2
find . -type f -size 0 -exec rm {} \; -print
rm -f debian/APPLIED_i386_linux98-2.2.12.diffs.gz
exit 0
