#!/bin/sh

(
ocroscript=../ocroscript

cd tests

test -x $ocroscript || {
    echo "I can't find ocroscript."
    exit 1
}

$ocroscript -e 'pcall(function () a=intarray(); a:at(1); end)' || {
    echo "TEST FAILED Your version of ocroscript cannot catch C++ exceptions."
    echo "You need to patch tolua++ or get a newer version of tolua++."
    echo "Do a touch *.pkg in ocropus/ocroscript after updating tolua++ and"
    echo "before running jam to make sure the binary gets updated properly."
    echo "This is a FATAL failure because many scripts rely on catching C++ exceptions."
    exit 1
}

$ocroscript -e 'pcall(function () fill(a,0) end)' || {
    echo "TEST FAILED your version of ocroscript segfaults on nil reference arguments."
    echo "You need to patch tolua++ or get a newer version of tolua++."
    echo "Do a touch *.pkg in ocropus/ocroscript after updating tolua++ and"
    echo "before running jam to make sure the binary gets updated properly."
}

if [ `ls images | grep png | wc -l` = "0" ]; then 
    echo "TEST FAILED Your test/images directory has no images."
    echo "Test images are distributed in a separate package,"
    echo "so look for it on http://www.ocropus.org"
fi

for i in test-*.lua; do 
	echo
	echo '***' $i '***'
	echo
        $ocroscript  -e '
            ok,result = pcall(function() dofile("'$i'") end)
            if not ok then print(result.." TEST FAILED UNEXPECTEDLY ('$i')") end
        ' /dev/null
done

./run-toplevels

echo
echo '*** done ***'
echo
echo "   +--------------------------------------+"
echo "   |  don't forget to run ./run-bigtests  |"
echo "   +--------------------------------------+"
echo

)

