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

set -e

PATCHNAME=international
PATCHFILE=int-patch.gz
PATCHDIR=/usr/src/kernel-patches/all

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_all_$PATCHNAME ; then
   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/')

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

PATCH_OPTIONS="-l -s -p1"

# If a prepatch exists unapply it:
if test -f $PATCHDIR/pre-int-patch-$VERSION.$PATCHLEVEL.$SUBLEVEL.gz ; then
  zcat $PATCHDIR/pre-int-patch-$VERSION.$PATCHLEVEL.$SUBLEVEL.gz | patch  -R $PATCH_OPTIONS
fi

zcat $PATCHDIR/$PATCHFILE | patch -R $PATCH_OPTIONS
echo "Removing empty files after unpatching" >&2
find . -type f -size 0 -exec rm {} \; -print
rm -f debian/APPLIED_all_$PATCHNAME
[ -d debian ] && rmdir -p debian
exit 0
