###############################################################################
# File .bashrc
# Jonas Mitschang - jonen@mitschang.net
###############################################################################
umask 022

# set PATH so it includes user's private bin if it exists
if [ -d ~/bin ] ; then
    PATH="~/bin:${PATH}"
    export PATH
fi
# python (pip install) puts binaries here:
if [ -d ~/.local/bin ] ; then
    PATH="~/.local/bin:${PATH}"
    export PATH
fi

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

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

export COLORTERM=1
#export GREP_OPTIONS='--color=auto'

# prefered editor
export EDITOR=vim

# bash history control
export HISTCONTROL=ignoreboth
export HISTTIMEFORMAT='%d/%m/%y %T '
export HISTFILESIZE=
export HISTSIZE=5000
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)"

# set a fancy prompt
if [ $UID == 0 ]; then
	# root: red
	#PS_COLOR="\[\033[1;31m\]"
	PS_COLOR="9"
else
	# user
	#PS_COLOR="\[\033[33m\]"
	PS_COLOR="31"
fi

function _fgbg() {
	echo -n "\[\e[0;38;5;${1};48;5;${2}m\]"
	# echo -n "\["
	# FG: echo -n "\e[0;38;5;${1}m"
	# BG: echo -n "\e[48;5;${2}m"
	# echo -n "\]"
}
# old foreground, new foreground, common background
function _arr() {
	_fgbg $1 $3
	echo -n 
	if [[ $2 == -1 ]]; then
		echo -n "\[\e[0m\]"
	else
		_fgbg $2 $3
	fi
}

#PS1="\t$(_arr 0 220 166)\h$(_arr 166 231 $PS_COLOR)\u$(_arr $PS_COLOR 252 240)\w$(_arr 240 -1 0)"
#TODO \${?/0/}"
# set a fancy prompt
if [ $UID == 0 ]; then
  # root: red
  PS_COLOR="\[\033[1;31m\]"
else
  # user
  PS_COLOR="\[\033[33m\]"
fi
PS1="\t ${PS_COLOR}${debian_chroot:+($debian_chroot)}\u\[\033[0m\]@\h:\w\$ "


# if this is an xterm modify the title
case $TERM in
xterm*)
    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME} $(date +%H:%M:%S) [${PWD}]\007"'
    ;;
*)
    ;;
esac

# powerline - prompt and statusline utility
#[ -f /usr/share/powerline/bindings/bash/powerline.sh ] && source /usr/share/powerline/bindings/bash/powerline.sh

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

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

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

if [ -x /usr/bin/neofetch ] && [ ! -f /etc/update-motd.d/20-pico ]; then
	/usr/bin/neofetch
fi

