#!/bin/bash

set -eu

show_help() {
    echo "usage: tests.info <CMD>"
    echo
    echo "Supported commands:"
    echo "    is-snapd-from-archive:  indicates when the snapd package is from the repository"
    echo "    is-reexec-enabled:      indicates re-execution has been explicitly enabled through the env"
    echo "    is-reexec-in-use:       indicates re-execution to the snapd snap is being effectively performed"
}

# ubuntu 14.04: snapd deb is always from the repositories
# ubuntu-core: it is built based on the snapd.deb file, so it requires the deb is created from current.
is_snapd_from_archive() {
    {
        [ "$SNAPD_DEB_FROM_REPO" = true ] && \
        [ -n "$(command -v apt)" ] && \
        [[ "$SPREAD_SYSTEM" != ubuntu-core-* ]]
    } || \
    os.query is-trusty
}

is_reexec_enabled() {
    snap debug execution snap | grep -q "is-reexec-enabled: true"
}

is_reexec_in_use() {
    snap debug execution snap | grep -q "is-reexecd: true"
}

main() {
    if [ $# -eq 0 ]; then
        show_help
        exit 0
    fi

    local subcommand="$1"
    local action=
    while [ $# -gt 0 ]; do
        case "$subcommand" in
            -h|--help)
                show_help
                exit 0
                ;;
            *)
                action=$(echo "$subcommand" | tr '-' '_')
                shift
                break
                ;;
        esac
    done

    if [ -z "$(declare -f "$action")" ]; then
        echo "tests.info: no such command: $subcommand"
        show_help
        exit 1
    fi

    "$action" "$@"
}

main "$@"
