### Debug

# In case the config is buggy, set TR_NO_SHELL_CONFIG before sourcing it to
# disable it

if [ -n "$TR_NO_SHELL_CONFIG" ]
then
    echo "Not loading Bash profile"
    return 0
fi

### Make sure profile is loaded, and that it is loaded before Bashrc

if [ -z "$DOTFILES_DIR" ]
then
    source "$HOME/.bash_profile"
fi

### History

HISTCONTROL=ignorespace:erasedups
HISTSIZE=10000
HISTFILESIZE=10000

### Shell Behavior

# cd just by typing directories
shopt -s autocd
# include dotfiles in completion and correct directories spelling
shopt -s dirspell dotglob
# enable ** wildcard
shopt -s globstar
# append to the history file, don't overwrite it
shopt -s histappend
# case-insensitive completion
shopt -s nocaseglob

### Aliases

if [ -f "$HOME/.bash_aliases" ]
then
    source "$HOME/.bash_aliases"
fi

if [ -f "$DOTFILES_DIR/shell/bash_aliases" ]
then
    source "$DOTFILES_DIR/shell/bash_aliases"
fi

### Completion

if [ -f '/usr/share/bash-completion/bash_completion' ]
then
    source /usr/share/bash-completion/bash_completion
fi

### Machine-local config

if [ -f "$DOTFILES_DIR/shell/local" ]
then
    source "$DOTFILES_DIR/shell/local"
fi

### Opam config

opam_env() {
    if [ -f "$DOTFILES_DIR/shell/opam" ]
    then
	source "$DOTFILES_DIR/shell/opam"
	echo "Opam environment loaded"
    else
	echo "No Opam environment to load"
    fi
}

### Guix environment

guix_env() {
    if [ -f "$HOME/.config/guix/current" ]
    then
	source "$HOME/.config/guix/current/etc/profile"
	echo "Guix environment loaded"
    else
	echo "No Guix environment to load"
    fi
}

### Prompt

color_red='\e[0;31m'
color_green='\e[0;32m'
color_yellow='\e[1;33m'
color_blue='\e[1;34m'
color_reset='\e[0m'

# Detect without we are running on a "local" machine (i.e. laptop or desktop,
# not server).
#
# Usage:
# if [ "$(machine_is_local)" = 'true' ]
# then
#     echo "I am on a local machine"
# else
#     echo "I am not on a local machine"
# fi
machine_is_local() {
    case "$(hostname -s)" in
	'fenetre'|'menestrel'|'sapin')
	    echo 'true';;
	*)
	    echo 'false';;
    esac
}

prompt_hostname() {
    if [ "$(machine_is_local)" = 'false' ]
    then
	echo "${color_green}$(hostname -s)${color_reset}:"
    fi
}

prompt_guix() {
    if [ -n "$GUIX_ENVIRONMENT" ]
    then
	echo " ${color_yellow}GUIX"
    fi
}

prompt_last_exit() {
    local last_exit_status=$?
    if [ $last_exit_status -ne 0 ]
    then
	echo " ($last_exit_status)"
    fi
}

prompt_emoji() {
    if [ ! -t 0 ]
    then
	# No emoji if stdin is not a TTY
	echo '$'
    elif [ "$TERM" = 'linux' ]
    then
	# No emoji in Linux terminal
	echo '$'
    elif [ -n "$INSIDE_EMACS" ]
    then
	# No emoji if running within Emacs
	echo '$'
    elif [ -f "$DOTFILES_DIR/shell/emoji.txt" ]
    then
	# Prioritize local emoji definition
	cat "$DOTFILES_DIR/shell/emoji.txt"
    elif [ -f "$HOME/emoji.txt" ]
    then
	cat "$HOME/emoji.txt"
    elif [ "$(machine_is_local)" = 'true' ]
    then
	# Default emoji for local machines
	echo '🍷'
    else
	# Default is no emoji
	echo '$'
    fi
}

PS1="${color_reset}$(prompt_hostname)\
${color_blue}\\w${color_reset} \
${color_green}${SHLVL}${color_reset}\
$(prompt_guix)${color_red}\
\$(prompt_last_exit)${color_reset}
$(prompt_emoji)"

BASHRC_LOADED=1

# Local Variables:
# mode: sh
# End:
