#!/bin/bash
set -eu

help() {
    echo "Usage: $0 [OPTION] [--] [ARGS]"
    echo
    echo "Options:"
    echo "   -m, --mode MODE    - Use selected mode:"
    echo "                        sru-validation   - Ubuntu SRU validation"
    echo "                        native-package   - Use native package"
    echo "   -n, --no-rebuild   - Do not rebuild snapd snap"
    echo "   --                 - Delimiter for arguments parsed by this script"
    echo "   ARGS               - Arguments passed to spread"
    echo
}

need_rebuild=1
mode=""
no_rebuild=0

while true; do
    case "${1-}" in
        --help|-h)
            help
            exit 0
            ;;
        --mode|-m)
            shift
            maybe_mode="$1"
            shift
            if [ "$mode" != "" ]; then
                echo "already using mode $mode"
                exit 1
            fi

            case "$maybe_mode" in
                sru-validation)
                    mode="sru-validation"
                    ;;
                native-package)
                    mode="native-package"
                    ;;
                *)
                    echo "unsupported mode: $maybe_mode"
                    exit 1
                    ;;
            esac
            ;;
        --no-rebuild|-n)
            no_rebuild=1
            shift
            ;;
        --)
            shift
            break
            ;;
        *)
            break
            ;;
    esac
done

set -x

shopt -s nullglob

if [ "${NO_REBUILD:-0}" = "1" ]; then
    echo "-- $(date) -- requested no snap rebuild"
    need_rebuild=0

    # check if we have any snaps built at all
    built_snaps=(built-snap/snapd_*.snap.keep)
    if (( "${#built_snaps[@]}" > 0 )); then
        echo "-- $(date) -- found prebuilt snapd snaps:"
        for s in "${built_snaps[@]}"; do
            echo "--   $s"
        done
    else
        echo "-- $(date) -- no prebuilt snaps found"
        if [ "$no_rebuild" = "1" ]; then
           # no rebuild explicitly requested
           echo "either drop --no-rebuild or use ./tests/build-test-snapd-snap to build the snap"
           exit 1
        fi
        need_rebuild=1
    fi
fi

if [ "$need_rebuild" = 1 ]; then
    echo "-- $(date) -- rebuilding snapd snap"
    ./tests/build-test-snapd-snap
    echo "-- $(date) -- snapd snap rebuild complete"
fi

if [ -t 1 ]; then
    export SPREAD_DEBUG_EACH=0
fi

case "$mode" in
    sru-validation)
        echo "-- SRU validation mode"
        export SPREAD_SNAP_REEXEC=0
        export SPREAD_SRU_VALIDATION=1
        ;;
    native-package)
        echo "-- native package mode"
        export SPREAD_SNAP_REEXEC=0
        export SPREAD_SNAPD_DEB_FROM_REPO=false
        ;;
esac

export SPREAD_USE_PREBUILT_SNAPD_SNAP=true

# check that we are passing a test suite (in the form of
# <backend>:<system>:<test>) to spread, otherwise this would turn into a very
# annoyng spread invocation
maybe_has_ts=0
for arg in "$@"; do
    if [ "${arg/://}" != "$arg" ]; then
        maybe_has_ts=1
        break
    fi
done

if [ "$maybe_has_ts" = "0" ]; then
    echo "error: attempting to run spread without any arguments, this would launch all test suites"
    exit 1
fi

# Run spread
exec spread "$@"
