#!/bin/sh
#

YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'

msg() {
    local m="$@"
    if echo "$m" | grep -qP '##'; then
        m="$YELLOW$m$NC"
    else
        m="$CYAN$m$NC"
    fi

    [ "$QUIET" != "-q" ] && echo -e "$m"
}

force_msg() {
    local m="$@"
    if echo "$m" | grep -qP '##'; then
        m="$YELLOW$m$NC"
    else
        m="$CYAN$m$NC"
    fi

    echo -e "$m"
}

is_verbose_mode() {
   test "$QUIET" != "-q"
}

msgn() {
    msg "\n## $@"
}

QUIET=${QUIET:-"-q"}
POLDEK_TESTING=1 # prevent diff.toc vacuuming
export POLDEK_TESTING

# vtest.sh denies them to fetch
POLDEK_TESTING_DENIED_FILES=
export POLDEK_TESTING_DENIED_FILES

unset POLDEK_TESTING_INDEX_COMPR
unset POLDEK_TESTING_PRELOAD_CONF
unset POLDEK_TESTING_WITH_SUGGESTS


# rpm's --root (and thus poldek's rpm and install-dist code) calls chroot(), which needs
# CAP_SYS_CHROOT. Rather than requiring root/sudo, re-run inside a user namespace where
# the caller is mapped to uid 0.
#
#   TEST_USERNS=auto      (default) use the namespace when possible
#   TEST_USERNS=0|off     opt out; the rpm tests then use sudo, or get skipped
#   TEST_USERNS=required  fail instead of silently skipping the rpm tests
user_ns_failed() {
    [ "${TEST_USERNS:-auto}" = "required" ] || return 0
    echo "## $1" >&2
    exit 1
}

user_ns_up() {
    [ -n "$POLDEK_TEST_USERNS" ] && return 0   # already re-exec'd, or opted out
    [ "$(id -u)" = "0" ] && return 0          # real root, nothing to do

    case "${TEST_USERNS:-auto}" in
        0|off|no)
            POLDEK_TEST_USERNS=opt-out
            export POLDEK_TEST_USERNS
            return 0 ;;
        required)
            ;;
    esac

    if ! command -v unshare >/dev/null 2>&1; then
        user_ns_failed "TEST_USERNS=required, but unshare is not installed"
        return 0
    fi
    if ! unshare -Urm true >/dev/null 2>&1; then
        user_ns_failed "TEST_USERNS=required, but unshare -Urm failed (userns disabled?)"
        return 0
    fi

    POLDEK_TEST_USERNS=1
    export POLDEK_TEST_USERNS
    force_msg "## no root/sudo: re-running under a user namespace (unshare -Urm)"
    # called as `user_ns_up "$0" "$@"`, so $@ here is the full argv of the test script
    # -- a function has its own positional parameters, the caller's are not visible
    exec unshare -Urm sh "$@"
}
user_ns_up "$0" "$@"


MAXLOOP=60
VERBOSE=""
RUNTEST_ONLY=

while test $# -gt 0 ; do
    case "${1}" in
        -l)
           grep -E ^test $0 | sed 's|()||g;s|{||g'; exit; shift ;;
        --source-repo)
            shift; SOURCE_REPO="${1}"; shift ;;
        -v)
            shift; VERBOSE="${VERBOSE}v";;
        -vv)
            shift; VERBOSE="${VERBOSE}vv";;

        -n) shift; MAXLOOP="${1}"; shift ;;

        -c) shift; COMPR="${1}"; shift ;;

        -t)
            shift; RUNTEST_ONLY="${1}"; shift;;
        -h)
            echo "Usage: $(basename $0) [--source-repo REPO] [-v] [-n NUM]"
            echo '    --source-repo  - source (read-only) repository for *-repo tests'
            echo "    -v             - be verbose"
            echo '    -n NUM         - do NUM iteration in *-loop tests'
            echo '    -c COMPR       - set compression method in repo tests'
            echo '    -t NAME        - run NAME test only'
            exit 0; shift ;;

        test[A-Za-z]*)
            RUNTEST_ONLY="${1}"; shift;;
        *)
            echo "unknown option ${1}; try -h"; exit 1;
    esac
done

if [ -n "$VERBOSE" ]; then
    QUIET="-$VERBOSE"
fi
