###############################################################################
# File .bashrc
# jul.io
###############################################################################
umask 022

# set PATH so it includes user's private bin if it exists
if [ -d $HOME/bin ] ; then
    PATH="$HOME/bin:${PATH}"
    export PATH
fi

if [ -d ~/.local/bin ] ; then
    PATH="$HOME/.local/bin:${PATH}"
    export PATH
fi

if [ -d ~/.kubectx ] ; then
    PATH="$HOME/.kubectx:${PATH}"
    export PATH
fi

# do the same with MANPATH
if [ -d $HOME/man ]; then
    MANPATH=$HOME/man:"${MANPATH}"
    export MANPATH
fi

# some programs use a special .dot directory for not
# trashing the home directory with .dotfiles
if [ -d $HOME/.dot ]; then
    DOT=$HOME/.dot
    export DOT
fi

export COLORTERM=1

# change darkblue dir-colors to lightgrey because it's unreadable with black background
LS_COLORS="di=01;90"
export LS_COLORS

# prefered editor
export EDITOR=vi

# bash history control
export HISTCONTROL=ignoreboth:erasedups
export HISTFILESIZE=
export HISTSIZE=5000
export HISTTIMEFORMAT="%Y-%m-%d %T "
shopt -s histappend
shopt -s cmdhist

# completion
shopt -s cdspell
shopt -s hostcomplete

# If not running interactively don't do any more
[ -z "$PS1" ] && return

###############################################################################
# running interactively
###############################################################################
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# only linux, not SunOS etc
if [ `uname` == "Linux" ]; then
    # enable color support of ls
    if [ "$TERM" != "dumb" ]; then
        eval `dircolors -b`
        alias ls='ls --color'
    fi
fi

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(lesspipe)"

parse_git_branch() {
	     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
		 }

## set a fancy prompt
# --- 1. Color-Definitions (256-Colors) ---
# Format: \[ \033[38;5;COLOR_IDm \]  ('m' marks the end of the code)

# Reset standard text
F_RESET="\[\033[00m\]"

# Colors for user
U_USER="\[\033[38;5;3m\]"   # Gelb/Orange
U_PWD="\[\033[38;5;39m\]"   # soothing sky-blue

# Colors for Root
R_USER="\[\033[1;31m\]"     # bold, red (important!)
R_PWD="\[\033[38;5;203m\]"  # smooth salmon-rose

# Common colors
#C_HOST="\[\033[38;5;7m\]"   # Light Grey
#C_HOST="\[\033[38;5;0m\]"   # Black
C_HOST="\[\033[38;5;214m\]" # Orange
C_GIT="\[\033[38;5;15m\]"   # White
C_CHAR="\[\033[38;5;15m\]"  # White (for colon)


# --- 2. Logic and Prompt-Composition ---
if [ "$UID" -eq 0 ]; then
    # --- ROOT PROMPT ---
    # Structure: Time [RootColor]User [Normal]@[Host] : [RootPath]Path [Git] \n #
    export PS1="\t ${R_USER}\u${C_HOST}@\h${C_CHAR}:${R_PWD}\w${C_GIT}\$(parse_git_branch)$F_RESET\n# "
else
    # --- USER PROMPT ---
    # Structure: Time [UserFarbe]User [Normal]@[Host] : [PathFarbe]Path [Git] \n $
    export PS1="\t ${U_USER}\u${C_HOST}@\h${C_CHAR}:${U_PWD}\w${C_GIT}\$(parse_git_branch)$F_RESET\n\\$ "
fi

# Cleanup (for the variables not to stau 'exported')
unset F_RESET U_USER U_PWD R_USER R_PWD C_HOST C_GIT C_CHAR


# define your own aliases in ~/.bash_aliases
[ -f $HOME/.bash_aliases ] && source $HOME/.bash_aliases

# local bashrc
[ -f $HOME/.bashrc_local ] && source $HOME/.bashrc_local

# enable programmable completion features
if [ -f /etc/bash_completion ]; then
  . /etc/bash_completion
fi

# host-specific bashrc
[ -f $HOME/.bashrc_$HOSTNAME ] && source $HOME/.bashrc_$HOSTNAME

# This allows you to bookmark your favorite places across the file system
# Define a variable containing a path and you will be able to cd into it regardless of the directory you're in
shopt -s cdable_vars

# Examples:
# export dotfiles="$HOME/dotfiles"

# colorful manpages
export LESS_TERMCAP_mb=$'\e[1;32m'
export LESS_TERMCAP_md=$'\e[1;32m'
export LESS_TERMCAP_me=$'\e[0m'
export LESS_TERMCAP_se=$'\e[0m'
export LESS_TERMCAP_so=$'\e[01;33m'
export LESS_TERMCAP_ue=$'\e[0m'
export LESS_TERMCAP_us=$'\e[1;4;31m'
