#!/usr/bin/env bash
export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d"
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"

# this nees to run after bash-completion as __git functions are defined in there
if [[ $(type -t __git_ps1) == "function" ]] ; then 
  PS1='\[\033[32m\]$(__git_ps1 "(%s)")\[\033[00m\]\w \\$ '
else
  PS1='\w \\$ '  
fi

# homebrew
export HOMEBREW_AUTO_UPDATE_SECS=86400
export HOMEBREW_NO_ANALYTICS=1
export HOMEBREW_NO_GITHUB_API=1
export HOMEBREW_DISPLAY_INSTALL_TIMES=1

# hstr
export HSTR_CONFIG=hicolor,raw-history-view # get more colors

# history
shopt -s histappend              # append new history items to .bash_history
export HISTCONTROL=ignoredups:erasedups  # no duplicate entries
export CLICOLOR=YES
export HISTFILESIZE=2500000
export HISTSIZE=250000

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

export EDITOR=vim

# ensure synchronization between Bash memory and history file
# export PROMPT_COMMAND="history -a; history -n; ${PROMPT_COMMAND}"
# see: https://github.com/dvorka/hstr/issues/277
export PROMPT_COMMAND=${PROMPT_COMMAND:+$PROMPT_COMMAND; }'history -a; history -n'
# export PROMPT_COMMAND='history -a; history -c; history -r;'"${PROMPT_COMMAND:+$PROMPT_COMMAND; }" # laut c't 20/2020

# if this is interactive shell, then bind hstr to Ctrl-r (for Vi mode check doc)
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

# aliases and functions
alias ll='ls -kahlFG'
alias la='ls -CAF'
alias l='ls -CF'
alias cd..='cd ..'
alias mkdir='mkdir -pv'
alias bashrc="vim ~/.bashrc && source ~/.bashrc"
alias git-root='cd "$(git rev-parse --show-toplevel)"'
alias pcat='pygmentize -f terminal256 -O style=tango -g'
alias tcat='tree-sitter highlight'

qst() { xsv stats "$@" | gawk -v fmt="%.4g" -f ~/printf_g.awk | xsv table; }

dcleanup(){
    docker rm -v "$(docker ps --filter status=exited -q 2>/dev/null)" 2>/dev/null
    docker rmi "$(docker images --filter dangling=true -q 2>/dev/null)" 2>/dev/null
}

append_path() {
  for chkpth ; do
    if [[ ! $PATH = *"$chkpth"* && -d "$chkpth" ]] ; then 
      PATH="$PATH":"$chkpth"
    fi
  done
}

prepend_path() {
  for chkpth ; do
    if [[ ! $PATH = *"$chkpth"* && -d "$chkpth" ]] ; then 
      PATH="$chkpth":"$PATH"
    fi
  done
}

export MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"

export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"
export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"

export BAT_THEME="OneHalfLight"

export INPUTRC=~/.inputr

[[ -r "$HOME"/.gem/ruby ]] && gems=$(find "$HOME"/.gem/ruby -maxdepth 2 -type d -name bin);

prepend_path "/usr/local/bin" "$HOME/.cargo/bin" "/usr/local/opt/ruby/bin" "$gems" "$HOME/go/bin" "$HOME/bin"

append_path "/usr/local/texlive/2018/bin/x86_64-darwin/"

[[ -r "$HOME/.cargo/env" ]] && . "$HOME/.cargo/env"

ramdisk(){
  if [[ ! -d /Volumes/ramdisk ]]; then
    local default=${1:-24} # 24 GB default
    local size=$((1024*1024*2*default)) # a sector is 512 bytes, default in GB
    set -x
    diskutil erasevolume HFS+ "ramdisk" $(hdiutil attach -nomount ram://$size) # do not quote $(hdutil…) as it contains unwanted white-space; good work Apple
    if [[ -d /Volumes/ramdisk/ ]]; then
      rm -rfv /Volumes/ramdisk/.{,_.}{fseventsd,Spotlight-V*,Trashes}
      mkdir -v /Volumes/ramdisk/.fseventsd
      touch /Volumes/ramdisk/.fseventsd/no_log /Volumes/ramdisk/.metadata_never_index /Volumes/ramdisk/.Trashes
      mdutil -i off /Volumes/ramdisk
      echo -e "\n\tcd /Volumes/ramdisk/\n"
      cd /Volumes/ramdisk/ || exit
    fi
    set +x
  else
    cd /Volumes/ramdisk/ || exit
  fi
}

if [[ -f ~/.config/broot/launcher/bash/br ]]; then
  . ~/.config/broot/launcher/bash/br
fi

export PATH
