#!/bin/bash

# auto bench for why

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

shopt -s nullglob

suffix=$1

# TODO: remove the hack about int.mlw once it has become builtin
goods () {
    pgm="bin/why3prove$suffix"
    ERROR=
    test -d $1 || exit 1
    rm -f bench_errors
    for f in $1/*.[wm][hl][yw] ; do
	printf "  $f... "
        opts="$2"
        if test $f = stdlib/int.mlw; then opts="$2 --type-only"; fi
	# running Why
	if ! $pgm $opts $f > /dev/null 2> bench_error; then
	    echo "FAILED!"
#            echo "env: WHY3DATA='$WHY3DATA'"
            echo "invocation: $pgm $opts $f" | tee -a bench_errors
            cat bench_error | tee -a bench_errors
            ERROR=yes
	elif test -s bench_error; then
            echo "warning!"
            cat bench_error
        else
	    echo "ok"
        fi
    done
    rm -f bench_error
    if test -n "$ERROR"; then
        echo "bench aborted due to the following errors:"
        cat bench_errors
        rm bench_errors
        exit 1
    fi
}

bads () {
    pgm="bin/why3prove$suffix"
    test -d $1 || exit 1
    for f in $1/*.[wm][hl][yw] ; do
	printf "  $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 () {
    pgm="bin/why3prove$suffix"
    test -d $1 || exit 1
    for f in $1/*.drv; do
        if [[ $f == drivers/ocaml*.drv ]]; then continue; fi
        if [[ $f == drivers/c.drv ]]; then continue; fi
        if [[ $f == drivers/cakeml.drv ]]; then continue; fi
	printf "  $f... "
	# running Why
	if ! echo "theory Test goal G : 1=2 end" | $pgm -F whyml --driver $f - > /dev/null; then
	    echo "why FAILED"
            echo "theory Test goal G : 1=2 end" | $pgm -F whyml --driver $f -
	    exit 1
	fi
	echo "ok"
    done
}

valid_goals () {
    pgm="bin/why3prove$suffix"
    test -d $1 || exit 1
    for f in $1/*.mlw; do
	printf "  $f... "
        if ! $pgm -t 15 -P alt-ergo $f > /dev/null 2>&1; then
            echo "FAILED!"
            echo "$pgm -t 15 -P alt-ergo $f"
            $pgm -t 15 -P alt-ergo $f
	    exit 1
       fi
       echo "ok"
    done
}

invalid_goals () {
    pgm="bin/why3prove$suffix"
    test -d $1 || exit 1
    for f in $1/*.mlw; do
	printf "  $f... "
	if $pgm -t 3 -P alt-ergo $f | grep -q Valid; then
	    echo "invalid test $f failed!"
	    echo "$pgm -P alt-ergo $f"
	    $pgm -t 3 -P alt-ergo $f
	    exit 1
        else
	    echo "ok"
       fi
    done
}

replay () {
    pgm="bin/why3replay$suffix"
    test -d $1 || exit 1
    for f in $1/*/; do
        printf "  $f... "
        if $pgm $2 $f > /dev/null 2> /dev/null; then
            echo "ok"
        else
            echo "replay test failed!"
            echo $pgm $2 $f
            $pgm $2 $f
            exit 1
        fi
    done
}

execute () {
    pgm="bin/why3execute$suffix"
    printf "  $1 $2... "
    if $pgm $1 $2 > /dev/null; then
        echo "ok"
    else
        echo "execution test failed!"
        echo $pgm $1 $2
        $pgm $1 $2
        exit 1
    fi
}

extract_and_run () {
    dir=$1
    shift
    prg=$1
    shift
    printf "  $dir... "
    rm -f $dir/$prg
    printf "clean... "
    if BENCH=yes make -C $dir clean > /dev/null; then
        printf "extract... "
        if BENCH=yes make -C $dir extract > /dev/null; then
            printf "compile... "
            if BENCH=yes make -C $dir > /dev/null; then
                printf "execute... "
                if $dir/main.opt "$@" > /dev/null; then
                    printf "doc... "
                    if BENCH=yes make -C $dir doc > /dev/null; then
                        echo "ok"
                    else
                        echo "documentation failed!"
                        echo BENCH=yes make -C $dir doc
                        BENCH=yes make -C $dir doc
                        exit 1
                    fi
                else
                    echo "execution failed!"
                    echo $dir/main.opt "$@"
                    $dir/main.opt "$@"
                    exit 1
                fi
            else
                echo "compilation failed!"
                echo BENCH=yes make -C $dir
                BENCH=yes make -C $dir
                exit 1
            fi
        else
            echo "extract failed!"
            echo BENCH=yes make -C $dir extract
            BENCH=yes make -C $dir extract
            exit 1
        fi
    else
        echo "clean failed!"
        echo BENCH=yes make -C $dir clean
        BENCH=yes make -C $dir clean
        exit 1
    fi
}

extract_and_test_wmp () {
    dir=$1
    shift
    printf "  $dir... "
    printf "clean... "
    if BENCH=yes make -C $dir clean > /dev/null; then
        printf "extract... "
        if BENCH=yes make -C $dir extract > /dev/null; then
            printf "compile... "
            if BENCH=yes make -C $dir bench-tests > /dev/null; then
                printf "execute... "
                if $dir/build/bench-tests > /dev/null; then
                    echo "ok"
                else
                    echo "execution failed!"
                    echo $dir/main.opt
                    $dir/main.opt
                    exit 1
                fi
            else
                echo "compilation failed!"
                echo BENCH=yes make -C $dir
                BENCH=yes make -C $dir
                exit 1
            fi
        else
            echo "extract failed!"
            echo BENCH=yes make -C $dir extract
            BENCH=yes make -C $dir extract
            exit 1
        fi
    else
        echo "clean failed!"
        echo BENCH=yes make -C $dir clean
        BENCH=yes make -C $dir clean
        exit 1
    fi
}

