# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

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

ZSH=$HOME/.oh-my-zsh
ZSH_THEME="powerlevel10k/powerlevel10k"

CASE_SENSITIVE="true"

# Comment this out to disable bi-weekly auto-update checks
# DISABLE_AUTO_UPDATE="true"

# Uncomment to change how often before auto-updates occur? (in days)
export UPDATE_ZSH_DAYS=3

# Uncomment following line if you want to disable colors in ls
# DISABLE_LS_COLORS="true"

# Uncomment following line if you want to disable autosetting terminal title.
DISABLE_AUTO_TITLE="true"

# Uncomment following line if you want to disable command autocorrection
DISABLE_CORRECTION="true"

# Uncomment following line if you want red dots to be displayed while waiting
# for completion
COMPLETION_WAITING_DOTS="true"

# Uncomment following line if you want to disable marking untracked files under
# VCS as dirty. This makes repository status check for large repositories much,
# much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

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/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
  nvm
  pip
  podman
  postgres
  python
  rails
  rake-fast
  rbenv
  redis-cli
  rsync
  ruby
  rust
  symfony-complete
  tmux
  vagrant
  vi-mode
  yarn
)

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

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 PYENV_ROOT="${HOME}/.pyenv"
export SDKMAN_DIR="${HOME}/.sdkman"
export VISUAL=vim
export PTD_CACHE="${HOME}/.local/ptd"
export PTD_ROOT="${HOME}/code/rstudio/ptd/infra"

# 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

if terraform version &>/dev/null; then
  alias terraform=tofu
  alias tf=tofu
fi

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

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

if which phpenv >/dev/null; then
  eval "$(phpenv init - 2>/dev/null)"
fi

if [[ -f ~/.cargo/env ]]; then
  source ~/.cargo/env
fi

if gimme --version &>/dev/null; then
  eval "$(gimme 1.21.4)" 2>/dev/null
  export GOPATH="$HOME/go"
fi

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

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

function 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
}

function ptd-completion() {
  if command -v ptd &>/dev/null; then
    eval "$(_PTD_COMPLETE=zsh_source ptd)"
  fi
}

function 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
}

export NVM_DIR="${HOME}/.nvm"

__maybesource ~/.zshenv
__maybesource ~/google-cloud-sdk/path.zsh.inc
__maybesource ~/google-cloud-sdk/completion.zsh.inc
__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
__maybesource ~/.kiex/scripts/kiex
__maybesource ~/.config/tabtab/__tabtab.zsh
__maybesource ~/.sdkman/bin/sdkman-init.sh

if [[ -f ~/perl5/lib/perl5/local/lib.pm ]]; then
  eval "$(perl -I ~/perl5/lib/perl5 '-Mlocal::lib')"
fi

if [[ -d /usr/local/opt/python@3.8/lib ]]; then
  export LDFLAGS="-L/usr/local/opt/python@3.8/lib"
fi

if [[ -d /usr/local/opt/python@3.8/lib/pkgconfig ]]; then
  export PKG_CONFIG_PATH="/usr/local/opt/python@3.8/lib/pkgconfig"
fi

if nvm &>/dev/null; then
  nvm use --delete-prefix 20 --silent
fi

autoload -Uz compinit
compinit

if kitty --version &>/dev/null; then
  kitty + complete setup zsh | source /dev/stdin
fi

compdef _symfony_complete composer
compdef _symfony_complete dep

alias fz=fuzzbucket-client

if ptd --version &>/dev/null; then
  ptd-completion
fi

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

# pnpm
export PNPM_HOME="/home/me/.local/share/pnpm"
case ":$PATH:" in
  *":$PNPM_HOME/bin:"*) ;;
  *) export PATH="$PNPM_HOME/bin:$PATH" ;;
esac
# pnpm end
