#! /bin/sh
#
# (C) 1998 Manoj Srivastava & Eric Delaunay.

set -e

# wrong, but I'll figure it out later
ARCH=$(dpkg-architecture -qDEB_HOST_ARCH)
PATCHNAME=linux
PATCHDIR=/usr/src/kernel-patches/$ARCH

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_${ARCH}_$PATCHNAME ; then
   echo "Patch not applied, skipping"
   exit 0		# no need to remove a non existent patch
fi

VERSION=$(grep ^VERSION Makefile 2>/dev/null | \
                 sed -e 's/[^0-9]*\([0-9]*\)/\1/')
PATCHLEVEL=$(grep ^PATCHLEVEL Makefile 2>/dev/null | \
                    sed -e 's/[^0-9]*\([0-9]*\)/\1/')
SUBLEVEL=$(grep ^SUBLEVEL Makefile 2>/dev/null | \
                  sed -e 's/[^0-9]*\([0-9]*\)/\1/')

PATCHFILE=$PATCHNAME-$VERSION.$PATCHLEVEL.$SUBLEVEL.diff.gz
PATCH_VERSION=$(patch -v | head -1 | sed -e 's/[^0-9\.]//g')

PATCH_OPTIONS="-l -s -p1"

zcat $PATCHDIR/$PATCHFILE | patch -R $PATCH_OPTIONS
rm -f debian/APPLIED_${ARCH}_$PATCHNAME
exit 0
