#! /bin/bash
set -e

### * This "apply" script was automatically generated by
### * dh_installkpatches from apply.tmpl Revision: 1.17
# It was modified manually to copy files instead of just applying a patch

# Environment variables that will impact this script:
# KPATCH_tekram_dc3x5
#    if set to a kernel version for which you have a tekram-dc3x5 patch,
#    it will be used instead of the version for your kernel.

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

TOPPATCHDIR=/usr/src/kernel-patches/all/tekram-dc3x5

ARCHITECTURE=`dpkg --print-installation-architecture`
KVERSIONS=(2.4 2.2 2.5 2.0)
PATCHFILES=(/usr/src/kernel-patches/all/tekram-dc3x5/dc395-integ24.diff.gz /usr/src/kernel-patches/all/tekram-dc3x5/dc395-integ22.diff.gz /usr/src/kernel-patches/all/tekram-dc3x5/dc395-integ25.diff.gz /usr/src/kernel-patches/all/tekram-dc3x5/dc395-integ20.diff.gz )
STRIPLEVELS=(1 1)
COPY_FILES="dc395x_trm.h dc395x_trm.c"

DECOMPRESSOR="zcat -f"
PATCH_OPTIONS="--ignore-whitespace --silent"


# Idempotence: stop with success if already applied
[ -f debian/APPLIED_${ARCHITECTURE}_tekram-dc3x5 -o \
  -f debian/APPLIED_all_tekram-dc3x5 ] && exit 0

# Get current kernel version number etc
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/')
EXTRAVERSION=$(grep ^EXTRAVERSION Makefile 2>/dev/null | \
	sed -e 's/EXTRAVERSION =[ 	]*\([^ 	]*\)$/\1/')
KERNELRELEASE=${VERSION}.${PATCHLEVEL}.${SUBLEVEL}${EXTRAVERSION}

###

# Find the patchfile to use (kernel's version, or env-specified one)

IDX=

declare -i i=${#PATCHFILES[*]}-1
while [ $i -ge 0 ]
do
    v=${KVERSIONS[$i]}
    if [ -n "$KPATCH_tekram_dc3x5" -a $v = "$KPATCH_tekram_dc3x5" \
         -o $v = ${VERSION}.${PATCHLEVEL} -o $v = all ]
    then
	IDX=$i
    fi
    i=i-1
done

# If a specified version was asked and it was not found, stop here
if [ -n "$KPATCH_tekram_dc3x5" -a ${KVERSIONS[$IDX]} != "$KPATCH_tekram_dc3x5" ]
then
    echo >&2 "Requested kernel version \`$KPATCH_tekram_dc3x5' not found for patch tekram-dc3x5"
    exit 1
fi

###

if [ "$IDX" ]
then
    ### Now for this patch

    KVERSION=${KVERSIONS[$IDX]}
    PATCHFILE=${PATCHFILES[$IDX]}
    STRIPLEVEL=${STRIPLEVELS[$IDX]}

    echo >&2 "Testing whether "Tekram DC315-DC395 SCSI Host support" patch for $KVERSION applies (dry run):"
    # We must check that patchfile exists, or the pipe will return 0
    # even though $DECOMPRESSOR returns 1.  Damn shell semantics.
    if [ -r $PATCHFILE ] && $DECOMPRESSOR $PATCHFILE |
	    patch --force --dry-run $PATCH_OPTIONS -p$STRIPLEVEL
    then
	# Don't use --force on second run, there should be no need
	# for it.  If something requires interaction, it's likely
	# there is a bug somewhere, better catch it.
	if $DECOMPRESSOR $PATCHFILE |
		patch $PATCH_OPTIONS -p$STRIPLEVEL
	then
	    echo >&2 ""Tekram DC315-DC395 SCSI Host support" patch for $KVERSION succeeded"
	    echo >&2 "Removing empty files after patching:"
	    # make an exception for ./debian/ contents, and for
	    # files named APPLIED*, or else some stamp files may
	    # be caught too.
	    find .  -path ./debian -prune -o \
		    -type f -size 0 ! -name 'APPLIED*' -exec rm {} \; -print
	    echo >&2 "Done."

	    # keep enough infos for unpatching
	    mkdir -p debian
	    cat > debian/APPLIED_all_tekram-dc3x5 <<EOF
PATCHFILE='$PATCHFILE'
STRIPLEVEL='$STRIPLEVEL'
COPY_FILES="${COPY_FILES[0]} ${COPY_FILES[1]}"
APPLYDIR='drivers/scsi'
EOF
	    # Now copy the files
		for file in $COPY_FILES; do
			cp -f $TOPPATCHDIR/$file drivers/scsi/
		done
	else
	    # This should never happen, thanks to the dry-run
	    echo >&2 "ASSERTION FAILED - "Tekram DC315-DC395 SCSI Host support" patch for $KVERSION failed"
	    exit 1
	fi
    else
	echo >&2 ""Tekram DC315-DC395 SCSI Host support" patch for $KVERSION does not apply cleanly"
    fi
fi

###

exit 0
