#! /bin/sh

set -e

PATCHDIR=/usr/src/kernel-patches/i386/2.2.17

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

if ! test -f debian_patches/APPLIED_i386_2.2.17_kernel-patch-2.2.17-pc9800 ; then
   echo -n "kernel-patch-2.2.17-pc9800 not yet applied ???"
   if [ "$1" = "-f" ]; then
     echo ", but force options specified."
   else
     echo "Aborting."
     exit 0 
   fi
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

for p in 'linux98-2.2.17.diffs.gz', 'zcat', 
do
  echo "Unpatching `basename $p`..." >&2
  =CAT= $PATCHDIR/`basename $p` | patch -R $PATCH_OPTIONS
done

echo "Removing empty files after unpatching" >&2
find . -type f -size 0 -exec rm {} \; -print
rm -f debian_patches/APPLIED_i386_2.2.17_kernel-patch-2.2.17-pc9800
rmdir debian_patches || true
exit 0
