#! /bin/bash
set -e

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

# Environment variables that will impact this script:
# KPATCH_lpp
#    if set to a kernel version for which you have a lpp 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.17 2.4.18)
PATCHFILES=(/usr/src/kernel-patches/all/lpp/patch-lpp.gz /usr/src/kernel-patches/all/lpp/patch-lpp.gz)
STRIPLEVELS=(1 1)
DEPENDS=("" "")

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


# Idempotence: stop with success if already applied
[ -f debian/APPLIED_${ARCHITECTURE}_lpp -o \
  -f debian/APPLIED_all_lpp ] && 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_lpp" -a $v = "$KPATCH_lpp" \
         -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_lpp" -a ${KVERSIONS[$IDX]} != "$KPATCH_lpp" ]
then
    echo >&2 "Requested kernel version \`$KPATCH_lpp' not found for patch lpp"
    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
	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 "Linux Progress Patch" 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 ""Linux Progress Patch" 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_lpp <<EOF
PATCHFILE='$PATCHFILE'
STRIPLEVEL='$STRIPLEVEL'
DEPENDS='$UNPATCHDEPS'
EOF
	else
	    # This should never happen, thanks to the dry-run
	    echo >&2 "ASSERTION FAILED - "Linux Progress Patch" patch for $KVERSION failed"
	    exit 1
	fi
    else
	echo >&2 ""Linux Progress Patch" patch for $KVERSION does not apply cleanly"
    fi
fi

###

exit 0
