#!/bin/sh -e

# (a copy of the former %pre script)
# when it was run via /usr/sbin/sh-safely, it was interpreted with just set -e

set -o pipefail

if [ -z "${TESTDIRECTORY-}" ]; then
    readonly TESTDIRECTORY="$(readlink -f "$(dirname "$0")")"
fi
# Usually expected location: %_datadir/%name/tests/run-tests
if [ -z "${RUNTESTS-}" ]; then
    readonly RUNTESTS="$TESTDIRECTORY"/run-tests
fi

# force the target arch for the tests
#
# By default, the packages would be built for the arch detected by rpm-build
# (rpmbuild --eval %_arch). On installation, they would be compared
# by rpm for compatibility with the arch detected by rpm. Currently,
# the mismatch in the detection between rpm and rpm-build can lead to problems,
# at least, on armh. So, we set the target by force to a value that must work.
system_arch="$(rpm -q rpm --qf='%{ARCH}')"
[ -n "$system_arch" ]
export APT_TEST_TARGET="$system_arch"

# to be rpm --import'ed
APT_TEST_GPGPUBKEY=1
export APT_TEST_GPGPUBKEY

# cache built pkgs and other stuff
# (CLEAN UP is on you!!! If run via /usr/sbin/sh-safely, it must set up
# and clean up $TMPDIR. FIXME: maybe just clean up in this script,
# but this will loose information for debugging afterwards.)
APT_TEST_INTERMEDIATES="$(mktemp -d)"
export APT_TEST_INTERMEDIATES

. "$RUNTESTS".defaults.sh

all_unique_methods=('' $(for method in "${APT_TEST_ALL_METHODS[@]}" "${APT_TEST_ALL_http_METHODS[@]}"; do echo "$method"; done | sort -u))
readonly -a all_unique_methods

# Below we run the same tests many times in order to possibly catch
# bad races. (It's more probable to catch a race under heavy load;
# so, we do simultaneously as many as reasonable, possibly more than real nprocs.)

# To not run in parallel, run with NPROCS_FOR_CHECK=
# Consider multiplying `nproc` by 2 for heavier load.
NPROCS=`nproc`
if ! [ "$NPROCS" -gt 0 ] 2>/dev/null; then
	NPROCS=1
fi
NPROCS=$(( 2 * NPROCS )) # for heavier load
${NPROCS_FOR_CHECK+NPROCS=$NPROCS_FOR_CHECK}

already_once=0
job=0
# Repeat all, so that every one gets tested at least once under maximal load;
# this is the purpose of extra_job counter.
readonly EXTRA_JOBS=$((1 * NPROCS)) # just an arbitrary num of extra jobs at the end
for (( extra_job = 0; extra_job < EXTRA_JOBS; )); do
    for method in "${all_unique_methods[@]}"; do
	# We could do the same method several times by increasing the number here
	# (to provoke even more races), but there are already too many tests.
	for (( repeat = 0; repeat < 1; ++repeat )); do
	    # %02d in order not to pass spaces in xargs' {} placeholder
	    printf '%02d:%s\n' "$((job++))" "$method"
	    if (( already_once && (++extra_job >= EXTRA_JOBS) )); then
		break 2
	    fi
	done
    done
    already_once=1
done >jobs

sed -i -Ee "s,^([^:]+):,\1/$job:," jobs

export NPROCS # for the embedded script (to show the total number of slots)
export RUNTESTS
xargs <jobs \
      -d'\n' -I'{}' ${NPROCS:+-P$NPROCS --process-slot-var=PARALLEL_SLOT} \
      -- sh -efuo pipefail \
	  -c 'APT_RUN_TEST_ONLY_IF_METHOD_MATCHES={}
              APT_RUN_TEST_ONLY_IF_METHOD_MATCHES="${APT_RUN_TEST_ONLY_IF_METHOD_MATCHES#*:}"
              export APT_RUN_TEST_ONLY_IF_METHOD_MATCHES
              "$RUNTESTS" -v '${NPROCS:+'|& sed --unbuffered -e "s,^,[$(printf %2d $PARALLEL_SLOT)/$NPROCS {}] ,"'}
