#! /bin/bash
set -e

if ! [ -d kernel -a -d Documentation ]
then
    echo >&2 "Not in kernel top level directory. Exiting"
    exit 1
fi
TOPPATCHDIR=/usr/src/kernel-patches
ARCHITECTURE=`dpkg --print-installation-architecture`
DECOMPRESSOR="zcat -f"
PATCH_OPTIONS="--ignore-whitespace --silent"
# This is informational only, used by lskpatches
DHPKPATCHES_VERSION=0.99.31

DEPENDS=("" "" "" "" "")

KVERSIONS=(2.4.20 2.4.21 2.4.22 2.4.23 2.4.24)
PATCHFILES=("/usr/src/kernel-patches/diffs/supermount-ng/supermount-1.2.11-2.4.20.patch.gz" "/usr/src/kernel-patches/diffs/supermount-ng/supermount-1.2.11-2.4.21.patch.gz" "/usr/src/kernel-patches/diffs/supermount-ng/supermount-1.2.11-2.4.22.patch.gz" "/usr/src/kernel-patches/diffs/supermount-ng/supermount-1.2.11-2.4.23.patch.gz" "/usr/src/kernel-patches/diffs/supermount-ng/supermount-1.2.11-2.4.23.patch.gz")
DEBPATCHFILES=("" "" "" "" "")
STRIPLEVELS=(1 1 1 1 1)

[ -f debian/APPLIED_${ARCHITECTURE}_supermount-ng -o \
  -f debian/APPLIED_all_supermount-ng ] && exit 0
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}
IDX=

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

if [ -n "$KPATCH_supermount_ng" -a ${KVERSIONS[$IDX]} != "$KPATCH_supermount_ng" ]
then
    echo >&2 "Requested kernel version \`$KPATCH_supermount_ng' not found for patch supermount-ng"
    exit 1
elif [ -z "$IDX" ]
then
    echo >&2 "No \"supermount-ng patch for the Linux kernel\" patch found for kernel version $KERNELRELEASE"
    exit 1
fi
KVERSION=${KVERSIONS[$IDX]}
STRIPLEVEL=${STRIPLEVELS[$IDX]}

if [ "${DEBPATCHFILES[$IDX]}" != '' -a \
    \( -r version.Debian -o -r README.Debian \) ]
then
    PATCHFILE=${DEBPATCHFILES[$IDX]}
else
    PATCHFILE=${PATCHFILES[$IDX]}
fi

echo >&2 "START applying patch \"supermount-ng patch for the Linux kernel\""

NEEDED_DEPS=
for dep in ${DEPENDS[$IDX]}
do
    if [ -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"
        echo >&2 "END applying patch \"supermount-ng patch for the Linux kernel\""
        exit 1
    fi
done
if [ "$NEEDED_DEPS" ]
then
    echo >&2 "Ensuring the following patches are applied 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"
            echo >&2 "END applying patch \"supermount-ng patch for the Linux kernel\""
            exit 1
        fi
    done
    UNPATCHDEPS=$(echo ${NEEDED_DEPS} | sed s,/apply/,/unpatch/,g)
fi

echo >&2 "Testing whether \"supermount-ng patch for the Linux kernel\" patch for $KVERSION applies (dry run):"
if ! [ -r $PATCHFILE ]
then
    echo >&2 "\"supermount-ng patch for the Linux kernel\" patch for $KVERSION not found"
    exit 1
elif ! $DECOMPRESSOR $PATCHFILE |
        patch --force --dry-run $PATCH_OPTIONS -p$STRIPLEVEL
then
    echo >&2 "\"supermount-ng patch for the Linux kernel\" patch for $KVERSION does not apply cleanly"
    exit 1
fi
if ! $DECOMPRESSOR $PATCHFILE |
        patch $PATCH_OPTIONS -p$STRIPLEVEL
then
    # This should never happen, thanks to the dry-run
    echo >&2 "ASSERTION FAILED - \"supermount-ng patch for the Linux kernel\" patch for $KVERSION failed"
    echo >&2 "END applying patch \"supermount-ng patch for the Linux kernel\""
    exit 1
fi
echo >&2 "\"supermount-ng patch for the Linux kernel\" patch for $KVERSION succeeded"

echo >&2 "Removing empty files after patching:"
find .  -path ./debian -prune -o \
        -type f -size 0 ! -name 'APPLIED*' -exec rm {} \; -print
echo >&2 "Done."

mkdir -p debian
cat > 'debian/APPLIED_all_supermount-ng' <<EOF
PATCHFILE='$PATCHFILE'
STRIPLEVEL='$STRIPLEVEL'
DEPENDS='$UNPATCHDEPS'
EOF
mkdir -p debian/image.d
PKGNAME=`dpkg -S $PATCHFILE | cut -d: -f1`
PKGVERS=`grep-dctrl -n -P $PKGNAME -s Version -X /var/lib/dpkg/status`
cat > 'debian/image.d/register-supermount-ng' <<EOF
#!/bin/sh

# This scripts documents the "supermount-ng patch for the Linux kernel" kernel patch into the
# kernel-image package, as being applied to the kernel.

docdir=\${IMAGE_TOP}/usr/share/doc/kernel-image-\${version}

mkdir -p \${docdir}

(
    printf 'supermount-ng patch for the Linux kernel (supermount-ng)${KPATCH_supermount_ng:+ for kernel ${KPATCH_supermount_ng}},'
    echo ' from package $PKGNAME, version $PKGVERS'
) >> \${docdir}/applied-patches
EOF
chmod +x 'debian/image.d/register-supermount-ng'

echo >&2 "END applying patch \"supermount-ng patch for the Linux kernel\""
