
# aliases are stored in ~/.aliasrc 
if [ -f ~/.aliasrc ]; then
  source ~/.aliasrc
fi

fpath+=~/.local/share/zsh/functions

typeset -U path
path+=(~/bin)
path+=(~/.local/bin)
path+=($GOPATH/bin)
export PATH

# allow for # to be interpreted as a comment on the command line
setopt interactivecomments

setopt auto_cd # there is no binary by that name in your $PATH, your shell will cd into it that directory

# use vi-style keymap
bindkey -v

if [[ "${TERM}" == xterm* ]]; then
  TERM=xterm-256color
fi

# setup my personal functions
autoload -Uz ~/.local/share/zsh/functions/*(.:t)

#
# Prompt setup
#

# populate colors arrays
autoload -U colors && colors

# git branch prompt
autoload -Uz vcs_info
setopt prompt_subst

precmd_vcs_info() { vcs_info }
precmd_functions+=( precmd_vcs_info )

# %% escapes '%'
# %S == "standout" => reverses fg/bg
# Surround color codes with '%{' and '%}'
# %r == repo name
# %b == branch name
# %S == relative directory
zstyle ':vcs_info:git*' formats '%r (%b):%S'
zstyle ':vcs_info:git*' actionformats '%%S%r%%s:%{$fg[red]%}%b:%a%{$reset_color%} %S'

function set_prompt() {

    if [[ -n "$AWS_PROFILE" ]]; then
      aws_prompt="
%S%B$AWS_PROFILE%b%s
"
    else
      aws_prompt=""
    fi
    if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
        PROMPT=$aws_prompt"%B${vcs_info_msg_0_} ▶%b "
    else
        # PROMPT=$aws_prompt"%B%m:%~ ▶%b "
        PROMPT=$aws_prompt"%B%m:%2~ ▶%b "
    fi
}
precmd_functions+=( set_prompt )

#
# Configure SSH auth sock
#

#if [ -n "$SSH_AUTH_SOCK" ]; then
#  if [ "$SSH_AUTH_SOCK" != $HOME/.ssh/ssh_auth_sock ]; then
#    ln -sf $SSH_AUTH_SOCK $HOME/.ssh/ssh_auth_sock
#    export SSH_AUTH_SOCK=$HOME/.ssh/ssh_auth_sock
#  fi
#fi

# setup SSH keys
if [ -e /usr/bin/keychain ]; then
  eval $(/usr/bin/keychain --eval -Q --quiet id_ed25519)
fi

# ctrl-x opens up the command line in $EDITOR
autoload -U edit-command-line
zle -N edit-command-line
bindkey "^X" edit-command-line

# direnv setup
whence -p direnv &>/dev/null && eval "$(direnv hook zsh)"

# secrets
if [ -r "$HOME/.env_secrets" ]; then
  set -a
  source "$HOME/.env_secrets"
  set +a
fi

# command line completion
autoload -U +X bashcompinit && bashcompinit
autoload -Uz compinit && compinit -C

# gcloud
if [ -f "$HOME/google-cloud-sdk/path.zsh.inc" ]; then . "$HOME/google-cloud-sdk/path.zsh.inc"; fi
if [ -f "$HOME/google-cloud-sdk/completion.zsh.inc" ]; then . "$HOME/google-cloud-sdk/completion.zsh.inc"; fi

# kubectl
[[ $commands[kubectl] ]] && source <(kubectl completion zsh)

# AWS CLI completion
if command -v aws_completer > /dev/null; then
  complete -C aws_completer aws
fi

if command -v tofu > /dev/null; then
  complete -o nospace -C $(command -v tofu) tofu
fi

# Highlight the current autocomplete option
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"

# command line completion for ssh
#h=()
#if [[ -r ~/.ssh/config ]]; then
  h=($h ${${${(@M)${(f)"$(cat ~/.ssh/config)"}:#Host *}#Host }:#*[*?]*})
#fi
#if [[ -r ~/.ssh/known_hosts ]]; then
#  h=($h ${${${(f)"$(cat ~/.ssh/known_hosts{,2} || true)"}%%\ *}%%,*}) 2>/dev/null
#fi
#if [[ $#h -gt 0 ]]; then
#  zstyle ':completion:*:(ssh|scp|slogin|sftp):*' hosts $h
#fi

zstyle :compinstall filename '~/.zshrc'
