###### Color and Behavior ######

case $- in
    *i*) ;;
      *) return;;
esac

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
	# We have color support; assume it's compliant with Ecma-48
	# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
	# a case would tend to support setf rather than setaf.)
	color_prompt=yes
    else
	color_prompt=
    fi
fi

# Set 'man' colors
if [ "$color_prompt" = yes ]; then
	man() {
	env \
	LESS_TERMCAP_mb=$'\e[01;31m' \
	LESS_TERMCAP_md=$'\e[01;31m' \
	LESS_TERMCAP_me=$'\e[0m' \
	LESS_TERMCAP_se=$'\e[0m' \
	LESS_TERMCAP_so=$'\e[01;44;33m' \
	LESS_TERMCAP_ue=$'\e[0m' \
	LESS_TERMCAP_us=$'\e[01;32m' \
	man "$@"
	}
fi

unset color_prompt force_color_prompt

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls -G --color=auto'
    alias dir='dir --color=auto'
    alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

###### Useful ENV ######
# This one is not necessary, but quite useful for both the prompt and the CD command (you will see at the shopts)
export work="path to your main work folder"

# Here is the opportunity to add custom path folders
#PATH=

###### Aliases ######

alias v2_norminette="~/norm/norminette.rb"

alias ll='ls -lh'
alias la='ls -lha'

alias dd='dd status=progress'

alias _='sudo'
alias _i='sudo -i'
alias please='sudo'
alias fucking='sudo'

alias gcl='git clone'

###### shopts ######

# enable programmable completion features
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

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

# when using cd, corrects minor spelling errors
shopt -s cdspell

# "$<directory>" expands to "$cd <directory>"
shopt -s autocd

# if directory not found, treats argument as as variable (remember the work variable?)
shopt -s cdable_vars

# warns if jobs are running in background before exiting
shopt -s checkjobs

# do not autofill on empty line
shopt -s no_empty_cmd_completion

# Block > redir if overwrite happens, now needs to be >|
set -C

###### Prompt and Term Title ######

# Used colors
red_c="\[\033[0;31m\]"
end_c="\[\e[0m\]"
white_c="\[\033[0;39m\]"
yellow_c="\[\033[01;33m\]"
blue_c="\[\033[01;96m\]"
blue_oc="\[\033[0;96m\]"
green_c="\[\033[0;32m\]"

# Used charsets
bottomCharset="\342\224\224\342\224\200\342\224\200\342\225\274"
topCharset="\342\224\214\342\224\200"
crossCharset="\342\234\227"
dashCharset="\342\224\200"

# This provides the current branch you are working in
gitStatus=""
function setGitStatus {
	git_status=$(printf "%.10s" $(git rev-parse --abbrev-ref HEAD 2>/dev/null))
}

function psOne {
	# Last process return value
	c_status=$?
	# Count number of running background jobs
	r_jobs=$(jobs | wc -l)
	# Count number of existing docker containers
	r_containers=$(docker ps -a 2>/dev/null | sed '1d' | wc -l)

	# Your path, with a replacement for HOME and work path
	path_r=$(pwd | sed "s|$HOME|~|" | sed "s|$work|work|")
	# Your current computer @42 (or just the hostname cut at the first dot)
	host_r=$(hostname | cut -d "." -f1 )

	setGitStatus
	# Random bullshit go, good luck with debugging
	PS1="$red_c$topCharset$(if [[ $c_status != 0 ]]; then echo "[$white_c$crossCharset$red_c]$dashCharset"; fi)"
	PS1+="$(if [[ $r_jobs != 0 ]]; then echo "[$white_c$r_jobs$red_c]$dashCharset"; fi)"
	PS1+="[$white_c\u$yellow_c@$blue_c\h$red_c]$dashCharset[$green_c$path_r$red_c]"
	PS1+="$(if [[ "$git_status" ]]; then echo "$dashCharset[$blue_c$git_status$red_c]"; fi)"
	PS1+="$(if [[ $r_containers != 0 ]]; then echo "$dashCharset<$blue_oc$r_containers$red_c>"; fi)"
	PS1+="\n$bottomCharset $yellow_c\$$end_c"
}

# Set ps1_sp to the title you want for your terminal
function updateTitle {
	ps1_sp=$(echo "Hello!")
	echo -en "\033]0;$ps1_sp\a"
}

PROMPT_COMMAND='psOne'