#######################################################
# GENERAL
#######################################################

setopt HIST_VERIFY           # Confirm !! and similar before executing
autoload -Uz compinit
compinit

export PATH="$PATH:$HOME/.local/bin"

fpath=(~/.zsh/completion $fpath)
autoload -Uz compinit
compinit

export EDITOR="nvim"

HISTFILE=$ZDOTDIR/.zsh_history
HISTSIZE=10000
SAVEHIST=10000

# Options to make history nicer
setopt HIST_IGNORE_DUPS HIST_IGNORE_SPACE


#######################################################
# PROMPT
#######################################################

setopt prompt_subst                     # allow ${...} inside PROMPT to expand
autoload -Uz vcs_info
precmd() { vcs_info }                   # refresh git info before each prompt

# vcs_info (git branch in parentheses)
zstyle ':vcs_info:*' enable git
zstyle ':vcs_info:git:*' formats ' (%b)'

# optional: hide the default end-of-line % sign on errors
PROMPT_EOL_MARK=''

PROMPT='%F{cyan}[%n@%m %F{yellow}%~%f%F{green}${vcs_info_msg_0_}%f%F{cyan}]%f
%# '

# Remove right side prompt (like time)
unset RPROMPT

#######################################################
# GOPASS
#######################################################

# Gopass aliases
alias gpi='gopass insert --multiline'
alias gpl='gopass ls'
alias gps='gopass show'
alias gpsclip='gopass show --clip'
alias gpe='gopass edit'

#######################################################
# ALIAS
#######################################################

alias dot='cd ~/code/opalfiles'
alias ans='cd ~/code/ansible'

alias v='nvim'
alias vim='nvim'
alias vi='nvim'

# DNF
alias din='sudo dnf install'
alias dre='sudo dnf remove'
alias dup='sudo dnf update'
alias dse='dnf search'

# alias to show the date
alias da='date "+%Y-%m-%d %A %T %Z"'

# Alias's to modified commands
alias cp='cp -i'
alias mv='mv -i'
alias mkdir='mkdir -p'
alias ps='ps auxf'
alias ping='ping -c 10'
alias less='less -R'

# cd into the old directory
alias bd='cd "$OLDPWD"'

# Search command line history
alias h="history | grep "

# Search files in the current folder
alias f="find . | grep "

# Alias's to show disk space and space used in a folder
alias diskspace="du -S | sort -n -r |more"
alias folders='du -h --max-depth=1'
alias folderssort='find . -maxdepth 1 -type d -print0 | xargs -0 du -sk | sort -rn'
alias tree='tree -CAhF --dirsfirst'
alias treed='tree -CAFd'
alias mountedinfo='df -hT'

# Alias's for archives
alias mktar='tar -cvf'
alias mkbz2='tar -cvjf'
alias mgz='tar -cvzf'
alias untar='tar -xvf'
alias unbz2='tar -xvjf'
alias ungz='tar -xvzf'

# Wget (keeps hsts files out of $HOME)
alias wget='wget --hsts-file="$XDG_CACHE_HOME/wget-hsts"'

# Translate
alias tbr='trans :pt-BR'

# ncmpcpp
alias ncmpcpp='ncmpcpp -b ~/.config/ncmpcpp/bindings'

# ls
alias ll='ls -l --color=auto'
alias la='ls -la --color=auto'

# git
alias gs='git status'
alias ga='git add'
alias gpull='git pull'
alias gcom='git commit -m'

alias h='history 1'

alias pkg='nvim ~/Code/arch-ansible/roles/packages/tasks/main.yml'
alias pkg-aur='nvim ~/Code/arch-ansible/scripts/aur-pkg-install.sh'

#######################################################
# FUNCTIONS
#######################################################

# Reset all USB devices after suspend/hibernate if they vanish
usbreset() {
  echo "Re-authorizing all USB devices..."
  for d in /sys/bus/usb/devices/*/authorized; do
    if [[ -w $d ]]; then
      echo 0 | sudo tee "$d" >/dev/null
      echo 1 | sudo tee "$d" >/dev/null
    fi
  done
  echo "Done. Check with lsusb."
}

#######################################################
# KEYBINDS
#######################################################

autoload -U select-word-style
select-word-style bash
bindkey '^[[1;3D' backward-word   # Alt+Left
bindkey '^[[1;3C' forward-word    # Alt+Right

#######################################################
# WORK
#######################################################

# Wireguard
alias wgup='nmcli connection up wg0'
alias wgdown='nmcli connection down wg0'

keys()
{
  eval "$(keychain --quiet --absolute --dir ~/.config/keychain --eval ry_ecdsa)"
  eval "$(keychain --quiet --absolute --dir ~/.config/keychain --eval id_rsa)"
}

keys


apply() {
  (cd ~/Code/arch-ansible && ansible-playbook -i inventory/hosts.yml site.yml -K "$@")
}

apply-aur() {
  (cd ~/Code/arch-ansible/scripts && bash aur-pkg-install.sh)
}

shred-dir() {
  local dir="${1:?Usage: shred-dir <directory>}"
  local passes="${2:-7}"

  [[ ! -d "$dir" ]] && { echo "Not a directory: '$dir'"; return 1; }

  echo "Shred '$dir' with $passes passes? [y/N]"
  read -r confirm
  [[ "$confirm" != [yY] ]] && { echo "Aborted."; return 1; }

  find "$dir" -type f -exec shred -uz -n "$passes" {} \;
  rm -rf "$dir"
  echo "Done."
}
