#!/bin/bash

LIST=run.list
ALIVE=Alive
HWSW=hwsw

#### TODO: print the flags for each case
# (1) recursion bound
# (2) inline depth
# (3) delays

echo "Running automated tests.."
for b in $(cat ${LIST} | cut -d ' ' -f 2)
do
    flags=${b/.bpl/.config}
    target=${b/.bpl/.alive.bpl}
    output=${b/.bpl/.output}
    outcome=$(grep ${b} ${LIST} | cut -d ' ' -f 1)

    echo
    echo "[${b/.bpl/}]:"

    if [[ ${outcome} != "Y" && ${outcome} != "N" ]]
    then
        echo "Bad ${LIST} file; format is Y/N <filename>.bpl"
        exit -1
    fi

    if [[ ! -f ${flags} ]]
    then
        echo "Found no configuration file for ${b}."
    fi

    echo "Translating ${b}.."
    ${ALIVE} ${b}

    echo "Analyzing ${target}.."
    ${HWSW} ${target} /flags:${flags} > ${output}

    runtime=$(grep "Total Time" ${output} | cut -d ':' -f 2 | tr -d ' ')

    if [[ -z $(grep "No bugs" ${output}) ]]
    
    ## "True bug"
    then
        if [[ ${outcome} == "Y" ]]
        then
            echo "OK; found non-termination in ${runtime}."  
        else
            echo "!!; unexpected non-termination"
        fi

    ## "No bugs"
    else
        if [[ ${outcome} == "Y" ]]
        then
            echo "!!; expected non-termination."
        else
            echo "OK; did not find non-termination in ${runtime}."
        fi
    fi
done