#!/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=2
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

# A quick test with just one method
APT_TEST_METHODS='file' APT_TEST_http_METHODS= "$RUNTESTS" -v

# The same tests, but just via cdrom with a missing release:
#APT_TEST_METHODS=cdrom_missing_release APT_TEST_http_METHODS= "$RUNTESTS" -v
