# -*- mode: shell-script -*-

exname="$(basename $0)"

if [ -t 1 ]; then
    GRAY=$(echo -en "\e[1;30m")
    RED=$(echo -en "\e[1;31m")
    GREEN=$(echo -en "\e[1;32m")
    YELLOW=$(echo -en "\e[1;33m")
    BLUE=$(echo -en "\e[1;34m")
    PINK=$(echo -en "\e[1;35m")
    CYAN=$(echo -en "\e[1;36m")
    WHITE=$(echo -en "\e[1;37m")

    DARKGRAY=$(echo -en "\e[0;30m")
    DARKRED=$(echo -en "\e[0;31m")
    DARKGREEN=$(echo -en "\e[0;32m")
    DARKYELLOW=$(echo -en "\e[0;33m")
    DARKBLUE=$(echo -en "\e[0;34m")
    DARKPINK=$(echo -en "\e[0;35m")
    DARKCYAN=$(echo -en "\e[0;36m")

    NORMAL=$(echo -en "\e[0m")
fi

function out() { cat "$tmp_out"; }
function err() { cat "$tmp_err"; }
function errlvl() { cat "$tmp_errlvl"; }
function var() { echo "${$1}"; }


function time_note() {
    echo "scale=1 ; l($1 - $empty_try_time) / l(10)" | bc -l
}

function swallow_last_time() {
    if test "$sum_time" == "0" -a -z "$cmd"; then ## catches first empty try ''
        empty_try_time="$(echo "scale=0 ; $time_diff / 2" | bc -l )"
        return 0
    fi
    test -z "$test_counter" && test_counter=0 || test_counter=$[$test_counter + 1]
    test -z "$sum_time" && sum_time=0
    test_name=${exname}_${test_counter}
    if test "$time_diff"; then
        test_time_note=$(time_note $time_diff)
        profiler_info="$(echo -en "$profiler_info\n- $test_name\t$test_time_note")"
        sum_time=$(echo "scale=3; $sum_time + $time_diff" | bc -l )
    fi

}

function time_exec() {
    beg_exec=$(date +%s.%N)
    ( echo "$*" | bash )
    errorlevel=$?
    end_exec=$(date +%s.%N)
    time_diff="$(echo "scale=3; ($end_exec - $beg_exec)*1000000" | bc | cut -f 1 -d ".")"
    return $errorlevel
}


function try() {
    swallow_last_time
    cmd="$*"
    desc=$(echo ; echo "$ $cmd" )
    time_exec "$prefix_cmd$cmd"  1> "$tmp_out" 2> "$tmp_err"
    echo $? > "$tmp_errlvl"
}

function apply_opt() {
    code=$(cat -)
    for opt in $*; do
        code=$(echo "$code" | $opt)
    done
    echo "$code"
}

function NOCOLOR() {
    esc_char=$(echo -en "\e")
    cat - | sed -r "s/$esc_char\[[0-9]+(;[0-9]+)*m//g"
}

function NOPOS() {
    esc_char=$(echo -en "\e\\[[0-9]\\+[GA]")
    cat - | sed "s/$esc_char//g"
}

function TRIM() {
    cat - | sed -r "s/^ +//g" | sed -r "s/ +\$//g"
}

function RTRIM() {
    cat - | sed -r "s/ +\$//g"
}

function SIZE() {
    cat - | wc -c
}

## usage:
##   is ACTION [reg] CODE [OPTION ...]
is() {
    local act="$1" type code msg
    test -z "$total" && total=0
    shift

    case "$1" in
        reg|part)
            type="$1"
            shift
            ;;
        *)
            type=""
            ;;
    esac

    code="$1"
    shift
    #code=$(echo "$code" | apply_opt $*)
    msg=$(echo "$type $code" | cut -c -30)

    output=$($act | apply_opt $*)
    case "$type" in
        "")
            test "$code" == "$output"
            ;;
        "part")
            [[ "$output" == *"$code"* ]]
            ;;
        ("reg")
            echo -n "$output" | egrep -- "$code" >/dev/null 2>&1
            ;;
    esac && total=$[$total + 1] &&
    echo "[v] is $act $msg" >/dev/null && return 0
    echo "$desc"
    echo "[ ] is $act $msg"
    echo "--- $*"
    echo -n "$output"
    echo
    echo "--- DIFF"
    diff -u <(echo "$code") <(echo "$output") | egrep -v '^(---|\+\+\+) /'
    exit 1

}

function summary() {
    swallow_last_time

    echo "$profiler_info"
    echo
    echo "$total tests conducted in $(echo "scale=3;$sum_time/1000000" | bc) s ($(time_note $sum_time))"
}
function noerror() {
    is err ''
    is errlvl 0
}

pid=$$
tmp_dir="/tmp"
tmp_out="$tmp_dir/test.$pid.out.tmp"
tmp_err="$tmp_dir/test.$pid.err.tmp"
tmp_errlvl="$tmp_dir/test.$pid.errlvl.tmp"

try ''
try ''
try ''