#! /bin/bash
#
# File automatically generated from unpatch.tmpl $Revision: 1.6 $ by dh_installkpatches
set -e

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="IRC connection tracking #PATCHNAME# NAT"
PATCHID=irc
PATCHARCH=i386

DECOMPRESSOR="zcat -f"

[ -f debian/APPLIED_${PATCHARCH}_$PATCHID ] || exit 0

if [ ! -s debian/APPLIED_${PATCHARCH}_$PATCHID ]
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

APPLY_INFO=$(cat debian/APPLIED_${PATCHARCH}_$PATCHID)
PATCHFILE=$(echo ${APPLY_INFO} | cut -d# -f1)
STRIPLEVEL=$(echo ${APPLY_INFO} | cut -d# -f2)

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

# # META: what's this ?? backup ?
# if dpkg --compare-versions $PATCH_VERSION \>= 2.2
# then
#     PATCH_OPTIONS="$PATCH_OPTIONS -z"
# else
#     PATCH_OPTIONS="$PATCH_OPTIONS -b"
# fi

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

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
