#!/bin/sh

#
# Remove a file from a patch
#

. patchfns 2>/dev/null ||
. /usr/lib/patch-scripts/patchfns 2>/dev/null ||
. $PATCHSCRIPTS_LIBDIR/patchfns 2>/dev/null ||
{
	echo "Impossible to find my library 'patchfns'."
	echo "Check your install, or go to the right directory"
	exit 1
}

usage()
{
	echo "Usage: funpatch filename ..."
	exit 1
}

doit()
{
	patch_name=$(stripit $1)
	file_name=$2
	if is_applied_last $patch_name
	then
		true
	else
		if is_applied $patch_name
		then
			echo $patch_name is not the last-applied patch
			exit 1
		fi
	fi

	if ! file_in_patch $file_name $patch_name
	then
		echo File $file_name is not in patch $patch_name
		exit 1
	fi

	remove_file_from_patch $file_name $patch_name
}

if [ $# == 1 ]
then
	PATCH_NAME=$(top_patch)
	FILENAME=$1
	doit $PATCH_NAME $FILENAME
else
	usage
fi