list_stuff () {
    pgm="bin/why3$suffix"
    printf '  %s ' "$1"
    if $pgm $1 > /dev/null; then
        echo "ok"
    else
        echo "$pgm $1 FAIL"
        $pgm $1
        exit 1
    fi
}

echo "=== Checking stdlib ==="
goods stdlib
goods stdlib/mach
echo ""

echo "=== Checking drivers ==="
drivers drivers
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
goods bench/typing/x-bad --type-only
echo ""

echo "=== Checking good files ==="
goods bench/typing/good
goods bench/typing/x-good --parse-only
bads bench/typing/x-good --type-only
goods bench/programs/good
goods bench/ce
goods src/trywhy3/examples "--debug ignore_missing_diverges"
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/WP_revisited
goods examples/vacid_0_binary_heaps "-L examples/vacid_0_binary_heaps"
goods examples/bitvectors "-L examples/bitvectors"
goods examples/avl "-L examples/avl"
goods examples/verifythis_2016_matrix_multiplication "-L examples/verifythis_2016_matrix_multiplication"
goods examples/double_wp "-L examples/double_wp"
goods examples/ring_decision "-L examples/ring_decision"
goods examples/multiprecision "-L examples/multiprecision"
goods examples/prover "-L examples/prover --debug ignore_unused_vars"
goods examples/in_progress
echo ""

echo "=== Checking replay (no prover) ==="
replay bench/replay
replay examples/stdlib --merging-only
replay examples/bts --merging-only
replay examples/tests --merging-only
replay examples/tests-provers --merging-only
replay examples/check-builtin --merging-only
replay examples/logic --merging-only
replay examples --merging-only
replay examples/foveoos11-cm --merging-only
replay examples/WP_revisited --merging-only
replay examples/vacid_0_binary_heaps "-L examples/vacid_0_binary_heaps --merging-only"
replay examples/bitvectors "-L examples/bitvectors --merging-only"
replay examples/avl "-L examples/avl --merging-only"
replay examples/verifythis_2016_matrix_multiplication "-L examples/verifythis_2016_matrix_multiplication --merging-only"
replay examples/double_wp "-L examples/double_wp --merging-only"
replay examples/ring_decision "-L examples/ring_decision --merging-only"
replay examples/multiprecision "-L examples/multiprecision --merging-only"
replay examples/prover "-L examples/prover --merging-only --debug ignore_unused_vars"
#replay examples/in_progress --merging-only
#replay examples/in_progress/multiprecision "-L examples/in_progress/multiprecision --merging-only"
echo ""

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

echo "=== Checking invalid goals ==="
invalid_goals bench/invalid
echo ""

echo "=== Checking execution ==="
execute examples/euler001.mlw Euler001.bench
execute examples/euler002.mlw Solve.bench
execute examples/fibonacci.mlw FibRecGhost.bench
execute examples/fibonacci.mlw FibonacciLogarithmic.bench
# fails: cannot execute Cany
# execute examples/same_fringe.mlw SameFringe.bench
# fails: cannot evaluate condition a=b (how to do it?)
# execute examples/same_fringe.mlw SameFringe.test5
execute examples/same_fringe.mlw Test.test1
execute examples/vstte12_combinators.mlw Combinators.test_SKK
execute examples/selection_sort.mlw SelectionSort.bench
execute examples/insertion_sort.mlw InsertionSort.bench
execute examples/quicksort.mlw Test.bench
execute examples/conjugate.mlw Test.bench
# fails: needs support for "val" without code (how to do it?)
# examples/vacid_0_sparse_array.mlw Harness.bench
execute examples/knuth_prime_numbers.mlw PrimeNumbers.bench
execute examples/vstte10_max_sum.mlw TestCase.test_case
execute examples/verifythis_fm2012_LRS.mlw LCP_test.bench
execute examples/verifythis_fm2012_LRS.mlw SuffixSort_test.bench
execute examples/verifythis_fm2012_LRS.mlw SuffixArray_test.bench
execute examples/verifythis_fm2012_LRS.mlw LRS_test.bench
execute examples/verifythis_PrefixSumRec.mlw PrefixSumRec.bench
execute examples/vstte10_queens.mlw NQueens.test8
# fails: "Cannot decide condition of if: (not ((~)((<<)((~)(0), 8)) = 0))"
# examples/queens.mlw NQueensBits.test8
# fails: cannot find definition of routine eq
# examples/residual.mlw Test.test_astar
echo ""


echo "=== Checking extraction to OCaml ==="
extract_and_run examples/euler001 euler001.ml 1000000
extract_and_run examples/gcd gcd.ml 6 15
extract_and_run examples/vstte10_max_sum vstte10_max_sum.ml
# needs to port
# extract_and_run examples/vstte12_combinators vstte12_combinators.ml "((((K K) K) K) K)"
extract_and_run examples/defunctionalization defunctionalization.ml
extract_and_run examples/sudoku sudoku.ml 2,0,9,0,0,0,0,1,0,0,0,0,0,6,0,0,0,0,0,5,3,8,0,2,7,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,7,5,0,0,3,0,4,1,2,0,8,9,0,0,0,0,4,0,9,0,0,2,0,8,0,0,0,0,1,0,0,5,0,0,0,0,0,0,0,7,6
echo ""

echo "=== Checking extraction to C ==="
extract_and_test_wmp examples/in_progress/multiprecision
extract_and_test_wmp examples/multiprecision
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 ""

echo "=== Checking realizations ==="
exec bench/check_realizations.sh
