#! /bin/bash
set -e

# This "unpatch" script was automatically generated by
# dh_installkpatches from unpatch.tmpl Revision: 1.10
# And modified by hand by Baruch Even <baruch@debian.org>

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="Tekram DC315-DC395 SCSI Host support"
PATCHID=tekram-dc3x5
PATCHARCH=all

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
for file in $COPY_FILES; do
	rm -f $APPLYDIR/$file
done

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
