#!/bin/sh

set -e -u

cp -va test "$AUTOPKGTEST_TMP"/
cd "$AUTOPKGTEST_TMP"

RET=0

# Loop over all supported Python and unrar versions
for py in $(py3versions -s); do
	for unrar in unrar-nonfree unrar-free; do
		echo "Running testsuite with $py and $unrar:"

		# Point the symlink at /usr/bin/unrar to the unrar of our choice
		sudo update-alternatives --set unrar "$(command -v $unrar)"
		update-alternatives --display unrar

		# Allow test runs to continue even in case of failures
		set +e

		if [ "$unrar" = "unrar-free" ]; then
			# Deselect tests broken by unrar-free's limited format support
			$py -m pytest -k "not ( \
				test_open_psw_late_rar5 or \
				test_open_pathlib_path or \
				test_read_psw_late_rar5 or \
				test_vols[test/files/rar3-old.rar] or \
				test_rar3_header_encryption or \
				test_reading[test/files/rar15-comment-lock.rar] or \
				test_reading[test/files/rar15-comment.rar] or \
				test_reading[test/files/rar202-comment-nopsw.rar] or \
				test_reading[test/files/rar202-comment-psw.rar] or \
				test_reading[test/files/rar3-solid.rar] or \
				test_reading[test/files/rar5-psw-blake.rar] or \
				test_reading[test/files/rar5-psw.rar] or \
				test_reading_rar3_hpsw or \
				test_reading_rar5_hpsw or \
				test_unrar_tool )" \
				test
		else
			$py -m pytest test
		fi

		# Record the exit status and make errors fatal again
		RET=$((RET+$?))
		set -e
	done
done

exit $RET
