#!/bin/sh

# This is part on ALT Linux tests environment based on Tapper
# (c) 2013 Andrey Cherepanov <cas@altlinux.org>
# License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.

. shell-error

prg_version="1.1.4"

tests_path="/usr/lib/alt-test"
tests_program="prove"

# Default parameters
VERBOSE_OUTPUT=""
FAILED_OUTPUT=""
tests=""

show_tests()
{
	find "$tests_path" -mindepth 1 -maxdepth 1 -type f -name '*.t' | sed 's/^.*\///' | sed 's/\.t$//' | sort
	exit 0
}

show_usage()
{
	cat << _EOF_
alt-test is utility for Tapper test run. Version ${prg_version}
(c) 2013 Andrey Cherepanov <cas@altlinux.org>
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.

Usage: alt-test [option] [test1 ...]

Options:
	-l    list all available tests (from ${tests_path})
	-h    show help
	-f    show only failed tests
	-v    run with verbose output
	--version show version

Supported environment variables:
 TEST_PACKAGE_SOURCE  ISO image for existing package checks
_EOF_
	exit 0;
}

show_version()
{
	echo "$prg_version"
	exit 0
}

for i in $@
do
	case "$i" in
		-l)
			show_tests
			;;
		-h)
			show_usage
			;;
		-v)
			VERBOSE_OUTPUT="-v"
			;;
		-f)
			FAILED_OUTPUT="1"
			;;
		--version)
			show_version
			;;
		*)
			# add test to list
			if [ -e "$tests_path/${i}.t" ] ; then
				tests="$tests $tests_path/${i}.t"
			else
				if [ -e "$i" ] ; then
					tests="$tests $i"
				else
					fatal "Test '$i' is not found" >&2
				fi
			fi
	esac
done

test -z "$tests" && show_tests

# Run tests
if [ -n "$FAILED_OUTPUT" ] ; then
	$tests_program -v $tests | grep '^not ok '
else
	$tests_program $VERBOSE_OUTPUT $tests 
fi
