<commit_before>#!/bin/sh

usage()
{
	cat <<EOF 
$(basename ${0}) [OPTION] TEST_FILE

--builddir DIR  specify where tools can be found
--srcdir DIR    specify where the source tree resides
--hash PROG     use hasher PROG instead of md5sum
--husk PROG     use husk around tool, e.g. 'valgrind -v'

-h, --help      print a short help screen
EOF
}

for arg; do
	case "${arg}" in
	"-h"|"--help")
		usage
		exit 0
		;;
	"--builddir")
		builddir="${2}"
		shift 2
		;;
	"--srcdir")
		srcdir="${2}"
		shift 2
		;;
	"--hash")
		hash="${2}"
		shift 2
		;;
	"--husk")
		HUSK="${2}"
		shift 2
		;;
	--)
		shift
		testfile="${1}"
		break
		;;
	"-"*)
		echo "unknown option ${arg}" >&2
		shift
		;;
	*)
		testfile="${1}"
		;;
	esac
done

## setup
fail=0
tool_stdout=$(mktemp "/tmp/tmp.XXXXXXXXXX")
tool_stderr=$(mktemp "/tmp/tmp.XXXXXXXXXX")

## source the check
. "${testfile}" || fail=1

rm_if_not_src()
{
	file="${1}"
	srcd="${2:-${srcdir}}"
	dirf=$(dirname "${file}")

	if test "${dirf}" -ef "${srcd}"; then
		## treat as precious source file
		:
	elif test "$(pwd)" -ef "${srcd}"; then
		## treat as precious source file
		:
	else
		rm -vf -- "${file}"
	fi
}

myexit()
{
	rm_if_not_src "${stdin}" "${srcdir}"
	rm_if_not_src "${stdout}" "${srcdir}"
	rm_if_not_src "${stderr}" "${srcdir}"
	rm -f -- "${tool_stdout}" "${tool_stderr}"
	exit ${1:-1}
}

xrealpath()
{
	readlink -f "${1}" 2>/dev/null || \
		realpath "${1}" 2>/dev/null
}

find_file()
{
	file="${1}"
<commit_msg>fix test suite, emulate readlink -f on systems where it's missed<commit_after>#!/bin/sh

usage()
{
	cat <<EOF 
$(basename ${0}) [OPTION] TEST_FILE

--builddir DIR  specify where tools can be found
--srcdir DIR    specify where the source tree resides
--hash PROG     use hasher PROG instead of md5sum
--husk PROG     use husk around tool, e.g. 'valgrind -v'

-h, --help      print a short help screen
EOF
}

for arg; do
	case "${arg}" in
	"-h"|"--help")
		usage
		exit 0
		;;
	"--builddir")
		builddir="${2}"
		shift 2
		;;
	"--srcdir")
		srcdir="${2}"
		shift 2
		;;
	"--hash")
		hash="${2}"
		shift 2
		;;
	"--husk")
		HUSK="${2}"
		shift 2
		;;
	--)
		shift
		testfile="${1}"
		break
		;;
	"-"*)
		echo "unknown option ${arg}" >&2
		shift
		;;
	*)
		testfile="${1}"
		;;
	esac
done

## setup
fail=0
tool_stdout=$(mktemp "/tmp/tmp.XXXXXXXXXX")
tool_stderr=$(mktemp "/tmp/tmp.XXXXXXXXXX")

## source the check
. "${testfile}" || fail=1

rm_if_not_src()
{
	file="${1}"
	srcd="${2:-${srcdir}}"
	dirf=$(dirname "${file}")

	if test "${dirf}" -ef "${srcd}"; then
		## treat as precious source file
		:
	elif test "$(pwd)" -ef "${srcd}"; then
		## treat as precious source file
		:
	else
		rm -vf -- "${file}"
	fi
}

myexit()
{
	rm_if_not_src "${stdin}" "${srcdir}"
	rm_if_not_src "${stdout}" "${srcdir}"
	rm_if_not_src "${stderr}" "${srcdir}"
	rm -f -- "${tool_stdout}" "${tool_stderr}"
	exit ${1:-1}
}

xrealpath()
{
	readlink -f "${1}" 2>/dev/null || \
	realpath "${1}" 2>/dev/null || \
	( cd "$(dirname "${1}")" || exit 1
		tmp_target="$(basename "${1}")"
		# Iterate down a (possible) chain of symlinks
		while test -L "${tmp_target}"; do
			tmp_target="$(readlink "${tmp_target}")"
			cd "$(dirname "${tmp_target}")" || exit 1
			tmp_target="$(basename "${tmp_target}")"
		done
		echo "$(pwd -P || pwd)/${tmp_target}"
	) 2>/dev/null
}

find_file()
{
	file="${1}"

{'fraction_non_alphanumeric': 0.2242934455802766, 'fraction_numerical': 0.016536380036079375, 'mean_word_length': 3.211392405063291, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2436273', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1357, 'n_words': 356}