#!/usr/bin/env bash
# shellcheck disable=SC1090,SC1091

### /etc/bashrc
[ ! -f /etc/bashrc ] || . /etc/bashrc

# Prefer US English
export LANG="en_US"
if [[ "$(hostname)" != *'-minimal' ]]; then
  export LC_ALL="en_US.UTF-8"
fi

# Detect support for advanced terminal features
if [ -t 1 ]; then
  if [ "$0" = 'bash' ] || [ "$0" = '/bin/bash' ]; then
    export BASH_SUPPORT=true

    ### Pre-exec
    # Source: https://github.com/rcaloras/bash-preexec
    if [ -f "${XDG_CONFIG_HOME:-$HOME/.config}/shell/bash/pre-exec.bash" ]; then
      # export __bp_enable_subshells="true"
      . "${XDG_CONFIG_HOME:-$HOME/.config}/shell/bash/pre-exec.bash"
      preexec() {
        true
      }
      precmd() {
        true
      }
    fi
  fi
fi

### Import Common Settings
export SHOW_MOTD=false
[ ! -f "${XDG_CONFIG_HOME:-$HOME/.config}/shell/profile.sh" ] || . "${XDG_CONFIG_HOME:-$HOME/.config}/shell/profile.sh"

### Escape if user is root
if [ "$USER" = 'root' ]; then
  return
fi

### Ble.sh
# Loaded with bash-it
# [[ $- == *i* ]] && source "${XDG_DATA_HOME:-$HOME/.local/share}/blesh/ble.sh" --noattach --rcfile "${XDG_CONFIG_HOME:-$HOME/.config}/blesh/bleshrc"

### Escape if shell not interactive
case $- in
  *i*) ;;
    *) return;;
esac

### History
export HISTFILE="$XDG_STATE_HOME/bash/history"
if command -v hstr > /dev/null; then
  ### Append new history items to .bash_history
  shopt -s histappend
  ### Ensure synchronization between bash memory and history file
  export PROMPT_COMMAND="history -a; history -n; ${PROMPT_COMMAND}"
  if [[ $- =~ .*i.* ]]; then bind '"\C-r": "\C-a hstr -- \C-j"'; fi
  ### If this is interactive shell, then bind 'kill last command' to Ctrl-x k
  if [[ $- =~ .*i.* ]]; then bind '"\C-xk": "\C-a hstr -k \C-j"'; fi
