#!/bin/sh

. util

map() { ann "divine verify --map --report" "$@" "2> progress | capture"; }
ndfs() { ann "divine verify --nested-dfs --report" "$@" "2> progress | capture"; }
owcty() { ann "divine verify --owcty --report" "$@" "2> progress | capture"; }
reachability() { ann "divine verify --reachability --report" "$@" "2> progress | capture"; }
metrics() { ann "divine metrics --report" "$@" "2> progress | capture"; }
verify() { ann "divine verify --report" "$@" "2> progress | capture"; }
draw() { ann "divine draw" "$@"; }
info() { ann "divine info" $(echo "$@" | sed -e "s,--property=[^ ]*\|--no-reduce\|-D [^ ]*,,g"); }

mpi_verify() { mpi verify --mpi "$@"; }
mpi_metrics() { mpi metrics --mpi "$@"; }

mpi() {
    test "$O_MPI" = "ON" || exit 200;
    "$MPIEXEC" > /dev/null 2>&1 || exit 200; # probably not found
    ann "$MPIEXEC -H localhost,localhost divine" "$@" "--report 2> progress | capture"
}

fixreduce() {
    if echo "$@" | grep -q "reduce="; then
        "$@"
    else
        "$@" --no-reduce
    fi
}

identity() {
    file=$1
    shift
    "$@" "$file"
}

dve_compile() {
    file=$1
    shift
    args=
    defs=

    while test -n "$1"; do
        if test "$1" = "-D"; then
            defs="$defs -D $2"
            shift
        else
            args="$args $1";
        fi
        shift
    done

    ann divine compile $defs "$file"
    $args `basename $file$cesmiext`
}

llvm_assemble() {
    file=$1
    shift

    out=`basename $file .ll`.bc
    ann $(echo $LLVMCONFIG | sed -e s,llvm-config,llvm-as,) -o=$out $file
    "$@" $out
}

set -e
fixreduce "$@"
