#!/bin/sh
#
#    apply-patch - apply the patch specified on the argument line,
#                  automatically detecting the patch level
#
#    Copyright (C) 2011 Dustin Kirkland <kirkland@ubuntu.com>
#
#    Authors:
#        Dustin Kirkland <kirkland@ubuntu.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

# apply-patch [PATCHFILE|URL]

P=
DIFF="$1"

# If $1 is a URL, grab it!
case "$1" in
	http://*|https://*|ftp://*)
		DIFF=$(mktemp)
		trap "rm -f $DIFF 2>/dev/null || true" EXIT HUP INT QUIT TERM
		if which wget >/dev/null 2>&1; then
			if ! wget -q -O"$DIFF" "$1"; then
				echo "ERROR: Unable to retrieve [$1]" 1>&2
				exit 1
			fi
		else
			echo "ERROR: Please install wget to use a URL with this tool" 1>&2
			exit 1
		fi
	;;
esac

# Try patch levels 0 - 16
for i in $(seq 0 16); do
	if patch -p$i --dry-run -i "$DIFF" 1>/dev/null 2>&1; then
		P=$i
		break
	fi
done

if [ -n "$P" ]; then
	exec patch -p$P -i "$DIFF"
else
	echo "ERROR: Unable to determine suitable patch strip level" 1>&2
	exit 1
fi
