#!/bin/bash
#
# Copyright, the authors of the Linux man-pages project
# SPDX-License-Identifier: GPL-3.0-or-later

set -Eefuo pipefail;
shopt -s lastpipe;

# Defaults:
s='';
u='-u';
w='';

err()
{
	>&2 echo "$(basename "$0"): error: $*";
	exit 1;
}

while test $# -ge 1; do
	case "$1" in
	-s)
		s='-s';
		;;
	-U)
		if test $# -lt 2; then
			err "-U: Missing argument.";
		fi;
		u="-U$2";
		shift;
		;;
	-U*)
		u="$1";
		;;
	-w)
		w='-w';
		;;
	--)
		break;
		;;
	-*)
		err "$1: Unknown option.";
		;;
	*)
		break;
		;;
	esac;
	shift;
done;

n=0;
if test $# -ge 1 && test "$1" != '--'; then
	commit1="$1^";
	commit2="$1";
	n=1;
	shift;
fi;
if test $# -ge 1 && test "$1" != '--'; then
	commit1="$commit2";
	commit2="$1";
	n=2;
	shift;
fi;
if test $# -ge 1; then
	if test "$1" != '--'; then
		err "Too many arguments.";
	fi;
	shift;
fi;

git rev-parse --show-toplevel | read -r dir;
cd "$dir";

test -v MAN_KEEP_FORMATTING || export MAN_KEEP_FORMATTING=1;
test -v MANROFFOPT          || export MANROFFOPT='-d AD=l';

# shellcheck disable=SC2206  # We want only non-empty variables in the array.
opts=($s $w $u);

case $n in
0)  git diff --name-only -- "$@";			;;
*)  git diff --name-only "$commit1..$commit2" -- "$@";	;;
esac \
| grep -E '(\.[[:digit:]]([[:alpha:]][[:alnum:]]*)?\>|\.man)+(\.man|\.in)*$' \
| sortman \
| while read -r f; do \
	case $n in
	0)  old="HEAD:$f";      new="./$f";         ;;
	*)  old="$commit1:$f";  new="$commit2:$f";  ;;
	esac;

	case $n in
	0)  cat "$new";       ;;
	*)  git show "$new";  ;;
	esac \
	| man /dev/stdin \
	| diff -F'^[^ ]' --label "$old" --label "$new" "${opts[@]}" \
		<(git show "$old" | man /dev/stdin) \
		/dev/stdin \
	|| true;
done;
