#!/usr/bin/env bash

# Our custom diff tool which normalizes result and expected files before
# doing the actual diff. Rules for normalization are in normalize.sed. See
# the comments there for more information.
#
# Note that src/test/regress/Makefile adds this directory to $PATH so
# pg_regress uses this diff tool instead of the system diff tool.
set -eu -o pipefail

file1="${@:(-2):1}"
file2="${@:(-1):1}"
# pg_regress passes the expected file as the first argument ($file1 above),
# and results file as the second argument ($file2 above). We take the base
# filename of the expected file as the test name. We can have multiple
# expected files for a single test with _0, _1, ... suffixes.
# So for the test name, we also strip the additional suffix.
test=$(basename "$file1" .out | sed -E "s/_[0-9]+$//")
args=${@:1:$#-2}
BASEDIR=$(dirname "$0")

DIFF=/usr/bin/diff
if [ ! -f "$DIFF" ]
then
	# whereis searches for standard unix places before $PATH. So select the
	# first entry as the original diff tool.
	# With the default WSL2 configuration whereis is very slow though ~400ms,
	# so we only use it if /usr/bin/diff does not exist.
	DIFF=$(whereis diff | sed "s/diff://g" | awk '{print $1}')
	if [ -z "$DIFF" ]
	then
		echo "ERROR: could not find diff command" 1>&2
		exit 1
	fi
fi

if test -z "${VANILLATEST:-}"
then
	if [[ ! -e "$file1" ]]
	then
		touch "$file1"  # when adding a new test the expected file does not exist
	fi
	normalize_file="$BASEDIR/normalize.sed"
	# when running tests on an existing cluster some changes need to be done on
	# normalize.sed file. So a new file is used.
	if [[ -f "$BASEDIR/normalize_modified.sed" ]]
	then
		normalize_file="$BASEDIR/normalize_modified.sed"
	fi
	sed -Ef "$normalize_file" < "$file1" > "$file1.modified"
	sed -Ef "$normalize_file" < "$file2" > "$file2.modified"
	if [[ "$test" == "seclabel" ]]
	then
		# Node activation emits these independent metadata commands in heap-scan order.
		python3 - "$file1.modified" "$file2.modified" <<'PY' || exit $?
import pathlib
import re
import sys

header = "SELECT 1 FROM citus_add_node('localhost', :worker_2_port);\n"
pair = r"NOTICE:  issuing [^\n]*\nDETAIL:  [^\n]*\n"
block = re.compile(f"({re.escape(header)})((?:{pair})+)")
for name in sys.argv[1:]:
    path = pathlib.Path(name)
    text = path.read_text()
    text = block.sub(lambda m: m[1] + "".join(sorted(re.findall(pair, m[2]))), text)
    path.write_text(text)
PY
		"$DIFF" -w $args "$file1.modified" "$file2.modified"
		exit $?
	fi
	"$DIFF" -w $args "$file1.modified" "$file2.modified" | LC_CTYPE=C.UTF-8 diff-filter "$BASEDIR/normalize.sed"
	exit ${PIPESTATUS[0]}
else
	exec "$DIFF" -w $args "$file1" "$file2"
fi

