#!/bin/bash

################################################################################
# run_regressions.sh
#
# Overview
# ========
#
# This script iterates through all conf files in `benchmarks/config-jsons`, runs
# Gambit on them, and compares the output directory to the regression results
# stored in `resources/regressions` named after the configuration json
# (generated by make_regressions.sh).
#
# Usage
# -----
#
# This script takes no arguments. It determines paths to all input files (i.e.,
# files in `benchmarks/config-jsons`) and regression locations (i.e.,
# `resources/regressions/XXXXX`) relative to this script's parent directory.

SCRIPTS=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
GAMBIT="$SCRIPTS/.."
CONFIGS="$GAMBIT/benchmarks/config-jsons"
REGRESSIONS="$GAMBIT"/resources/regressions
echo "scripts: $SCRIPTS"
echo "gambit: $GAMBIT"
echo "configs: $CONFIGS"
echo "regressions: $REGRESSIONS"

[ -e "$REGRESSIONS" ] || {
    echo "No regressions exist!"
}

passed=()
failed=()
echo "Running tests..."
cd "$GAMBIT" || {
    echo "Error: couldn't cd $GAMBIT"
    exit 1
}
for conf_path in "$CONFIGS"/*; do
    echo
    echo
    printf "\033[1m- Conf path: %s\033[0m\n" "$conf_path"

    conf=$(basename "$conf_path")
    regression_dir="$REGRESSIONS"/"$conf"

    printf "  \033[1mRunning:\033[0m %s\n" "gambit mutate --json $conf_path"
    stdout="$(cargo run -- mutate --json "$conf_path")"
    printf "  \033[1mGambit Output:\033[0m '\033[3m%s\033[0m'\n" "$stdout"
    printf "  \033[1mDiffing\033[0m gambit_out and %s\n" "$regression_dir"
    bash "$SCRIPTS"/remove_sourceroots.sh gambit_out/gambit_results.json
    if diff -q -r gambit_out "$regression_dir"; then
        printf "  \033[92mSUCCESS\033[0m\n"
        passed+=("$conf")
    else
        printf "  \033[91mFAILED:\033[0m %s\n" "$conf"
        failed+=("$conf")
    fi
    rm -rf gambit_out

done

printf "\n\n\033[96mREGRESSION SUMMARY\033[0m\n"
printf "\033[96m==================\033[0m\n\n"

printf "\033[92mPassed:\033[0m %s of %s tests\n" ${#passed[@]} $((${#failed[@]} + ${#passed[@]}))
printf "\033[92m-------\033[0m\n"

for conf in "${passed[@]}"; do
    printf "\033[92m[+]\033[0m %s\n" "$conf"
done

printf "\n"

printf "\033[91mFailed:\033[0m %s of %s tests\n" ${#failed[@]} $((${#failed[@]} + ${#passed[@]}))
printf "\033[91m-------\033[0m\n"

for conf in "${failed[@]}"; do
    printf "\033[91m[-]\033[0m %s\n" "$conf"
done
