#! /bin/sh
set -eu

prev_output=""
prev_ts=""
: "${filter:=*}"

ulimit -c 0

for make in \
    $(ls bin/make-$filter-plain) \
    /usr/bin/make \
    $(ls "$HOME/proj/src/usr.bin/make/make" 2>/dev/null); do
	make_bin="$make"
	case "$make" in
	/*) ;;
	*) make_bin="$PWD/$make";;
	esac

	ts="$(echo "$make" | sed 's,.*\([0-9][0-9][0-9][0-9]\.[0-9][0-9].[0-9][0-9.]*\).*,\1,')"

	curr_output=$( ("$make_bin" "$@" && exit 0) 2>&1 \
	    && echo "exit status $?" \
	    || echo "exit status $?"
	)
	curr_output=$(printf '%s\n' "$curr_output" | sed "s,${make##*/},make,g")
	if [ "$prev_output" != "$curr_output" ]; then
		if [ "$prev_ts" ]; then
			printf '%s\n\n' "$prev_ts"
		fi
		printf '%s\n' "$ts"
		printf '%s\n' "$curr_output" | sed 's,^,| ,'
		prev_output="$curr_output"
	fi
	prev_ts="$ts"
done