#!/usr/bin/env bash

# This script is expected to be run from the root of the project.

WATCH_ALONG=0

# Paths to the two implementations
GPL="src/gpl"
SPL="swipl"

GPL_FLAGS=("--error")
SPL_FLAGS=("--quiet" "-t" "'true'")

# Directory containing test files
TEST_DIR="examples"

# Temporary files for storing outputs
GPL_OUT=$(mktemp)
GPL_ERR=$(mktemp)
SPL_OUT=$(mktemp)
SPL_ERR=$(mktemp)

touch "$GPL_OUT" "$GPL_ERR" "$SPL_OUT" "$SPL_ERR"

# Flag to track if all tests pass
PASSED=0
FAILED=0

# Iterate over all test files in the test directory
#for TESTFILE in $(find ${TEST_DIR} -type f); do
files=(
"examples/program.pl"
"examples/basics/disjunction.pl"
"examples/basics/fraternity.pl"
"examples/basics/unification.pl"
"examples/basics/write.pl"
)
for TESTFILE in "${files[@]}"; do
    # Run both programs with the test file
    "${SPL}" "${SPL_FLAGS[@]}" "$TESTFILE" > "${SPL_OUT}" 2> "${SPL_ERR}"
    "${GPL}" "${GPL_FLAGS[@]}" -s "$TESTFILE" > "${GPL_OUT}" 2> "${GPL_ERR}"

    # Compare the outputs
    was_different="$(
        diff -q "$SPL_OUT" "$GPL_OUT" > /dev/null
        echo $?
    )"
    if [[ "${was_different}" -ne 0 || "${WATCH_ALONG}" -eq 1 ]]; then
        if [ "${was_different}" -ne 0 ]; then
            message="TEST FAILED"
        fi
        printf "%s for %s\n" "${message:="Result"}" "${TESTFILE}"
        printf "\nTest:\n%s\n" "$(cat "${TESTFILE}")"
        printf "\nExpected:\n%s\n" "$(cat "${SPL_OUT}" && cat "${SPL_ERR}")"
        printf "\nGot:\n%s\n" "$(cat "${GPL_OUT}" && cat "${GPL_ERR}")"
        echo "-----------------------------------------"
    fi

    if [ "${was_different}" -ne 0 ]; then
        FAILED=$((FAILED + 1))
    else
        PASSED=$((PASSED + 1))
    fi
done

# Clean up temporary files
rm "$SPL_OUT" "$GPL_OUT" "$SPL_ERR" "$GPL_ERR"

# Final result, summary
if [ $FAILED -eq 0 ]; then
    echo "All tests passed!"
else
    printf "Tests passed: %d\nTests failed: %d\n" "$PASSED" "$FAILED"
    exit 1
fi
