#!/bin/bash

. "$TF_DIR/include/subtest.sh"
. "$TF_DIR/include/tools.sh"

PRETTY=plain

. "$(sfpath)" || tf_exit_error "failed to init shellfu"
shellfu import saturnin  || tf_exit_error "failed to import saturnin"

tf_enum_subtests() {
    echo unknown
    echo unknown_dash1
    echo unknown_dash2
    echo unknown_none
    echo unknown_same
    echo saturnin_conf_path
    echo app_version
    echo cache_home
    echo libexec
    echo libexec_prefix
# warn "unknown devel key: $key"
}

rndtoken() {
    head -c 100 /dev/urandom | md5sum | head -c 7
}

ckfuzzy() {
    local es
    local out="result/$name.stdout"
    mkdir -p result
    cat > "$out"
    case "$name" in
        saturnin_conf_path)  grep -qx "$SATURNIN_CONF_PATH" ;;
        app_version)    grep -q '[0-9]'                   ;;
        cache_home)     grep -qx "$SATURNIN_CACHE_HOME"   ;;
        libexec)        grep -qx "$SATURNIN_LIBEXEC"      ;;
        libexec_prefix) grep -qx "$SATURNIN_LIBEXEC_PREFIX" ;;
    esac <"$out"; es=$?
    if test $es -gt 0;
    then
        tf_warn "bad output format"
        cat "$out"
        return "$TF_ES_FAIL"
    fi
}

tf_do_subtest() {
    local name=$1
    local o_err="oracle/$name.stderr"
    SATURNIN_APP_VERSION=1.2.3
    SATURNIN_CACHE_HOME=chm_$(rndtoken)
    SATURNIN_CONF_PATH=cp_$(rndtoken)
    SATURNIN_LIBEXEC=lex_$(rndtoken)
    SATURNIN_LIBEXEC_PREFIX=lexpfx_$(rndtoken)
    case $name in
        unknown)        tf_testflt -n "$name" -E "$o_err" -S 2 saturnin__main --saturnin-get-foo             ;;
        unknown_dash1)  tf_testflt -n "$name" -E "$o_err" -S 2 saturnin__main --saturnin-get--               ;;
        unknown_dash2)  tf_testflt -n "$name" -E "$o_err" -S 2 saturnin__main --saturnin-get---              ;;
        unknown_none)   tf_testflt -n "$name" -E "$o_err" -S 2 saturnin__main --saturnin-get-                ;;
        unknown_same)   tf_testflt -n "$name" -E "$o_err" -S 2 saturnin__main --saturnin-get---saturnin-get  ;;
        saturnin_conf_path)   saturnin__main --saturnin-get-saturnin-conf-path | ckfuzzy ;;
        app_version)    saturnin__main --saturnin-get-app-version     | ckfuzzy ;;
        cache_home)     saturnin__main --saturnin-get-cache-home      | ckfuzzy ;;
        libexec)        saturnin__main --saturnin-get-libexec         | ckfuzzy ;;
        libexec_prefix) saturnin__main --saturnin-get-libexec-prefix  | ckfuzzy ;;
    esac
}

tf_do_subtests