#!/bin/sh
#
# Script to apply and remove the Debian/GNU Linux m68k 2.0.36 kernel patches.
#
# Nick Holgate <holgate@debian.org>, 17th Feb 1999
#

patchver=2.0.36
patchdir=/usr/src/kernel-patches/m68k
nativepatch=${patchver}-native.diff.gz
macpatch=${patchver}-mac.diff.gz
vmepatch=${patchver}-vme.diff.gz

prog=$(basename $0)

usage()
{
	echo "Usage: $prog [options] [architecture]"
	echo "Function: Apply or remove m68k $patchver sub-architecture patches"
	echo "          to i386 $patchver kernel source tree in current directory."
	echo " -h               Display this help message"
	echo " -r               Remove previously applied patch"
	echo " -f               Force application or removal of patch"
	echo " architecture     Patch sub-architecture one of: native, mac or vme"
	echo "                   use 'native' for atari and amiga."
	echo
}

force=0
reverse=0
subarch=""
while getopts :hrf opt ; do
	case "$opt" in
	f) force=1 ;;
	r) reverse=1 ;;
	h) usage; exit 0 ;;
	*) usage; echo -e "Unknown option \"-$OPTARG\".\n"; exit 1 ;;
	esac
done

shift $(($OPTIND - 1))

if [ "$1" != "" ]; then
	subarch=$1
	shift
	if [ "$1" != "" ]; then
		usage; echo -e "Unexpected arguments: $*\n"; exit 1
	fi
fi

if ! test -d kernel -a -d Documentation ; then
	echo
    echo "Not in kernel top level directory."
	echo
    exit 1
fi

eval $(sed -n 's/^\([A-Z]*\)[[:space:]]*=[[:space:]]*\([0-9]*\)[[:space:]]*$/\1=\2/p' Makefile)
if [ -z "$VERSION" -o -z "$PATCHLEVEL" -o -z "$SUBLEVEL" ]; then
	echo
    echo "Unable to determine current kernel version."
	echo
    exit 1
fi

kernver=$VERSION.$PATCHLEVEL.$SUBLEVEL

if [ "$patchver" != "$kernver" ]; then
	echo
	echo "Kernel version is $kernver, but patches are for $patchver"
	echo
	exit 1
fi

# determine source tree architecture from Makefile
ARCH=$(sed -n 's/^ARCH[[:space:]]*=[[:space:]]*\([[:alnum:]]*\)[[:space:]]*$/\1/p' Makefile)

# determine which patches have been applied already
if [ -f applied_m68k_patch ]; then
	applied=$(head -n 1 applied_m68k_patch)
else
	applied=""
fi

if [ $force -eq 0 ]; then
	if [ $reverse -ne 0 ]; then
		if [ "$applied" == "" ]; then
			echo
			echo "Kernel source does not seem to be patched, use -f option to force."
			echo
			exit 1
		fi
		if [ "$subarch" == "" ]; then
			subarch=$applied
		else
			echo
			echo "Specified architecture does not match applied patch, use -f option to force."
			echo
			exit 1
		fi
	else
		if [ "$applied" != "" ]; then
			echo
			echo "Kernel source seems to already be patched for $applied, use -f option to force"
			if [ "$applied" != "$subarch" ]; then
				echo "or -r option to remove existing patch first."
			fi
			echo
			exit 1
		fi
	fi	
fi	

if [ "$subarch" == "" ]; then
	usage; echo -e "Must specify architecture.\n"; exit 1
fi

case "$subarch" in
	vme) extrapatch=$vmepatch ;;
	mac) extrapatch=$macpatch ;;
	native) extrapatch="" ;;
	*) usage; echo -e "Unknown architecture \"$subarch\".\n"; exit 1 ;;
esac

if [ $force -eq 0 ]; then
	if [ $reverse -ne 0 ]; then
		if [ "$ARCH" != "m68k" ]; then
			echo
			echo "Kernel source appears to be $ARCH, but patches should be"
			echo "removed from an m68k source tree, use -f option to force."
			echo
			exit 1
		fi
	else
		if [ "$ARCH" != "i386" ]; then
			echo
			echo "Kernel source appears to be $ARCH, but patches should be"
			echo "applied to an i386 source tree, use -f option to force."
			echo
			exit 1
		fi
	fi
fi

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

if dpkg --compare-versions $PATCH_VERSION \>= 2.2 
then
    PATCH_OPTIONS="-l -s -z .native-orig -p1"
else
    PATCH_OPTIONS="-l -s -b .native-orig -p1"
fi

if [ $reverse -ne 0 ]; then
	echo "Removing $subarch patch ..."
	if [ "$extrapatch" != "" ] ; then
		if ! zcat $patchdir/$extrapatch | patch -R $PATCH_OPTIONS
		then
			echo "failed."; exit 1
		fi
	fi
	if ! zcat $patchdir/$nativepatch | patch -R $PATCH_OPTIONS
	then
		echo "failed."; exit 1
	fi
else
	echo "Applying $subarch patch ..."
	if ! zcat $patchdir/$nativepatch | patch $PATCH_OPTIONS 
		then
			echo "failed."; exit 1
		fi
	if [ "$extrapatch" != "" ] ; then
		if ! zcat $patchdir/$extrapatch | patch $PATCH_OPTIONS 
		then
			echo "failed."; exit 1
		fi
	fi
fi

echo "Removing empty files after patching ..."
find . -type f -size 0 -exec rm {} \; -print

if [ $reverse -ne 0 ]; then 
	rm -f applied_m68k_patch
else
	echo $subarch >applied_m68k_patch
fi

exit 0

