#!/bin/sh
#
# Copyright 2001 Wichert Akkerman

set -e

thispatch=ipmi-kcs
stamp=debian/APPLIED_i386_$thispatch
patchdir=/usr/src/kernel-patches/i386/$thispatch

if [ ! -d kernel -a -d Documentation ] ; then
	echo "This does not seem to be a kernel source directory." >&2
	exit 1
fi

if [ -f $stamp ] ; then
	echo "The $thispatch patch is already applied"
	exit 0
fi

version=$(sed -ne 's/^VERSION[[:space:]]\+=[[:space:]]\([^[:space:]]\+\).*/\1/p' Makefile)
patchlevel=$(sed -ne 's/^PATCHLEVEL[[:space:]]\+=[[:space:]]\([^[:space:]]\+\).*/\1/p' Makefile)
sublevel=$(sed -ne 's/^SUBLEVEL[[:space:]]\+=[[:space:]]\([^[:space:]]\+\).*/\1/p' Makefile)

ps="$patchdir/$version.$patchlevel"
pe="_$thispatch.patch"

for ptc in "$ps.$sublevel$pe" "$ps$pe" ; do
	if [ -f "$ptc" ] ; then
		patch="$ptc"
		echo "Will use $ptc to patch the kernel"
		break
	fi
done

if [ -z "$patch" ] ; then
	echo "No $thispatch patch for this kernel found"
	exit 1
fi

patch -p1 -s -l -N < "$patch"
mkdir -p debian
echo -n "$patch" > $stamp

exit 0