fi
### Bash Initialization Hooks
if [ "$BASH_SUPPORT" = 'true' ] && [ -n "$BASH" ]; then
  ### Sensible Bash
  # Source: https://github.com/mrzool/bash-sensible
  [ ! -f "${XDG_DATA_HOME:-$HOME/.local/share}/shell/sensible.bash" ] || source "${XDG_DATA_HOME:-$HOME/.local/share}/shell/sensible.bash"

  ### Fig / LC_ALL
  [ ! -f "$HOME/.fig/shell/bashrc.pre.bash" ] || source "$HOME/.fig/shell/bashrc.pre.bash"

  # Prompt (on bash only)
  if [[ "$(hostname)" != *'-minimal' ]]; then
    ### Styled Terminal
    export BASH_IT="${XDG_DATA_HOME:-$HOME/.local/share}/bash_it"
    export BASH_IT_THEME="powerline"
    if command -v powerline-daemon > /dev/null && [ -f "$BASH_IT/bash_it.sh" ]; then
      source "$BASH_IT/bash_it.sh"
    elif command -v oh-my-posh > /dev/null; then
      # Oh My Posh (See: https://ohmyposh.dev/)
      eval "$(oh-my-posh init bash --config "${XDG_CONFIG_HOME:-$HOME/.config}/oh-my-posh/Betelgeuse.omp.json")"
    else
      # Basic styled terminal
      case "$TERM" in
      xterm* | rxvt* | Eterm | aterm | kterm | gnome* | alacritty)
        PS1="\n \[\033[0;34m\]╭─\[\033[0;31m\]\[\033[0;37m\]\[\033[41m\] $OS_ICON \u \[\033[0m\]\[\033[0;31m\]\[\033[44m\]\[\033[0;34m\]\[\033[44m\]\[\033[0;30m\]\[\033[44m\] \w \[\033[0m\]\[\033[0;34m\] \n \[\033[0;34m\]╰ \[\033[1;36m\]\$ \[\033[0m\]"
        ;;
      esac
    fi
  else
    # Minimal session
    ! command -v oh-my-posh > /dev/null || eval "$(oh-my-posh init bash --config "${XDG_CONFIG_HOME/.config}/oh-my-posh/Betelgeuse-minimal.omp.json")"
  fi

  ### Bash Completion (Homebrew)
  if command -v brew  > /dev/null; then
    if [[ -r "${HOMEBREW_PREFIX}/etc/profile.d/bash_completion.sh" ]]; then
      source "${HOMEBREW_PREFIX}/etc/profile.d/bash_completion.sh"
    else
      for COMPLETION in "${HOMEBREW_PREFIX}/etc/bash_completion.d/"*; do
        [[ -r "${COMPLETION}" ]] && source "${COMPLETION}"
      done
    fi
  fi

  ### Atuin
  ! command -v atuin > /dev/null || eval "$(atuin init bash)"

  ### Carapace
  ! command -v carapace > /dev/null || source <(carapace _carapace)

  ### Cod
  ! command -v cod > /dev/null || source <(cod init $$ bash)

  ### Conda
  # if command -v conda > /dev/null && [ -f "${HOMEBREW_PREFIX:-/opt/homebrew}/Caskroom/mambaforge/base/bin/conda" ]; then
	#   __conda_setup="$("${HOMEBREW_PREFIX:-/opt/homebrew}/Caskroom/mambaforge/base/bin/conda" "shell.zsh" "hook" 2> /dev/null)"
	#   if [ $? -eq 0 ]; then
	# 	  eval "$__conda_setup"
	#   else
	# 	  if [ -f "${HOMEBREW_PREFIX:-/opt/homebrew}/Caskroom/mambaforge/base/etc/profile.d/conda.sh" ]; then
	# 		  . "${HOMEBREW_PREFIX:-/opt/homebrew}/Caskroom/mambaforge/base/etc/profile.d/conda.sh"
	# 	  else
	# 		  export PATH="${HOMEBREW_PREFIX:-/opt/homebrew}/Caskroom/mambaforge/base/bin:$PATH"
	# 	  fi
	#   fi
  # fi

  ### Google Cloud SDK
  [ ! -f "${HOMEBREW_PREFIX}/share/google-cloud-sdk/path.bash.inc" ] || source "$HOMEBREW_PREFIX/share/google-cloud-sdk/path.bash.inc"

  ### direnv
  ! command -v direnv > /dev/null || eval "$(direnv hook bash)"

  ### Emplace
  ! command -v emplace > /dev/null || source <(emplace init bash)

  ### Goto
  [ -f "${XDG_CONFIG_HOME:-$HOME/.config}/shell/goto.sh" ] || source "${XDG_CONFIG_HOME:-$HOME/.config}/shell/goto.sh"
  
  ### hiSHtory
  if [ -f "${XDG_CONFIG_HOME:-$HOME/.config}/hishtory/config.sh" ]; then
	  export PATH="$PATH:${XDG_CONFIG_HOME:-$HOME/.config}/hishtory"
	  source "${XDG_CONFIG_HOME:-$HOME/.config}/hishtory/config.sh"
  fi

  ### Hoard
  if command -v hoard > /dev/null && [ -f "${XDG_DATA_HOME:-$HOME/.local/share}/hoard/src/shell/hoard.bash" ]; then
    source "${XDG_DATA_HOME:-$HOME/.local/share}/hoard/src/shell/hoard.bash"
  fi

  ### iTerm2
  [ ! -f "${XDG_CONFIG_HOME:-$HOME/.config}/shell/iterm/iterm2.bash" ] || source "${XDG_CONFIG_HOME:-$HOME/.config}/shell/iterm/iterm2.bash"

  ### Fig
  [ ! -f "$HOME/.fig/shell/bashrc.post.bash" ] || . "$HOME/.fig/shell/bashrc.post.bash"

  ### mise
  ! command -v mise > /dev/null || eval "$(mise activate bash)"

  ### Navi (Ctrl+G)
  ! command -v navi > /dev/null || eval "$(navi widget bash)"

  ### ntfy
  ! command -v ntfy > /dev/null || eval "$(ntfy shell-integration)"

  ### pkgx
  ! command -v pkgx > /dev/null || eval "$(pkgx --shellcode)"

  ### RTX
  ! command -v rtx > /dev/null || eval "$(rtx activate bash)"

  ### SDKMan
  ### Using mise instead for Java handling
  # if command -v brew > /dev/null && command -v sdkman-cli > /dev/null; then
  #   export SDKMAN_DIR="$(brew --prefix sdkman-cli)/libexec"
  #   source "$SDKMAN_DIR/bin/sdkman-init.sh"
  # elif [ -f "$SDKMAN_DIR/bin/sdkman-init.sh" ]; then
  #   export SDKMAN_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/sdkman"
  #   source "$SDKMAN_DIR/bin/sdkman-init.sh"
  # fi

  ### Sheldon
  export SHELDON_CONFIG_FILE="${SHELDON_CONFIG_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/sheldon}/plugins.bash.toml"
  ! command -v sheldon > /dev/null || eval "$(command sheldon source)"

  ### Up
  [ ! -f "${XDG_DATA_HOME:-$HOME/.local/share}/up/up.sh" ] || source "${XDG_DATA_HOME:-$HOME/.local/share}/up/up.sh"

  ### Vault
  ! command -v vault > /dev/null || complete -C vault vault

  ### zoxide
  if command -v zoxide > /dev/null; then
    eval "$(zoxide init bash)" > /dev/null
  else
    alias z='cd'
  fi

  ### Ble.sh
  # Loaded with bash-it
  # [[ ${BLE_VERSION-} ]] && ble-attach
fi