#!/usr/bin/env bash
set -e

# @describe Demonstrates `--argc-parallel` for running subcommands in parallel
#
# Compared with GNU parallel, the biggest advantage of argc-parallel is that it preserves `argc_*` variables.

# @cmd Run the test suite
# @flag --verbose Show verbose output
test() {
    echo "test: running test suite (verbose=${argc_verbose:-0})"
    sleep 1
    echo "test: 128 tests passed"
}

# @cmd Run the linter
# @flag --verbose Show verbose output
lint() {
    echo "lint: running linter (verbose=${argc_verbose:-0})"
    sleep 1
    echo "lint: no issues found"
}

# @cmd Format source files
# @flag --check Check formatting without changing files
# @flag --verbose Show verbose output
format() {
    # Parallel jobs call this function directly, so they pass --check as $1.
    if [[ "$argc_check" == 1 ]]; then
        echo "format: checking formatting (verbose=${argc_verbose:-0})"
        sleep 1
        echo "format: all files formatted"
    else
        echo "format: formatting files (verbose=${argc_verbose:-0})"
        sleep 1
        echo "format: done"
    fi
}

# @cmd Run all checks in parallel
# @flag --verbose Show verbose output
check() {
    argc --argc-parallel "$0" test ::: lint ::: format --check
}

# @cmd Run all checks sequentially
# @flag --verbose Show verbose output
check-sequential() {
    test
    lint
    format --check
}

eval "$(argc --argc-eval "$0" "$@")"
