#! /bin/bash
set -e

# This "apply" script was automatically generated by
# dh_installkpatches from apply.tmpl $Revision: 1.20 $

# Environment variables that will impact this script:
# KPATCH_linux_ppscsi_2_4_10
#    if set to a kernel version for which you have a linux-ppscsi-2_4_10 patch,
#    it will be used instead of the version for your kernel.

# TODO:
# * ${Kernel:versions} substvar

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

ARCHITECTURE=`dpkg --print-installation-architecture`
KVERSIONS=(2.4.10 2.4.18 2.4.19 2.4.20)
PATCHFILES=(/usr/src/kernel-patches/all/linux-ppscsi-2_4_10/linux-ppscsi.patch.gz /usr/src/kernel-patches/all/linux-ppscsi-2_4_10/linux-ppscsi.patch.gz /usr/src/kernel-patches/all/linux-ppscsi-2_4_10/linux-ppscsi.patch.gz /usr/src/kernel-patches/all/linux-ppscsi-2_4_10/linux-ppscsi.patch.gz)
STRIPLEVELS=(1 1 1 1)
DEPENDS=("" "" "" "")

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

# This is informational only, used by lskpatches
DHPKPATCHES_VERSION=0.99.17

# Idempotence: stop with success if already applied
[ -f debian/APPLIED_${ARCHITECTURE}_linux-ppscsi-2_4_10 -o \
  -f debian/APPLIED_all_linux-ppscsi-2_4_10 ] && exit 0

TOPPATCHDIR=/usr/src/kernel-patches

# 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_linux_ppscsi_2_4_10" -a "$v" = "$KPATCH_linux_ppscsi_2_4_10" \
         -o "$v" = "$KERNELRELEASE" -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_linux_ppscsi_2_4_10" -a ${KVERSIONS[$IDX]} != "$KPATCH_linux_ppscsi_2_4_10" ]
then
    echo >&2 "Requested kernel version \`$KPATCH_linux_ppscsi_2_4_10' not found for patch linux-ppscsi-2_4_10"
    exit 1
fi

###

if [ "$IDX" ]
then

    # Check for dependencies

    NEEDED_DEPS=
    for dep in ${DEPENDS[$IDX]}
    do
	if [ -f debian/APPLIED_${ARCHITECTURE}_$dep -o \
	     -f debian/APPLIED_all_$dep ]
	then
	    echo "Patch dependency \`$dep' already applied - good"
	    continue
	elif [ -x ${TOPPATCHDIR}/${ARCHITECTURE}/${VERSION}.${PATCHLEVEL}.${SUBLEVEL}/apply/$dep ]
	then
	    NEEDED_DEPS="${ARCHITECTURE}/${VERSION}.${PATCHLEVEL}.${SUBLEVEL}/apply/$dep $NEEDED_DEPS"
	elif [ -x ${TOPPATCHDIR}/all/${VERSION}.${PATCHLEVEL}.${SUBLEVEL}/apply/$dep ]
	then
	    NEEDED_DEPS="all/${VERSION}.${PATCHLEVEL}.${SUBLEVEL}/apply/$dep $NEEDED_DEPS"
	elif [ -x ${TOPPATCHDIR}/${ARCHITECTURE}/apply/$dep ]
	then
	    NEEDED_DEPS="${ARCHITECTURE}/apply/$dep $NEEDED_DEPS"
	elif [ -x ${TOPPATCHDIR}/all/apply/$dep ]
	then
	    NEEDED_DEPS="all/apply/$dep $NEEDED_DEPS"
	else
	    echo >&2 "ERROR: Patch dependency \`$dep' not found - aborting"
	    exit 1
	fi
    done

    # Apply them (not really, at least not now)

    if [ "$NEEDED_DEPS" ]
    then
	echo "Applying the following patches first: $NEEDED_DEPS"
	for apply in ${NEEDED_DEPS}
	do
	    ${TOPPATCHDIR}/$apply

	    # check something was applied
	    if [ ! -f debian/APPLIED_${ARCHITECTURE}_$dep -a \
		 ! -f debian/APPLIED_all_$dep ]
	    then
		echo >&2 "ERROR: patch dependency did not left a patch stamp (version mismatch ?) - aborting"
		exit 1
	    fi
	done
	UNPATCHDEPS=$(echo ${NEEDED_DEPS} | sed s,/apply/,/unpatch/,g)
    fi

    ### Now for this patch

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

    echo >&2 "Testing whether "drivers for SCSI emulation over parallel port" 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 ""drivers for SCSI emulation over parallel port" 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_linux-ppscsi-2_4_10 <<EOF
PATCHFILE='$PATCHFILE'
STRIPLEVEL='$STRIPLEVEL'
DEPENDS='$UNPATCHDEPS'
EOF

	    # have this patch registered in doc-dir
	    mkdir -p debian/image.d
	    PKGNAME=`dpkg -S $PATCHFILE | cut -d: -f1`
	    PKGVERS=`grep-status -n -P $PKGNAME -s Version`
	    sed < '/usr/share/debhelper/dh-kpatches/register-patch' > 'debian/image.d/register-linux-ppscsi-2_4_10' \
		-e 's|@PATCHNAME@|"drivers for SCSI emulation over parallel port"|g' \
		-e 's/@PATCHID@/linux-ppscsi-2_4_10/g' \
		-e "s/@PKGNAME@/$PKGNAME/g" \
		-e "s/@PKGVERS@/$PKGVERS/g"
	    chmod +x 'debian/image.d/register-linux-ppscsi-2_4_10'
	else
	    # This should never happen, thanks to the dry-run
	    echo >&2 "ASSERTION FAILED - "drivers for SCSI emulation over parallel port" patch for $KVERSION failed"
	    exit 1
	fi
    else
	echo >&2 ""drivers for SCSI emulation over parallel port" patch for $KVERSION does not apply cleanly"
    fi
fi

###

exit 0
