#! /usr/local/bin/bash

#  This script is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.
#
#  See the COPYING and AUTHORS files for more details.

# Read in library functions
if [ "$(type -t patch_file_name)" != function ]
then
	if ! [ -r $QUILT_DIR/scripts/patchfns ]
	then
		echo "Cannot read library $QUILT_DIR/scripts/patchfns" >&2
		exit 1
	fi
	. $QUILT_DIR/scripts/patchfns
fi

declare -a opt_files=()

usage()
{
	printf $"Usage: quilt patches [-v] {file} [files...]\n"
	if [ x$1 = x-h ]
	then
		printf $"
Print the list of patches that modify any of the specified files. (Uses a
heuristic to determine which files are modified by unapplied patches.
Note that this heuristic is much slower than scanning applied patches.)

-v	Verbose, more user friendly output.

--color[=always|auto|never]
	Use syntax coloring (auto activates it only if the output is a tty).
"
		exit 0
	else
		exit 1
	fi
}

# Uses global variable opt_files
scan_applied()
{
	local color=$1 prefix=$2
	shift 2
	local patch file match

	for patch in "$@"
	do
		match=
		for file in "${opt_files[@]}"
		do
			if [ -f "$(backup_file_name $patch "$file")" ]
			then
				match=1
				break
			fi
		done

		[ -z "$match" ] || echo "$color$prefix$(print_patch $patch)$color_clear"
	done
}

# Note that we can only guess which files a patch touches because of the
# complicated heuristics that patch uses (see "Multiple Patches in a File"
# in the diff info pages).

touched_by_patch()
{
	local strip=$1 patch=$2
	[ $strip = ab ] && strip=1
	cat_file $(patch_file_name $patch) \
	|  awk '
	/^(\+\+\+|---)[ \t]/ {
		sub(/^(\+\+\+|---)[ \t]/, "")
		sub(/[ \t][^ \t]*$/, "")
		sub(/^\/dev\/null/, "")
		for (i=0; i<'$strip'; i++)
			sub(/^[^\/]*\//, "")
		if ($0 != "")
			print
	}'
}

# Uses global variable opt_files
scan_unapplied()
{
	local color=$1 prefix=$2 strip
	shift 2
	local patch file file_bre match

	for patch in "$@"
	do
		strip=$(patch_strip_level $patch)
		[ "$strip" = ab ] && strip=1

		match=
		for file in "${opt_files[@]}"
		do
			file_bre="$(quote_bre "$file")"
			if touched_by_patch $strip $patch \
			   | grep -q "^$file_bre\$"
			then
				match=1
				break
			fi
		done

		[ -z "$match" ] || echo "$color$prefix$(print_patch $patch)$color_clear"
	done
}

options=`getopt -o vh --long color:: -- "$@"`

if [ $? -ne 0 ]
then
	usage
fi

eval set -- "$options"

while true
do
	case "$1" in
	-v)
		opt_verbose=1
		shift ;;
	--color)
		case "$2" in
		"" | always)
			opt_color=1 ;;
		auto | tty)
			opt_color=
			[ -t 1 ] && opt_color=1 ;;
		never)
			opt_color= ;;
		*)
			usage ;;
		esac
		shift 2 ;;
	-h)
		usage -h ;;
	--)
		shift
		break ;;
	esac
done

if [ $# -lt 1 ]
then
	usage
fi
while [ $# -ge 1 ]
do
	opt_files[${#opt_files[@]}]="$SUBDIR$1"
	shift
done

top=$(top_patch)

if [ -n "$opt_verbose" ]
then
	applied="+ "
	current="= "
	unapplied="  "
else
	applied=""
	current=""
	unapplied=""
fi

[ -n "$opt_color" ] && setup_colors

setup_pager

scan_applied "$color_series_app" "$applied" \
	$(patches_before $top)
[ -n "$top" ] && \
	scan_applied "$color_series_top" "$current" \
		$top
scan_unapplied "$color_series_una" "$unapplied" \
	$(patches_after $top)

### Local Variables:
### mode: shell-script
### End:
# vim:filetype=sh
