[[ "${ZSH_DEBUG}" ]] && set -o xtrace

CASE_SENSITIVE="true"
COMPLETION_WAITING_DOTS="true"
DISABLE_AUTO_TITLE="true"
DISABLE_AUTO_UPDATE="true"
DISABLE_CORRECTION="true"
DISABLE_UNTRACKED_FILES_DIRTY="true"
ZSH="${HOME}/.oh-my-zsh"

export ZSH_THEME=''

if [[ -e "${XDG_RUNTIME_DIR}/ssh-agent.socket" ]]; then
  export SSH_AUTH_SOCK="${XDG_RUNTIME_DIR}/ssh-agent.socket"
fi

path=(
  $HOME/.rbenv/shims
  /home/linuxbrew/.linuxbrew/bin
  $HOME/bin
  $HOME/go/bin
  $HOME/.local/bin
  $HOME/.cargo/bin
  $HOME/.rbenv/bin
  $HOME/.phpenv/bin
  $HOME/.yarn/bin
  $HOME/.pulumi/bin
  ${KREW_ROOT:-$HOME/.krew}/bin
  $HOME/.local/bin
  $HOME/.nix-profile/bin
  $HOME/perl5/bin
  $HOME/Library/Python/3.8/bin
  /usr/local/opt/python@3.8/bin
  /usr/bin/core_perl
  /usr/local/sbin
  /usr/local/bin
  /usr/bin/vendor_perl
  /usr/sbin
  /usr/bin
  /sbin
  /bin
  /snap/bin
  /usr/games
  /usr/local/games
  /usr/local/texlive/2018/bin/x86_64-darwin
)

plugins=(
  archlinux
  asdf
  aws
  azure
  brew
  bundler
  command-not-found
  cp
  direnv
  docker
  gem
  git
  golang
  helm
  heroku
  httpie
  kubectl
  mercurial
  minikube
  nmap
  node
  pip
  podman
  postgres
  python
  rails
  rake-fast
  redis-cli
  rsync
  ruby
  rust
  symfony-complete
  tmux
  vagrant
)

if [[ -d ~/.oh-my-zsh/plugins/just/ ]]; then
  plugins+=just
fi

fpath=(/usr/local/share/zsh/site-functions $fpath)
fpath=("${HOMEBREW_PREFIX}/share/zsh/site-functions" $fpath)

source $ZSH/oh-my-zsh.sh

unalias gb &>/dev/null

export EDITOR=vim
export GOPATH="${HOME}/go"
export NVM_DIR="${HOME}/.nvm"
export PYENV_ROOT="${HOME}/.pyenv"
export SDKMAN_DIR="${HOME}/.sdkman"
export VISUAL=vim

GPG_TTY="$(tty)"
export GPG_TTY

# The completion for aws is not compatible with $fpath :scream_cat:
if [[ -f /usr/local/share/zsh/site-functions/_aws ]]; then
  source /usr/local/share/zsh/site-functions/_aws
fi

if [[ -f ~/.local/bin/aws_zsh_completer.sh ]]; then
  source ~/.local/bin/aws_zsh_completer.sh
fi

if command -v xdg-open &>/dev/null; then
  alias open=xdg-open
fi

if command -v setxkbmap &>/dev/null; then
  alias kbus='setxkbmap -layout us'
  alias kbdv='setxkbmap -layout dvorak'
fi

if R --version &>/dev/null; then
  alias R='R --no-save --no-restore-data'
fi

if command -v ssh-add &>/dev/null; then
  alias ssh-add='ssh-add -t 1h'
fi

if psql --version &>/dev/null; then
  alias pgme='psql -d postgres://me@jool.meatballhat.com:15744/me'
fi

unsetopt correct_all
bindkey '^R' history-incremental-search-backward

__maybesource() {
  if [[ -f "${1}" ]]; then
    if [[ "${2}" == silent ]]; then
      source "${1}" &>/dev/null
    else
      source "${1}"
    fi
  fi
}

brew() {
  unset -f brew
  if [[ -x /home/linuxbrew/.linuxbrew/bin/brew ]]; then
    eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
  fi
  brew "${@}"
}

nvm() {
  unset -f nvm
  __maybesource /usr/share/nvm/init-nvm.sh silent
  __maybesource /usr/local/opt/nvm/nvm.sh silent
  __maybesource "${NVM_DIR}/nvm.sh"
  __maybesource /home/linuxbrew/.linuxbrew/opt/nvm/nvm.sh
  nvm "${@}"
}

pyenv() {
  unset -f pyenv
  if which pyenv >/dev/null; then
    if [[ "${HATCH_ENV_ACTIVE}" ]]; then
      : skipping pyenv when inside a hatch env
    else
      path=($HOME/.pyenv/shims $HOME/.pyenv/bin "${path[@]}")
      plugins=("${plugins[@]}" pyenv)
      eval "$(pyenv init - 2>/dev/null)"
    fi
  fi
  pyenv "${@}"
}

phpenv() {
  unset -f phpenv
  if which phpenv >/dev/null; then
    eval "$(phpenv init - 2>/dev/null)"
  fi
  phpenv "${@}"
}

pnpm() {
  unset -f pnpm
  export PNPM_HOME="/home/me/.local/share/pnpm"
  case ":$PATH:" in
  *":$PNPM_HOME/bin:"*) ;;
  *) export PATH="$PNPM_HOME/bin:$PATH" ;;
  esac
  pnpm "${@}"
}

docker-cleanup() {
  local once
  if [[ "${1}" == once ]]; then
    once=1
  fi

  while true; do
    date -u
    echo ----
    docker images --filter='dangling=true' -q |
      xargs docker rmi
    if [[ "${once}" == 1 ]]; then
      return
    fi
    for i in {0..9}; do
      echo -n z
      sleep 1
    done
    echo
  done
}

mksecret() {
  local nbytes="${1:-42}"

  python3 <<EOPYTHON | tr -d '/' | tr -d '+' | cut -b1-$((nbytes - 1))
import secrets
import base64

print(base64.b64encode(secrets.token_bytes(${nbytes} * 2)).decode(), end="")
EOPYTHON
}

cdpath=(
  $HOME/code
  $HOME/src
)

__maybesource ~/.cargo/env
__maybesource ~/.zshenv
__maybesource ~/google-cloud-sdk/path.zsh.inc
__maybesource ~/google-cloud-sdk/completion.zsh.inc
__maybesource ~/.kiex/scripts/kiex
__maybesource ~/.config/tabtab/__tabtab.zsh
__maybesource ~/.sdkman/bin/sdkman-init.sh

autoload -Uz compinit
if [[ -n ~/.zcompdump(#qNmh-24) ]]; then
  compinit -C
else
  compinit
fi

autoload -U promptinit
promptinit
zstyle :prompt:pure:path color '#FFB86C'
zstyle :prompt:pure:prompt color '#50FA7B'
zstyle :prompt:pure:git:branch color '#BD93F9'
prompt pure

compdef _symfony_complete composer
compdef _symfony_complete dep
