#!/bin/bash

######################################################
### things to do AFTER host/user-specific settings ###
######################################################

#shellcheck disable=2120
__bashum__lastrv() {
    local rvfile        # path to return value cache file
    local lastrv        # actual last return value
    local do_rm         # true if we should remove the cache
    rvfile="$RV_TMP/$$.lastrv"
    lastrv="$(cat "$rvfile")"
    do_rm=false
    test "$1" == "-r" && do_rm=true
    if [ "0$lastrv" -gt 0 ];
    then
        echo "!$lastrv!"
    else
        echo ""
    fi
    $do_rm && rm -f "$rvfile"
}

__bashum__save_rv() {
    local rvfile="$RV_TMP/$$.lastrv"
    [ -w "${rvfile%/*}" ] && echo "$1" > "$rvfile"
}

__bashum__task_context() {
    local ctx       # TaskWarrior context
    type -t task >/dev/null || return 1
    ctx=$(task _get rc.context)
    test -n "$ctx" || return
    echo -n "|$ctx"
}

#shellcheck disable=SC2154 disable=2016
__bashum__mkps1() {
    # these functions must be already defined by ~/.bash/user/*.bashrc
    # and ~/.bash/host/*.bashrc
    test -n "$SSH_CONNECTION" \
     && echo -n "$(__bashum__mkps1user)"    # username only when on ssh
    echo -n "$lwhite@"                      # nice shiny at sign
    echo -n "$(__bashum__mkps1host)"        # hostname colored per host
    echo -n "$yellow"                       #\
    echo -n     '$(__bashum__task_context)' # > taskwarrior's context
    echo -n "$normal"                       #/
    echo -n "$lwhite:"                      # nice shiny colon
    echo -n "$lblue\w$normal"               # current workdir
    echo -n "$green"                        #\
    echo -n     '$(__git_ps1 "(%s)")'       # > git's PS1
    echo -n "$normal"                       #/
    echo -n "$lred"                         #\
    echo -n     '$(__bashum__lastrv -r)'    # > last exit status (nothing if zero)
    echo -n "$normal"                       #/
    echo '$ '                               # obligatory dollar
}

#shellcheck disable=SC2154
__bashum__mkps2() {
    echo "$lwhite·$blue>$yellow>$white>$normal "
}

# and use to assemble own PS1
PS1=$(__bashum__mkps1)
PS2=$(__bashum__mkps2)


__bashum__set_title() {
    #
    # Show the currently running command in the terminal title:
    #
    # http://www.davidpashley.com/articles/xterm-titles-with-bash.html
    #
    case "$BASH_COMMAND" in
        *"\033]"0*)
            # The command is trying to set the title bar as well;
            # this is most likely the execution of $PROMPT_COMMAND.
            # In any case nested escapes confuse the terminal, so don't
            # output them.
            ;;
        "")
            echo -ne "\033]0; "
            echo -n    "$(__bashum__lastrv)$(__bashum__mkhostid)${PWD/$HOME/\~}\$"
            echo -ne "\007"
            ;;
        *)
            echo -ne "\033]0; "
            echo -n    "${BASH_COMMAND} ($(__bashum__mkhostid)${PWD/$HOME/\~})"
            echo -ne "\007"
            ;;
    esac
}

#shellcheck disable=SC2016
__bashum__mkpc() {
    #
    # Compose PROMPT_COMMAND body
    #
    echo -n '__bashum__save_rv $?;'
    case "$TERM" in
        xterm*|rxvt*|screen*)
            echo -n 'echo -ne "\033]0;'
            echo -n   ' '
            echo -n   '$(__bashum__lastrv)'
            echo -n   "$(__bashum__mkhostid)"
            echo -n   '${PWD/$HOME/\~}'
            echo -n   '\$'
            echo -n '\007"'
            ;;
    esac
}

__bashum__mkhostid() {
    test -n "$SSH_CONNECTION" || return
    echo "${HOSTNAME%%.*}:"
}

__bashum__setup_traps() {
    case "$TERM" in
        xterm*|rxvt*|screen*) trap __bashum__set_title DEBUG ;;
    esac
}

__bashum__setup_traps

PROMPT_COMMAND=$(__bashum__mkpc)

bmo nag