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

. "$(sfpath)" || exit 3

shellfu import pretty
shellfu import saturnin
shellfu import jat

usage() {
    mkusage "$@" "[options] BODY" \
        -- \
            "Initialize and quick-run JAT test."                             \
            ""                                                               \
        --                                                                   \
            "Test body should consist of asserts and/or phases documented"   \
            "in jat.sh Shellfu module; use \`sfdoc jat\` to access its"      \
            "documentation."
}

qrun() {
    #
    # Wrap in boilerplate and run test code in $1
    #
    local tes
    bash -n "$Body" || {
        warn "test body has syntax errors"
        return 3
    }
    jat__rinit || {
        warn "failed to initialize logging"
        return 3
    }
    debug "(pre-source)"
    source "$Body"
    debug "(past-source)"
    jat__rfinish; tes=$?
    debug -v tes
    case $tes in
        0|1)    : ;;
        2)      warn "test indicated incomplete results"       ;;
        3)      warn "test indicated recoverable error"        ;;
        4)      warn "test indicated invalid results"          ;;
        *)      warn "test returned with illegal status: $tes" ;;
    esac
    return $tes
}

main() {
    local Body
    while true; do case $1 in
        -*) usage ;;
        *)  break ;;
    esac done
    Body=$1; shift
    test -n "$Body" || usage -w "no BODY?"
    test -f "$Body" || warn "test body not found: $Body"
    debug -v Body
    qrun
}

main "$@"
