#!/bin/bash
Fail()
{
    echo "ERROR($0): $1"
    exit 1
}

[[ "$1" == "" || "$1" == "-v" ]] || Fail "Invalid command line options."

# Make sure we don't get confused by stale calculations.
rm -f temp/k_check.txt

cd ../source/kotlin || Fail "Cannot change dir to ../source/kotlin"
rm -rf build
if ! ./gradlew assemble build test dokkaGfm jar; then
    # Dump the contents of the unit tests to the console,
    # so if the test fails on GitHub Actions, I can see what went wrong.
    cat build/test-results/test/*.xml
    Fail "Error building/testing Kotlin code."
fi

cd ../../generate || Fail "Cannot change dir to ../../generate"
./generate $1 check temp/k_check.txt || Fail "Verification failure for Kotlin unit test output."

# The above gradlew call also generated all the Markdown documentation.
# The other languages do this from makedoc, but Kotlin is an exception.
# Finish putting together custom documentation for GitHub.
cd kotlindoc || Fail "Cannot change dir to kotlindoc"
./format_kotlin_doc.py || Fail "Error running ./format_kotlin_doc.py"
cd .. || Fail "Cannot change dir to .."

for file in temp/k_longitude_*.txt; do
    ./generate $1 check ${file} || Fail "Failed verification of file ${file}"
done

echo "unit_test_kotlin: success"
exit 0
