#!/bin/sh

readonly patterns=$*

[ -f pkg-descr ] && WRKSRC=$(make -V WRKSRC) || WRKSRC="."
if [ "$WRKSRC" ]; then
	if [ ! -d "$WRKSRC" ]; then
		while true; do
			read -r -p "WRKSRC does not exist, run 'make configure' (y/n)? " answer
			case $answer in
			[Yy])
				make configure >/dev/null 2>&1
				break ;;
			[Nn])
				exit 0
			esac
		done
	fi
	cd "$WRKSRC" || exit 1
fi

p_files=$(find . -name '*.orig' | sed 's#^./##')
if [ ! "$p_files" ]; then
	echo "===> No patched files found"
	exit 1
fi

for f in $p_files; do
	if [ "$patterns" ]; then
		matched=0
		for p in $patterns; do
			case $f in *$p*)
				matched=1
				break
			esac
		done
		[ $matched -eq 1 ] || continue
	fi
	TZ=UTC diff -udp "$f" "${f%%.orig}" | sed \
			-e '/^---/s|\.[0-9]* +0000$| UTC|' \
			-e '/^+++/s|\([[:blank:]][-0-9:.+]*\)*$||'
done
