#!/usr/bin/env bash

# A simple wrapper around stack
#
# This exists mainly to make it easier to pass arguments to test and benchmark
# suites.

if [[ $# -eq 0 ]]; then
    cat <<EOF
Usage: $0 COMMAND [OPTS]

Commands:
---------

b[uild] [OPTS]                  build project

t[est] u[nit] [OPTS]            run unit tests
t[est] e[xamples] [OPTS]        run exampes
t[est] d[octests] [OPTS]        run doctests
t[est] [OPTS]                   run all testsuites

bench [OPTS]                    run the benchmark suite

r[un] [OPTS]                    run the main program

haddock [OPTS]                  build documentation
EOF
    exit 1
fi

doTest() {
    if [[ $# -eq 0 ]]; then
        exec stack test
    fi

    case "$1" in
        u|unit|s|spec)
            exec stack test :spec --test-arguments "${*:2}"
            ;;
        e|examples)
            exec stack test :examples --test-arguments "${*:2}"
            ;;
        d|doctests)
            exec stack test :doctests --test-arguments "${*:2}"
            ;;
        *)
            exec stack test "$@"
    esac
}

doBench() {
    exec stack bench :bench --benchmark-arguments "$*"
}

case "$1" in
    b|build)
        exec stack build "${@:2}"
        ;;
    t|test)
        doTest "${@:2}"
        ;;
    bench)
        doBench "${@:2}"
        ;;
    r|run)
        exec stack exec ma -- "${@:2}"
        ;;
    haddock)
        exec stack haddock :ma "${@:2}"
        ;;
    *)
        echo "Unknown command $1"
        exit 1
esac