#! /bin/bash

set -euo pipefail

check () {
    expname="$1"
    shift 1
    options=("$@")
    "./${expname}.py" "${options[@]}"
    properties="data/$expname-eval/properties"
    if [[ ! -f "$properties" ]]; then
        echo "File not found: $properties"
        exit 1
    fi
    set +e
    grep --after-context=10 "\"unexplained_errors\": \[$" "$properties"
    exitcode="$?"
    set -e
    if [[ "$exitcode" == 0 ]]; then
        echo "Unexplained error detected in $properties"
        exit 1
    fi
}

SCRIPT="$1"
shift 1  # Forget first argument.
OPTIONS=("$@")  # All remaining arguments are script options.
DIR=$(dirname "$SCRIPT")
FILENAME=$(basename "$SCRIPT")
FILENAME="${FILENAME%.*}"

cd $(dirname "$0")/../examples

rm -rf "$DIR/data/$FILENAME"
rm -rf "$DIR/data/$FILENAME-eval"
pushd "$DIR"
check "$FILENAME" "${OPTIONS[@]}"
