+###########################
+# Settings for all shells #
+###########################
+
+# Set umask, depending on the user ID
+if [ "$(id -un)" = "root" ]; then
+ umask 0022
+else
+ umask 0007
+fi
+
+# Add ~/bin and subdirs to PATH if needed
+while read -r p; do
+ echo "$PATH" | tr ':' '\n' | grep -qxF "$p" || PATH="${p}:$PATH"
+done < <(find -L ~/bin -maxdepth 1 -type d 2> /dev/null)
+
+# Set EMAIL from the freedesktop environment.d
+if [ -r ~/.config/environment.d/10-email.conf ]; then
+ source ~/.config/environment.d/10-email.conf
+ export EMAIL
+fi
+
+# Pick up SSH agent socket
+if [ -z "$SSH_AUTH_SOCK" ]; then
+ if [ -r ~/.ssh-agent.env ]; then
+ source ~/.ssh-agent.env
+ else
+ SSH_AUTH_SOCK="/run/user/$(id -u)/keyring/ssh"
+ export SSH_AUTH_SOCK
+ fi
+fi
+
+
+###########################
+# Interactive shells only #
+###########################
+
+if [ -z "${PS1}" ]; then
+ return
+fi
+
+
+################
+# bash options #
+################
+
+# Bash should check the terminal size after every command terminates
+shopt -s checkwinsize
+
+# Don't attempt to tab-complete an empty line
+shopt -s no_empty_cmd_completion
+
+# Prevent overwriting existing files on stdout redirection
+set -o noclobber
+
+# Better history
+shopt -s histappend
+shopt -s cmdhist
+export HISTCONTROL='erasedups:ignoredups:ignorespace'
+export HISTSIZE='100000'
+export HISTTIMEFORMAT='%F %T '
+
+
+##################
+# Terminal setup #
+##################
+
+# Disable CTRL-s / CTRL-q
+stty -ixon
+
+# hostname:workingdir
+PROMPT_COMMAND='echo -ne "\\033]0;$(hostname| cut -d. -f1):${PWD/$HOME/~}\\007"'
+
+
+#############
+# Fancy PS1 #
+#############
+
+# Revision control status for git, hg, svn
+
+function find_up_exists {
+ local d="$PWD"
+ while [ -n "$d" ]; do
+ if [ -e "$d/$1" ]; then
+ return 0
+ fi
+ d="${d%/*}"
+ done
+ return 1
+}
+
+[ -r /usr/share/git/completion/git-prompt.sh ] && source /usr/share/git/completion/git-prompt.sh
+function my_git_ps1 {
+ find_up_exists .git || return
+ GIT_PS1_SHOWDIRTYSTATE=1 \
+ GIT_PS1_SHOWUNTRACKEDFILES=1 \
+ __git_ps1 ' (%s)' 2> /dev/null
+}
+
+function my_hg_ps1 {
+ find_up_exists .hg || return
+ local status
+ status="$(hg status | cut -c1 | sort -u | tr -d ' \n')"
+ echo -n " ($status)"
+}
+
+function my_svn_ps1 {
+ find_up_exists .svn || return
+ local status
+ status="$(svn status --ignore-externals 2> /dev/null | cut -c1 | sort -u | tr -d ' \n')"
+ [ -n "$status" ] && echo -n " ($s)"
+}
+
+# Two line prompt
+PS1=''
+PS1="$PS1"'\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]'
+PS1="$PS1"'\[\033[01;36m\]$(my_git_ps1 ; my_hg_ps1 ; my_svn_ps1)\[\033[00m\]'
+
+if ! [[ "$TERM" =~ ^screen ]]; then
+ PS1="$PS1"'\n\$ '
+else
+ # matches with shelltitle in .screenrc
+ PS1="$PS1"'\n\[\033k\033\\\]\$> '
+fi
+
+
+#################################
+# Display return codes on error #
+#################################
+
+function print_exit_code {
+ _exit_msg="\\033[01;33mexit code: $?\\033[00m"
+ if [ -z "${BASH_SOURCE[1]}" ]; then
+ echo -e "$_exit_msg"
+ fi
+ unset _exit_msg
+}
+trap print_exit_code ERR
+
+
+###############
+# Pager setup #
+###############
+
+export PAGER='less'
+export LESS='RS'
+
+
+################
+# Editor setup #
+################
+
+if emacsclient --version &> /dev/null; then
+ export ALTERNATE_EDITOR='vim'
+ export EDITOR='emacsclient --tty'
+
+ if [[ "$TERM" == screen* ]]; then
+ alias edit='emacsclient --tty'
+ else
+ alias edit='emacsclient --create-frame --no-wait'
+ fi
+else
+ export EDITOR='vim'
+ alias edit='vim'
+fi
+
+
+##########################
+# ls aliases and colours #
+##########################
+
+# GNU ls colours
+# shellcheck disable=SC2046
+eval $(TERM=xterm dircolors 2> /dev/null)
+
+# BSD ls colours
+export LSCOLORS="ExFxCxDxBxEGEDABAGACAD"
+
+# Lets find the ls
+if ls --color=auto -v &> /dev/null; then
+ alias ls='ls --color=auto -v'
+elif gls --color=auto -v &> /dev/null; then
+ alias ls='gls --color=auto -v'
+elif ls -G &> /dev/null; then
+ alias ls='ls -G'
+else
+ alias ls='ls -F'