#! /bin/bash
set -e

# This "unpatch" script was automatically generated by
# dh_installkpatches from unpatch.tmpl $Revision: 1.10 $

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

ARCHITECTURE=`dpkg --print-installation-architecture`
PATCHNAME="ARM support for Linux version 2.4.16"
PATCHID=arm-2_4_16
PATCHARCH=arm

TOPPATCHDIR=/usr/src/kernel-patches
DECOMPRESSOR="zcat -f"

STAMP=debian/APPLIED_${PATCHARCH}_$PATCHID

[ -f $STAMP ] || exit 0

if [ ! -s $STAMP ]
then
    echo >&2 <<EOF
ERROR: $PATCHNAME patch was applied using old mechanism.
The relevant patch file may even not be on your system any more.
I strongly suggest you remove this kernel tree and unpack a clean one.
EOF
    exit 1
fi

if [ $(echo $(wc -l < $STAMP) ) != 1 ]
then
    . $STAMP
else
    # old format
    APPLY_INFO=$(cat $STAMP)
    PATCHFILE=$(echo ${APPLY_INFO} | cut -d# -f1)
    STRIPLEVEL=$(echo ${APPLY_INFO} | cut -d# -f2)
fi

PATCH_OPTIONS="--ignore-whitespace --silent"

if [ ! -r $PATCHFILE ]
then
    echo >&2 <<EOF
ERROR: applied $PATCHNAME patch file could not be found.
Presumably the package containing it was removed or upgraded.
I strongly suggest you remove this kernel tree and unpack a clean one.
EOF
    exit 1
fi

$DECOMPRESSOR $PATCHFILE | patch -R -p$STRIPLEVEL $PATCH_OPTIONS

# then remove those patches we depended on
if [ -n "$DEPENDS" ]
then
    echo "Un-applying the following patches first: $DEPENDS"
    for dep in $DEPENDS
    do
	if [ -x ${TOPPATCHDIR}/$dep ]
	then
	    ${TOPPATCHDIR}/$dep
	else
	    echo >&2 "Could not unpatch dependency: $dep - stopping here."
	    exit 1
	fi
    done
fi

echo >&2 "Removing empty files after unpatching:"
# make an exception for ./debian, or else the stamp files will go too.
find . -path ./debian -prune -o \
       -type f -size 0 ! -name 'APPLIED*' -exec rm {} \; -print
echo >&2 "Done."

rm -f debian/APPLIED_${PATCHARCH}_$PATCHID
rmdir -p debian || true
exit 0
