#!/bin/bash
# run - run tests
#
# Usage:
#   ./run zzsenha.sh zzcores.sh   # test zzsenha and zzcores
#   ./run                         # run all tests
#   ./run internet                # run all internet based tests
#   ./run local                   # run all local tests (no internet)
#
# See README.md file for instructions.

script_dir=$(dirname "$0")
zz_root=$(cd "$script_dir"; cd ..; pwd)  # handles absolute and relative
tmp='./run.tmp'
tester='bash clitest'

cd "$script_dir"

# For Travis exceptions, see https://github.com/funcoeszz/funcoeszz/issues/355
internet=$("$zz_root"/util/metadata.sh tags | awk '/^zz.* internet/{sub(/zz\//,"");printf $1 "\n"}')
internet_travis=$(echo "$internet" | grep -E -v '^zz(distro|linux|chavepgp)\.sh$')
all=$(ls -1 zz*.sh)
no_internet=$(echo "$all" | grep -F -v -x -f <(echo "$internet"))

# Check requirements
$tester -V > /dev/null || {
	printf '%s\n' "Ops... Não achei o programa testador: clitest"
	printf '%s\n' 'Baixe-o deste endereço:'
	printf '%s\n' 'https://raw.github.com/aureliojargas/clitest/master/clitest'
	exit 1
}

# Create temporary file with ZZ init in full paths
cat > "$tmp" <<EOF
. "$zz_root"/funcoeszz \\
	--cor 0 \\
	--path "$zz_root"/funcoeszz \\
	--dir  "$zz_root"/zz
EOF

# Note: To remove the dots ........ change 'dot' to 'none'
if test "internet" = "$1"
then
	# Test internet based files
	$tester --progress dot --pre-flight ". $tmp" $internet
	exitcode=$?
elif test "internet_travis" = "$1"
then
	# Test internet based files
	$tester --progress dot --pre-flight ". $tmp" $internet_travis
	exitcode=$?
elif test "local" = "$1"
then
	# Tests that do not need internet
	$tester --progress dot --pre-flight ". $tmp" $no_internet
	exitcode=$?
elif test $# -gt 0
then
	# Test specific file(s)
	$tester --progress dot --pre-flight ". $tmp" "$@"
	exitcode=$?
else
	# Test all files
	$tester --progress dot --pre-flight ". $tmp" zz*.sh
	exitcode=$?
fi

rm "$tmp"
exit "$exitcode"
