[ -t 0 ] || return
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 

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
}

if [ "${USER}" != "root" ]; then
  card="unknown"
  if [ ! -r ~/.ssh/id_ed25519_sk_green ] && [ ! -r ~/.ssh/id_ed25519_sk_rk_yk5cgreen ]; then
    if lsusb -d 1050:0407 > /dev/null 2>&1; then
      if [ -x "$(command -v ykinfo)" ] && [ "$(ykinfo -s -q)" = "20489729" ]; then
        card="green"
        if [ ! -d "${HOME}/.ssh" ]; then
          mkdir -p "${HOME}/.ssh"
        fi
        cd "${HOME}/.ssh"
        ssh-keygen -K
        cd "${OLDPWD}"
      fi
    fi
    if [ "${card}" != "green" ]; then
      printf "This system is missing your yk5cgreen primary key file\n"
    fi
  fi
  if [ ! -r ~/.ssh/id_ed25519_sk_red ] && [ ! -r ~/.ssh/id_ed25519_sk_rk_yk5cred ]; then
    if lsusb -d 1050:0407 > /dev/null 2>&1; then
      if [ "${card}" = "unknown" ] && [ -x "$(command -v ykinfo)" ] && [ "$(ykinfo -s -q)" = "20489728" ]; then
        card="red"
        if [ ! -d "${HOME}/.ssh" ]; then
          mkdir -p "${HOME}/.ssh"
        fi
        cd "${HOME}/.ssh"
        ssh-keygen -K
        cd "${OLDPWD}"
      fi
    fi
    if [  "${card}" != "red" ]; then
      printf "This system is missing your yk5cred backup key file\n"
    fi
  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_rk_yk5cgreen ~/.ssh/id_ed25519_sk_red ~/.ssh/id_ed25519_sk_rk_yk5cred ~/.ssh/gentoo_rsa)

  # https://github.com/drduh/YubiKey-Guide?tab=readme-ov-file#notes
  ## This doesn't work but it should?
  # running "scd serialno" "learn --force" once on a new system finds the first card, but running again doesn't successfully change the card
  if [ "${card}" = "green" ] && ! gpg-connect-agent 'keyinfo --list' /bye | grep -q 'D2760001240100000006204897290000'; then
    gpg-connect-agent killagent /bye
    gpg-connect-agent "scd serialno" "learn --force" /bye
  elif [ "${card}" = "red" ] && ! gpg-connect-agent 'keyinfo --list' /bye | grep -q 'D2760001240100000006204897280000'; then
    gpg-connect-agent killagent /bye
    gpg-connect-agent "scd serialno" "learn --force" /bye
  fi
  #gpg --card-edit
  #fetch
  #quit
  #gpg --edit-key A5DD1427DD11F94A
  #trust
  #quit
  GPG_TTY=$(tty)
fi
