# -*- shell-script -*-
# Joerg Arndt's  .jjbashrc
# ... online at http://www.jjj.de/
# your feedback is welcome  mailto: arndt (AT) jjj.de
# version: 2023-May-13 (13:56)

### ------- BASH specific settings -------

## control-d to exit:
export IGNOREEOF=0

function h { history; }


# Set to a value of `ignorespace', it means don't enter lines which
# begin with a space or tab into the history list.  Set to a value
# of `ignoredups', it means don't enter lines which match the last
# entered line.  A value of `ignoreboth' combines the two options.
# Unset, or set to any other value than those above, means to save
# all lines on the history list.
export HISTCONTROL=ignoredups


# Notify of job termination immediately:
set -o notify  # same as -b

# do not follow (i.e. expand) symbolic links:
set -o physical  # same as -P


### programmable completion for bash:
    complete -A directory cd rmdir rd
    complete -A file grep
    complete -A command type
    complete -A function function
    complete -A alias alias
    complete -A variable export ev unset
#    complete -A variable -P '$' echo
    complete -A shopt shopt
    complete -A setopt set
    complete -A builtin builtin
    complete -A user talk
    complete -A helptopic help
    complete -A hostname ping
    complete -p ee &>/dev/null  &&  complete -r ee


# If set, bash checks the window size after each command and, if
# necessary, updates the values of LINES and COLUMNS.
    shopt -s checkwinsize

# If  set,  and  readline is being used, bash will not attempt to
# search  the  PATH  for possible  completions  when  completion
# is attempted on an empty line.
    shopt -s no_empty_cmd_completion

# If  set,  minor errors in the spelling of a directory component in
# a cd command will be corrected. The errors checked for are transposed
# characters, a missing character, and  one  character too many.
    shopt -s cdspell

# If  set, and readline is being used, the results of history
# substitution are not immediately passed to the shell parser.
# Instead, the resulting line is loaded into the readline editing
# buffer, allowing further modification.
    shopt -s histverify


# ===== stuff for prompts: =====
unset PS1
export ME=$USER@$HOSTNAME;
    PS1="$PS1\[$NORMAL$GREEN\]\t\[$NORMAL\] $ME:"

    if [ -n "$DISPLAY" ]; then # xterm: settings for white background
        PS1="$PS1\[$BLUE\]" ; #'`dirs +0`'"/\[$NORMAL\]" ;
    else    # other: settings for black background
        PS1="$PS1\[$BRIGHT$CYAN\]" ; #'`dirs +0`'"/\[$NORMAL\]" ;
    fi

# add path/:
    PS1="$PS1"'\w'"/\[$NORMAL\]" ;

# echo number of jobs iff it is >0:
    JOBSQ='$(test 0 = \j || echo "[\j]")'

# finish prompt:
    if [ "$EUID" = "0" ]; then  # append '#' to prompt:
        PS1="$PS1 \[$MAGENTA\]$JOBSQ\[$NORMAL\]\[$BRIGHT$RED\]# \[$NORMAL\]" ;
    else
        PS1="$PS1 \[$MAGENTA\]$JOBSQ\[$NORMAL\]> " ;
    fi
    export TPS1="$PS1\n " ;

    PS2="\[$MAGENTA\]>>\[$NORMAL\]"

export PS1;
export PS2;

## useful for blind users:
## beep if last command returned != 0
#function check_exit()
#{
#    if [ "$1" -ne 0 ]; then  echo -ne "\a";  fi
#}
#export PROMPT_COMMAND="check_exit \$?";


#
# ===== end (prompts) =====


if [ -n "$DISPLAY" ]; then # set PROMPT_COMMAND:

    TAPP=" __ $(uname -mr) __ $(date +'     %A %d-%m-%Y')";
# use PROMPT_COMMAND for window title:
#    PROMPT_COMMAND="echo -ne '\033]0;'$ME:\`dirs +0\`/'    '`uname -mr` '\007'";
    PROMPT_COMMAND="echo -ne '\033]0;'$ME:\`dirs +0\`/' '$TAPP'\007'";

    TPC="$PROMPT_COMMAND"; # original prompt command
    # 'pp arg' sets title to arg
    # 'pp' resets title to default
    function pp
    {
        if test -z "$1"; then
            PROMPT_COMMAND=$TPC;
        else
            unset PROMPT_COMMAND;
            echo -ne '\033]0;' $@ '\007';
        fi
    }

    function xs  ## cd to dir and set title,  'xs .' just puts dirname into title
    {
        cd $1;  pp ${PWD##*/}
    }

    if test -n "$XTITLE"; then pp $XTITLE; fi


    function  xtitle
    {
        if [ $# = 0 ]; then
            echo -ne '\033]0;' $USER@$HOSTNAME:`dirs`/'    '`uname -mr` '\007'
        else
            echo -ne '\033]0;' $@ '\007'
        fi
    }

else
    unset PROMPT_COMMAND;
fi
#export PROMPT_COMMAND;
