#!/bin/sh

# auto bench for why

# Useless in this script ?
# export WHY3LIB=lib
# export WHY3DATA=.
# export WHY3LOADPATH=theories

pgm=$1

goods () {
    for f in $1/*.[wm][hl][yw] ; do
	echo -n "  $f... "
	# running Why
	if ! $pgm $2 $f > /dev/null 2>&1; then
	    echo "FAILED!"
#            echo "env: WHY3DATA='$WHY3DATA'"
            echo "invocation: $pgm $2 $f"
            echo "result:"
	    $pgm $2 $f
	    exit 1
	fi
	echo "ok"
    done
}

bads () {
    for f in $1/*.[wm][hl][yw] ; do
	echo -n "  $f... "
	if $pgm $2 $f > /dev/null 2>&1; then
	    echo "SHOULD FAIL!"
#            echo "env: WHY3DATA='$WHY3DATA'"
            echo "invocation: $pgm $2 $f"
	    exit 1
	fi
	echo "ok"
    done
}

drivers () {
    for f in $1/*.drv; do
        if [ $f = "drivers/ocaml.drv" ]; then continue; fi
	echo -n "  $f... "
	# running Why
	if ! echo "theory Test goal G : 1=2 end" | $pgm -F why --driver $f - > /dev/null 2>&1; then
	    echo "why FAILED"
            echo "theory Test goal G : 1=2 end" | $pgm -F why --driver $f -
	    exit 1
	fi
	echo "ok"
    done
}

valid_goals () {
    for f in $1/*.mlw; do
	echo -n "  "$f"... "
	if $pgm -t 10 -P alt-ergo $f | grep -q -v Valid; then
	    echo "valid test $f failed!"
	    echo "$pgm -P alt-ergo $f"
	    $pgm -t 10 -P alt-ergo $f
	    exit 1
       else
	    echo "ok"
       fi
    done
}

list_stuff () {
    echo -n "$1 "
    if $pgm $1 > /dev/null 2>&1; then
        echo "ok"
    else
        echo "$pgm $1 FAIL"
        $pgm $1
        exit 1
    fi
}


echo "=== Checking theories ==="
goods theories
echo ""

echo "=== Checking modules ==="
goods modules
echo ""

echo "=== Checking bad files ==="
goods bench/typing/bad --parse-only
goods bench/programs/bad-typing --parse-only
bads bench/typing/bad --type-only
bads bench/programs/bad-typing --type-only
echo ""

echo "=== Checking good files ==="
goods bench/typing/good
goods bench/programs/good
goods examples/bts
goods examples/tests
goods examples/tests-provers
goods examples/check-builtin
goods examples/logic
goods examples
goods examples/foveoos11-cm
goods examples/hoare_logic
goods examples/vacid_0_binary_heaps "-I examples/vacid_0_binary_heaps"
goods examples/bitvectors "-I examples/bitvectors"
goods examples/in_progress
echo ""

echo "=== Checking drivers ==="
drivers drivers
echo ""

echo "=== Checking valid goals ==="
valid_goals bench/valid
echo ""

echo "=== Checking --list-* ==="
list_stuff --list-transforms
list_stuff --list-printers
list_stuff --list-provers
list_stuff --list-formats
list_stuff --list-metas
list_stuff --list-debug-flags
echo ""
