source /etc/profile
autoload -U compinit promptinit
compinit
promptinit; prompt gentoo
setopt correct
setopt share_history
setopt histreduceblanks
setopt INC_APPEND_HISTORY
setopt NO_HIST_BEEP
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_EXPIRE_DUPS_FIRST
setopt HIST_SAVE_NO_DUPS
setopt HIST_FIND_NO_DUPS
# Use the same history file for all sessions
setopt SHARE_HISTORY
# Let the user edit the command line after history expansion (e.g. !ls) instead of immediately running it
setopt hist_verify

setopt extended_glob
#setopt noequals
setopt HASH_CMDS

HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000

eval $(dircolors -b /etc/DIR_COLORS)

alias mv='nocorrect mv'
alias cp='nocorrect cp'
alias mkdir='nocorrect mkdir'
alias rm='nocorrect rm'

alias ls="ls --color -F"
alias ll='ls -l --color -F'
alias grep='grep --color=always'
alias locate="noglob locate"
alias cvsstatus="cvs -qn update"
alias cvs-status="cvs -qn update"

#bindkey "\e[A" history-beginning-search-backward
#bindkey "\e[B" history-beginning-search-forward
#bindkey "\eOA" history-beginning-search-backward
#bindkey "\eOB" history-beginning-search-forward
#bindkey "\e[1~" beginning-of-line
#bindkey "\e[2~" quoted-insert
bindkey "\e[3~" delete-char
#bindkey "\e[4~" end-of-line
#bindkey "\e[5~" beginning-of-history
#bindkey "\e[6~" end-of-history
#bindkey "\e[7~" beginning-of-line
#bindkey "\e[8~" end-of-line
bindkey "\e[H" beginning-of-line
bindkey "\e[F" end-of-line
bindkey "\eOH" beginning-of-line
bindkey "\eOF" end-of-line
#bindkey "\eOd" backward-word
#bindkey "\eOc" forward-word 

if [ "${USER}" != "root" ]; then
  if [ ! -r ~/.ssh/id_ed25519_sk_green ] && [ ! -r ~/.ssh/id_ed25519_sk_red ] && [ ! -r ~/.ssh/gentoo_rsa ]; then
    printf "This system has no ssh keys, you are expected to forward ssh agent\n"
  else
    if [ ! -r ~/.ssh/id_ed25519_sk_green ]; then
      printf "This system is missing your yk5cgreen primary key id_ed25519_sk_green\n"
    fi
    if [ ! -r ~/.ssh/id_ed25519_sk_red ]; then
      printf "This system is missing your yk5cred backup key id_ed25519_sk_red\n"
    fi
    if [ ! -r ~/.ssh/gentoo_rsa ]; then
      printf "This system is missing your old key gentoo_rsa\n"
    fi

    eval $(keychain --eval --ignore-missing ~/.ssh/id_ed25519_sk_green ~/.ssh/id_ed25519_sk_red ~/.ssh/gentoo_rsa)
    GPG_TTY=$(tty)
  fi
fi

setopt prompt_subst
autoload -Uz vcs_info
zstyle ':vcs_info:*' actionformats \
    '%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{3}|%F{1}%a%F{5}]%f '
zstyle ':vcs_info:*' formats       \
    '%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{5}]%f '
zstyle ':vcs_info:(sv[nk]|bzr):*' branchformat '%b%F{1}:%F{3}%r'

zstyle ':vcs_info:*' enable git cvs svn

# or use pre_cmd, see man zshcontrib
vcs_info_wrapper() {
  vcs_info
  if [ -n "$vcs_info_msg_0_" ]; then
    echo "%{$fg[grey]%}${vcs_info_msg_0_}%{$reset_color%}$del"
  fi
}
RPROMPT=$'$(vcs_info_wrapper)%(?.. %? %F{red}%B⨯%b%F{reset})%(1j. %j %F{yellow}%B⚙%b%F{reset}.)'

#vidpid helpers because I love it
vidpid_to(){
  v=${1%:*}; p=${1#*:}  # split vid:pid into 2 vars
  if [ -z "${1:-}" ]; then
    printf 'vid_pid_to requires vid:pid as param 1\n'
    return 1
  fi
  # we want this to be split
  # shellcheck disable=2046
  find $(grep --color=never -l "PRODUCT=$(printf '%x/%x' "0x${v}" "0x${p}")" /sys/bus/usb/devices/[0-9]*:*/uevent | sed 's,uevent$,,') \
    /dev/null -name dev -o -name dev_id |
    sed 's,[^/]*$,uevent,'
}
vidpid_to_dev(){
    vidpid_to "${1}" | xargs sed -n -e s,DEVNAME=,/dev/,p
}
vidpid_to_net(){
    vidpid_to "${1}" | xargs sed -n -e s,INTERFACE=,,p
}
vidpid_to_all(){
    vidpid_to "${1}" | xargs sed -n -e s,DEVNAME=,/dev/,p -e s,INTERFACE=,,p
}
