#{- ~/.bashrc -}#

#{- SOURCING -}#

# Source profile, if it hasn't already been loaded
if [ -z "$__PROFILE_DONE" ]; then
  . $HOME/.profile
fi

# Source .env to extend with secret/sensitive environment variables if it exists
if [ -r "$HOME/.env" ]; then
  . "$HOME/.env"
fi

if [ -r "$HOME/.config/xdg/env" ]; then
  . "$HOME/.config/xdg/env"
fi

# Source aliases, if they exist
if [ -f "$HOME/.aliases" ] && [ -r "$HOME/.aliases" ]; then
  . "$HOME/.aliases"
fi

# Source private aliases, if they exist
if [ -f "$HOME/.private_aliases" ] && [ -r "$HOME/.private_aliases" ]; then
  . "$HOME/.private_aliases"
fi

# Source functions, if they exist
if [ -f "$HOME/.functions" ] && [ -r "$HOME/.functions" ]; then
  . "$HOME/.functions"
fi

# Source private functions, if they exist
if [ -f "$HOME/.private_functions" ] && [ -r "$HOME/.private_functions" ]; then
  . "$HOME/.private_functions"
fi

#{- PROMPT -}#

if which tput &> /dev/null && tput bold &> /dev/null; then
  red="\001$(tput bold)$(tput setaf 1)\002"
  green="\001$(tput bold)$(tput setaf 2)\002"
  yellow="\001$(tput bold)$(tput setaf 3)\002"
  blue="\001$(tput bold)$(tput setaf 4)\002"
  magenta="\001$(tput bold)$(tput setaf 5)\002"
  cyan="\001$(tput bold)$(tput setaf 6)\002"
  no_color="\001$(tput sgr0)\002"
else
  red="\[\033[1;31m\]"
  green="\[\033[1;32m\]"
  yellow="\[\033[1;33m\]"
  blue="\[\033[1;34m\]"
  magenta="\[\033[1;35m\]"
  cyan="\[\033[1;36m\]"
  no_color="\[\033[0m\]"
fi

export PS1="${green}"'\$'"${no_color} "
export DIRENV_PS1="${red}"'\$'"${no_color} "
export NIX_SHELL_PS1="${blue}"'λ'"${no_color} "
if [ "${HOSTNAME}" != "crypt" ] && [ "${HOSTNAME}" != "tomb" ] || [ "${CONTAINER_ID}" != "" ] || [ "${SSH_TTY}" != "" ]; then
  if [ "${CONTAINER_ID}" ]; then
    OLD_HOSTNAME="${HOSTNAME}"
    HOSTNAME="${CONTAINER_ID}"
  fi

  DIRENV_PS1="${DIRENV_PS1}${blue}${HOSTNAME} ${DIRENV_PS1}"
  NIX_SHELL_PS1="${NIX_SHELL_PS1}${green}${HOSTNAME} ${NIX_SHELL_PS1}"

  if [ "${CONTAINER_ID}" ]; then
    PS1="${PS1}${magenta}${HOSTNAME} ${PS1}"
  else
    PS1="${PS1}${blue}${HOSTNAME} ${PS1}"
  fi

  if [ "${CONTAINER_ID}" ]; then
    HOSTNAME="${OLD_HOSTNAME}"
  fi
fi
export ORIG_PS1=${PS1}

if which direnv &> /dev/null; then
  update_prompt() {
    if [ -n "${IN_NIX_SHELL}" ]; then
      PS1=${NIX_SHELL_PS1}
    elif direnv status | grep -iEq 'RC allowed (true|0)'; then
      PS1=${DIRENV_PS1}
    else
      PS1=${ORIG_PS1}
    fi
  }
  prompt_command_prepend update_prompt
fi

# Continuation Prompt: "> " in cyan
export PS2="${cyan}>${no_color} "
# Debugging Prompt: "# " in blue
export PS4="${blue}#${no_color} "

#{- HISTORY -}#

prompt_command_prepend "history -r"
prompt_command_prepend "history -c"
prompt_command_prepend "history -w"
prompt_command_prepend "history -n"
# PROMPT_COMMAND="echo hist1; history; history -n; echo hist2; history; history -w; echo hist3; history; history -c; echo hist4; history; history -r; echo hist5; history; $PROMPT_COMMAND"
export HISTFILE="$HOME/.history/bash"
export HISTTIMEFORMAT="%F %T  "
export HISTCONTROL="erasedups:ignoreboth"
export HISTFILESIZE=-1
export HISTSIZE=-1

export PERSISTENT_HISTFILE="${HOME}/.history/bash_persistent_history"
log_bash_persistent_history()
{
  local rc=$?
  [[
    $(history 1) =~ ^\ *[0-9]+\ +([^\ ]+\ [^\ ]+)\ +(.*)$
  ]]
  local date_part="${BASH_REMATCH[1]}"
  local command_part="${BASH_REMATCH[2]}"
  # Ensure file exists
  if ! [ -w "$(dirname ${PERSISTENT_HISTFILE})" ]; then
    mkdir -p $(dirname ${PERSISTENT_HISTFILE})
  fi
  if [ "$command_part" != "$PERSISTENT_HISTORY_LAST" ]
  then
    echo $date_part "|" "$rc" "|" "$command_part" >> "${PERSISTENT_HISTFILE}"
    export PERSISTENT_HISTORY_LAST="$command_part"
  fi
}

prompt_command_prepend "log_bash_persistent_history"


# Source bash completion, this file will then lazy load all other completions
source_completions() {
  if [ -r "/usr/share/bash-completion/bash_completion" ]; then
    . "/usr/share/bash-completion/bash_completion"
  elif [ -r "/etc/bash_completion" ]; then
    . "/etc/bash_completion"
  fi
}
source_completions

# Preserve the usefulness of set -x by attempting to hide everything that
# happens in prompt_command
preserve_xtrace() {
  if grep -q "x" <<< $-; then
    XTRACE_WAS_SET=1
    set +x
  fi
} 2>/dev/null
preserve_xtrace_reset() {
  if [ "${XTRACE_WAS_SET}" ]; then
    set -x
    unset XTRACE_WAS_SET
  fi
} 2>/dev/null

prompt_command_prepend preserve_xtrace
prompt_command_append  preserve_xtrace_reset

if $(which direnv &> /dev/null); then
  # Only if direnv isn't already defined in our prompt_command
  if ! grep -q _direnv_hook <<< $PROMPT_COMMAND; then
    eval "$(direnv hook bash)"
  fi
fi
