# shellcheck disable=SC2148

# fortune
cookie () {
  fortune -es | cowsay | lolcat -S 5
}
# cookie

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

# use date/time for history
HISTTIMEFORMAT="%F %T: "

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

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

# Autocorrect typos in path names when using `cd`.
shopt -s cdspell

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

# Bash attempts to save all lines of a multiple-line command in the same history entry.
# This allows easy re-editing of multi-line command.
shopt -s cmdhist

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

# Case-insensitive globbing (used in pathname expansion).
shopt -s nocaseglob

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
  debian_chroot=$(cat /etc/debian_chroot)
fi

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

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

if [ "$color_prompt" = yes ]; then
  PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
  PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
  xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
  *)
    ;;
esac

# 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 --color=auto'
  alias diff='diff --color=auto'
  alias grep='grep --color=auto'
  alias fgrep='fgrep --color=auto'
  alias egrep='egrep --color=auto'
fi

# alias ls/tree to exa if exists
if [[ -x $(command -v exa) ]]; then
  alias ls="exa --icons"
  alias tree="ls --tree"
fi

alias l='ls -l'
alias ll='ls -la'

# alternative find
if [[ -x "$(command -v fdfind)" ]]; then
  alias fd='fdfind'
fi

# alias cat to bat
if [[ -x "$(command -v bat)" ]]; then
  alias cat="bat -pp"
fi

# view/edit commands
if [[ -x "$(command -v view)" ]]; then
  alias v='view'
fi

if [[ -x "$(command -v hx)" ]]; then
  alias edit='hx'
  alias e='edit'
fi

# neovim
if [[ -x "$(command -v nvim)" ]]; then
  alias vim='nvim'
fi

# silver searcher with less
if [[ -x "$(command -v ag)" ]]; then
  alias agl='ag --pager "less -RF"'
fi

# maven/java version
if [[ -x "$(command -v mvn)" ]]; then
  alias mvn11="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 mvn"
  alias mvn17="JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 mvn"
fi

# kitty terminal
if [[ -x "$(command -v kitty)" ]]; then
  [[ "$TERM" == "xterm-kitty" ]] && alias ssh="kitty +kitten ssh"
fi

# kubecolor
if [[ -x "$(command -v kubecolor)" ]]; then
  alias k="kubecolor"
  alias wk="watch -c kubecolor --force-colors"
  complete -o default -F __start_kubectl kubecolor
  complete -o default -F __start_kubectl k
  complete -o default -F __start_kubectl wk
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
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

# git completions for rgit
if [[ -x "$(command -v rgit)" ]]; then
  if [ -f /usr/share/bash-completion/completions/git ]; then
    . /usr/share/bash-completion/completions/git
    complete -o bashdefault -o default -o nospace -F __git_wrap__git_main rgit
  fi
fi

# kubectl completions
if [[ -x "$(command -v kubectl)" ]]; then
  . <(kubectl completion bash)
fi

# helm completions
if [[ -x "$(command -v helm)" ]]; then
  . <(helm completion bash)
fi

# avp completions
if [[ -x "$(command -v avp)" ]]; then
  . <(avp completion bash)
fi

# rustup completions
if [[ -x "$(command -v rustup)" ]]; then
  . <(rustup completions bash)
fi

if [[ -x "$(command -v fnm)" ]]; then
  eval "$(fnm env --use-on-cd)"
fi

# Fuzzy search
# source /usr/share/doc/fzf/examples/key-bindings.bash

# thefuck
if [[ -x "$(command -v thefuck)" ]]; then
  eval $(thefuck --alias)
fi

# Starship prompt
if [[ -x "$(command -v starship)" ]]; then
  eval "$(starship init bash)"
fi

if [[ -x "$(command -v zoxide)" ]]; then
  eval "$(zoxide init bash)"
  alias cd="z"
fi

# add local changes to your bash environment in ~/.bash_local
if [ -f ~/.bash_local ]; then
  . ~/.bash_local
fi
