#!/usr/bin/bash
#shellcheck disable=SC1090,SC2164

. "$(sfpath)" || exit 3

shellfu import pretty

shellfu import jats

usage() {
    mkusage "$@" \
        "[options] TESTID       # jats://.." \
        "[options] TESTDIR" \
        -- \
            "Initialize test run and run JAT test."                          \
            ""                                                               \
            "Copy test directory into temp and launch test there."           \
            ""                                                               \
            "You can provide path to test directory directly (TESTDIR), or"  \
            "provide TESTID.  In the latter case, jattool will look for the" \
            "test under suite directory, which is specified by JATS__PATH,"  \
            "or /usr/share/jats/suite."                                      \
            ""                                                               \
        -o                                                                   \
            "-k         Keep run directory."                                 \
            "-p PARAM=VALUE   Set test parameter named PARAM to value VALUE."\
            "           Can be specified multiple times.  Note that VALUE"   \
            "           must be a simple word (see JATS specification)."     \
            "-t TIMEOUT   Kill test after TIMEOUT seconds.  Default: 0,"     \
            "           which disables the timeout.       "                  \
        --                                                                   \
            "Test body should consist of asserts and/or phases documented"   \
            "in jat.sh Shellfu module; use \`sfdoc jat\` to access its"      \
            "documentation."
}

runtest() {
    #
    # Wrap in boilerplate and run test code in ./test
    #
    JAT__TEST_ID=$TestId \
    JAT__TEST_VERSION=$TestVersion \
    JAT__TEST_PARAMS=$TestParams \
        jattool _qrun "./test"
}

main() {
    local TestSpec
    local TestHome
    local TestTmp
    local TestId
    local TestVersion
    local TestParams
    local Timeout
    local keeptmp=false
    local es=0
    while true; do case $1 in
        -k) keeptmp=true; shift ;;
        -p) TestParams+=",$2"; shift 2 || usage -w "missing K=V" ;;
        -t) Timeout=$2; shift 2 || usage -w "missing TIMEOUT" ;;
        -*) usage ;;
        *)  break ;;
    esac done
    test -n "$Timeout" && die "sorry TIMEOUT is not supported yet"
    TestSpec=$1
    case $TestSpec in
        "")
            usage -w "no TESTDIR or TESTID?"
            ;;
        jats://*)
            TestId=$(jats__tidparse id "$TestSpec")
            TestParams+=",$(jats__tidparse params "$TestSpec")"
            TestHome=$(jats__tlocate "$TestId") || die "could not locate test: $TestId"
            ;;
        *)
            TestHome=$TestSpec
            TestId=$(jats__tprop id "$TestHome")
            ;;
    esac
    TestParams=$(
        sed '
            s/,,*/,/g
            s/,$//
            s/^,//
        ' <<< "$TestParams"
    )
    test -f "$TestHome/test" \
     || die "test body not found: $TestHome/test"
    TestVersion=$(jats__tprop version "$TestHome")
    TestTmp=$(mktemp -dt jattool-runtest.XXXXXXXX)
    debug -v TestHome TestId TestTmp TestParams
    cp -ar "$TestHome"/* "$TestTmp" \
     || die "could not copy test to tempdir"
    pushd "$TestTmp" >/dev/null
        runtest; es=$?
    popd >/dev/null
    $keeptmp || rm -rf "$TestTmp"
    $keeptmp && warn "leaving temporary directory behind: $TestTmp"
    return $es
}

main "$@"
