#!/bin/bash

# Avoid bash localization of error messages
export LC_ALL=C

ebegin() { printf '%s*%s %s ... ' "${GOOD}" "${NORMAL}" "$*" ; }
eend() {
	local r=${1:-0}
	shift
	if [[ $r -eq 0 ]] ; then
		printf '[ %sok%s ]\n' "${GOOD}" "${NORMAL}"
	else
		printf '%s [ %s!!%s ]\n' "$*" "${BAD}" "${NORMAL}"
	fi
	return $r
}
die() { echo "$*" 1>&2; exit 1; }

vars=( CHOST GCC_CONFIG ROOT TROOT NOCOLOR RC_NOCOLOR )
unset ${vars[@]}

TROOT=${PWD}
GCC_CONFIG="${TROOT}/../.gcc-config"
if ! "${GCC_CONFIG}" --help >/dev/null ; then
	die "could not find gcc-config in ${TROOT}/../"
fi
PATH="${TROOT}/..:${PATH}"

NOCOLOR="true"
RC_NOCOLOR="yes" # Older baselayout/openrc versions.
# This CHOST value doesn't matter.  It could be anything.
# As long as all the configs we test are based on this being
# the CHOST value.
CHOST="x86_64-pc-linux-gnu"

cmp_log() {
	local ret log=$1 exp=$2
	local v args=()
	for v in "${vars[@]}" ; do
		args+=( -e "s:@${v}@:${!v}:g" )
	done
	args+=( -e "s|: line [0-9]*: |: |g" )
	sed "${args[@]}" "${exp}" > "${exp}.tmp"
	sed "${args[@]}" "${log}" > "${log}.tmp"
	diff -uw "${exp}.tmp" "${log}.tmp" > "${log}.diff"
	ret=$?
	rm "${exp}.tmp"
	return ${ret}
}

rm -f */*.log* */*.runit
rm -rf TMP-*-rw-*

if [[ $# -eq 0 ]] ; then
	set -- */test*
else
	set -- "${@/%//test*}"
	set -- "${@//\/\///}"
fi

max_jobs=$(getconf _NPROCESSORS_ONLN)
jobs=()
tret=0
for t in "$@" ; do
	[[ ${t} == *.exp ]] && continue

	(
	out=$(
	(
	ebegin "Running ${t}"

	test=${t##*/}
	tdir=${t%/*}
	if [[ ${tdir} == rw-* ]] ; then
		cp -pPR "${tdir}" "TMP-${test}-${tdir}"
		tdir="TMP-${test}-${tdir}"
	fi

	ROOT=${TROOT}/${tdir}

	r="${ROOT}/${test}.runit"
	cat <<-EOF > "${r}"
	#!/bin/bash
	cd "${ROOT}"
	$(for v in PATH ${vars[@]} ; do printf '%s="%s"\n' ${v} "${!v}" ; done)
	export ${vars[@]}
	gcc-config() { bash -c ". \${GCC_CONFIG}" gcc-config "\$@" ; }
	. ./${test}
	EOF
	chmod a+rx "${r}"

	log=${ROOT}/${test}.log
	"${r}" >& "${log}"
	ret=$?
	reason="exit failure"

	if [[ ${ret} -eq 0 ]] ; then
		reason="log difference"
		cmp_log "${log}" "${log%.log}.exp"
		ret=$?
	fi
	if eend ${ret} "due to ${reason}; see ${log}" ; then
		rm -f "${log}"* "${r}"
		[[ ${tdir} == TMP-${test}-rw-* ]] && rm -rf "${tdir}"
	else
		: $(( tret += $? ))
	fi

	) 2>&1
	)
	echo "${out}"
	) &

	# Run all the tests in parallel.
	jobs+=( $! )
	if [[ ${#jobs[@]} -ge ${max_jobs} ]] ; then
		wait ${jobs[0]}
		: $(( tret += $? ))
		jobs=( ${jobs[@]:1} )
	fi
done
for j in ${jobs[@]} ; do
	wait ${j}
	: $(( tret += $? ))
done
exit ${tret}
