#! /bin/bash
#
# File automatically generated from apply.tmpl $Revision: 1.8 $ by dh_installkpatches

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

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`
PATCHID=irc
PATCHNAME="IRC connection tracking #PATCHNAME# NAT"
PATCHARCH=i386
KVERSIONS=( 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7)
PATCHFILES=( /usr/src/kernel-patches/i386/irc/irc-2.4.3.gz /usr/src/kernel-patches/i386/irc/irc-2.4.4.gz /usr/src/kernel-patches/i386/irc/irc-2.4.5.gz /usr/src/kernel-patches/i386/irc/irc-2.4.6.gz /usr/src/kernel-patches/i386/irc/irc-2.4.7.gz)
STRIPLEVELS=( 1 1 1 1 1)
DEPENDS=

DECOMPRESSOR="zcat -f"

[ -f debian/APPLIED_${ARCHITECTURE}_$PATCHID -o \
  -f debian/APPLIED_all_$PATCHID ] && 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}

###

# Check for dependencies

NEEDED_DEPS=
for dep in $DEPENDS
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="$NEEDED_DEPS ${ARCHITECTURE}/${VERSION}.${PATCHLEVEL}.${SUBLEVEL}/apply/$dep"
    elif [ -x ${TOPPATCHDIR}/all/${VERSION}.${PATCHLEVEL}.${SUBLEVEL}/apply/$dep ]
    then
        NEEDED_DEPS="$NEEDED_DEPS all/${VERSION}.${PATCHLEVEL}.${SUBLEVEL}/apply/$dep"
    elif [ -x ${TOPPATCHDIR}/${ARCHITECTURE}/apply/$dep ]
    then
        NEEDED_DEPS="$NEEDED_DEPS ${ARCHITECTURE}/apply/$dep"
    elif [ -x ${TOPPATCHDIR}/all/apply/$dep ]
    then
        NEEDED_DEPS="$NEEDED_DEPS all/apply/$dep"
    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 "Would need to apply the following patches: $NEEDED_DEPS"
    exit 1
fi

###

PATCH_VERSION=$(patch -v | head -1 | sed -e 's/[^0-9\.]//g')

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

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

###

# Find nearest patch-supported kversions enclosing $KERNELRELEASE

CUR_IDX=
NEXT_IDX=
PREV_IDX=

declare -i i=${#PATCHFILES[*]}-1
while [ $i -ge 0 ]
do
    v=${KVERSIONS[$i]}
    if [ $v = $KERNELRELEASE ]
    then
	CUR_KVERS=$v
	CUR_IDX=$i
    elif dpkg --compare-versions $v '>>' $KERNELRELEASE
    then
	if [ -z "$NEXT_IDX" ] || dpkg --compare-versions $v '<<' $NEXT_KVERS
	then
	    NEXT_KVERS=$v
	    NEXT_IDX=$i
	fi
    elif dpkg --compare-versions $v '<<' $KERNELRELEASE
    then
	if [ -z "$PREV_IDX" ] || dpkg --compare-versions $v '>>' $PREV_KVERS
	then
	    PREV_KVERS=$v
	    PREV_IDX=$i
	fi
    fi
    i=i-1
done

###

for i in $CUR_IDX $NEXT_IDX $PREV_IDX
do
    if [ "$i" ]
    then
	KVERSION=${KVERSIONS[$i]}
	PATCHFILE=${PATCHFILES[$i]}
	STRIPLEVEL=${STRIPLEVELS[$i]}

	echo >&2 "Testing whether $PATCHNAME patch for $KVERSION applies (dry run):"
	# We must check that patchfile exists, or the pipe will return 0
	# even though $DECOMPRESSOR returns 1.  Is this a bash bug ?
	if [ -r $PATCHFILE ] && $DECOMPRESSOR $PATCHFILE |
		patch --batch --dry-run $PATCH_OPTIONS -p$STRIPLEVEL
	then
	    if $DECOMPRESSOR $PATCHFILE |
		    patch $PATCH_OPTIONS -p$STRIPLEVEL
	    then
		echo >&2 "$PATCHNAME patch for $KVERSION succeeded"
		echo >&2 "Removing empty files after patching:"
		# 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."

		# keep enough infos for unpatching
		mkdir -p debian
		echo "$PATCHFILE#$STRIPLEVEL" \
		    > debian/APPLIED_${PATCHARCH}_$PATCHID 
		break
	    else
		echo >&2 "$PATCHNAME patch for $KVERSION failed"
		exit 1
	    fi
	else
	    echo >&2 "$PATCHNAME patch for $KVERSION does not apply cleanly"
	fi
    fi
done

###

exit 0
