#!/bin/sh

dir=`dirname $0`

updateoracle=false
files=""
success=true

while test $# != 0; do
case "$1" in
  "-update-oracle")
      updateoracle=true;;
  "-"*)
      printf "unknown option: %s\n" "$1"
      printf "usage: ce-bench [-update-oracle] <files>\n"
      printf "  <files> must be given without the '.mlw' suffix\n"
      printf "  if <files> empty, use all files from directory 'ce'\n"
      exit 2;;
  *)
       files="$files $1"
esac
shift
done

if test "$files" = "" ; then
    files="$dir/ce/*.mlw"
fi


# $1 = prover, $2 = file
run () {
    printf "  $2 ($1)... "
    f="$2_$1"
    printf "WP  $2 ($1)... "
    echo "Weakest Precondition" > "$f.out"
    $dir/../bin/why3prove.opt --debug print_attrs -P "$1,counterexamples" --timelimit 1 -a split_vc "$2.mlw" | \
        # This ad hoc sed removes any timing information from counterexamples output.
        # Counterexamples in JSON format cannot match this regexp.
        sed 's/ ([0-9]\+\.[0-9]\+s)//' | \
        # This ad hoc sed removes diff between Timeout and Unknown (unknown)
        # when running from platform with different speed.
        sed -r 's/(Timeout|Unknown \(unknown\))/Timeout or Unknown/' >> "$f.out"
    printf "SP  $2 ($1)... "
    echo "Strongest Postcondition" >> "$f.out"
    $dir/../bin/why3prove.opt --debug print_attrs --debug vc_sp -P "$1,counterexamples" --timelimit 1 -a split_vc "$2.mlw" | \
        # This ad hoc sed removes any timing information from counterexamples output.
        # Counterexamples in JSON format cannot match this regexp.
        sed 's/ ([0-9]\+\.[0-9]\+s)//' | \
        # This ad hoc sed removes diff between Timeout and Unknown (unknown)
        # when running from platform with different speed.
        sed -r 's/(Timeout|Unknown \(unknown\))/Timeout or Unknown/' >> "$f.out"
    if cmp "$f.oracle" "$f.out" > /dev/null 2>&1 ; then
        echo "ok"
    else
        if $updateoracle; then
            echo "Updating oracle for $2, prover $1"
            mv "$f.out" "$f.oracle"
        else
            echo "FAILED!"
            echo "diff is the following:"
            diff "$f.oracle" "$f.out"
            success=false
        fi
    fi
}

for file in $files; do
    filedir=`dirname $file`
    filebase=`basename $file .mlw`
    printf "Running provers on $filedir/$filebase.mlw\n";
    run CVC4,1.5 $filedir/$filebase
    run Z3,4.6.0 $filedir/$filebase
done
if $success; then
    exit 0
else
    exit 1
fi
