# ~/.bashrc: executed by bash(1) for non-login shells.
# Note: PS1 and umask are already set in /etc/profile. You should not
# need this unless you want different defaults for root.
# PS1='${debian_chroot:+($debian_chroot)}\h:\w\$ '
# umask 022

# encoding
export LANG=en_US.UTF-8

# editor
if command -v nvim >/dev/null 2>&1; then
  export EDITOR=nvim
elif command -v vim >/dev/null 2>&1; then
  export EDITOR=vim
else
  export EDITOR=vi
fi
export VISUAL="${EDITOR}"
export FCEDIT="${EDITOR}"

# pager
export PAGER="less"
export LESSHISTFILE='-'
export LESS="-XFR"
export MANPAGER='less'
export GROFF_NO_SGR=1

# history
HISTCONTROL=ignoredups
export HISTIGNORE="&:[ ]*:..:[bf]g:cd:exit:history:l:ls"
# ignore one and two letter commands
# export HISTIGNORE="@(?|??|???)*( |$'\t'):*( |$'\t')"
# do not save command to history file
unset HISTFILE

# options
stty -ixon 2>/dev/null
# only logout if <ctrl-d> is executed two consecutive times
# IGNOREEOF=1
set -o noclobber
# set autocd if it works in a subshell, disregard if unsupported
# shellcheck disable=SC3044
shopt -s autocd 2>/dev/null

# readline
if [ -n "${INPUTRC-}" ] || [ -f "${HOME%/}"'/.config/readline/inputrc' ]; then
  bind -f "${INPUTRC:-${HOME%/}/.config/readline/inputrc}"
else
  bind Space:magic-space
  bind TAB:menu-complete
  bind '"\e[Z": menu-complete-backward'
  bind '"M-TAB": dynamic-complete-history'
  bind '"\e[A": history-search-backward'
  bind '"\e[B": history-search-forward'
  bind '"\C-a": beginning-of-line'
  bind '"\C-b": backward-char'
  bind '"\C-e": end-of-line'
  bind '"\C-f": forward-char'
  bind '"\C-k": kill-line'
  bind '"\C-l": clear-screen'
  bind '"\C-]": dabbrev-expand'
  bind '"\C-xa": alias-expand-line'
  bind '"\C-xd": dump-macros'
  bind '"\C-xe": shell-expand-line'
  bind '"\C-x\C-k": kill-whole-line'
  bind '"\C-x\C-u": undo'
  bind '"\e.": insert-last-argument'
  bind '"\ee": edit-and-execute-command'
  bind '"\en": history-substring-search-forward'
  bind '"\ep": history-substring-search-backward'
  bind '"\eu": undo'
  bind '"\e\C-e": shell-expand-line'
  # macros | convenient shell interaction
  bind '"\C-g": "fg\C-m"'
  bind '"\C-x\/": "; echo $?"'
  bind '"\C-x\`": "\$\(\)\C-b"'
  bind '"\C-xb": "\$\{\}\C-b"'
  bind '"\C-xl": "\C-e | less\C-m"'
  bind '"\e\"": "\e[1;3D\"\C-e\""'
  bind '"\ea": "\C-a\e[3;5~"'
  bind '"\es": "\C-a(set -x ; \C-e)\C-b"'
  bind '"\ev": "\C-e | vi -\C-m"'
  bind '"\eH": " --help"'
  bind '"\eV": " --version"'
  bind '"\eX": "\$\(!! \| tail -1\)\e\C-e"'
  bind '"\eOP": "\C-a\man \C-e\C-m"'
fi

# aliases
# cd
alias -- 1='cd -- "${OLDPWD:--}"' -='cd -- "${OLDPWD:--}"'
alias -- ...='cd -- ../..'
alias -- ....='cd -- ../../..'
alias -- .....='cd -- ../../../..'
alias -- ......='cd -- ../../../../..'
alias -- .......='cd -- ../../../../../..'
alias -- ........='cd -- ../../../../../../..'
alias -- .........='cd -- ../../../../../../../..'

# editor
alias v='"${EDITOR}" -i NONE'
alias vi='"${EDITOR}" -i NONE '
alias vim='"${EDITOR}" -i NONE '
alias vimrc='v "${HOME%/}"'\''/.vim/vimrc'\'''
# alias vimrc='v "${HOME%/}"'\''/.config/vim/vimrc'\'''

# ls
alias ls='ls --color=auto'
alias ll='ls -lAF'
alias la='ls -AF'
alias l='ls -ACF'
alias lg='ls -lAh | grep -i'

# shortcut
alias b='cat'
alias g='git'
alias t='type '
alias x='exit'

# unix
alias cp='cp -i'
alias csort='LC_ALL=C sort'
alias diff='diff --color=auto'
alias grep='grep --color=auto'                                                                     alias -- md >/dev/null 2>&1 &&
  unalias -- md
alias -- md='command -p -- mkdir -p'
alias mv='mv -v -i'
alias rm='rm -v -i'
command -v tree >/dev/null || alias tree='ls -R | grep ":$" | sed -e "s/:$//" -e "s/[^-][^\/]*\//--/g" -e "s/^/ /" -e "s/-/|/"'

# utility
alias cls='clear && printf "\e[3J"'
alias cmdv='command -v'
alias ec='echo'
alias ppath='echo ${PATH} | tr ":" "\n"'

# shell
alias bashrc='v "${HOME%/}"/.bashrc'
alias reload='. "${HOME%/}"'\''/.bashrc'\'''

# network
alias myip='curl -sq "https://ifconfig.co/ip"'

# completion | enable programmable completion features
# shellcheck source=/dev/null
# shellcheck disable=SC3044
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

# functions
bashman() { man bash | less -p "^       $1 "; }

# shellcheck disable=SC2312
cdtmp() { cd "$(mktemp -d)" || return; }

diffy() { command diff --side-by-side --suppress-common-lines --width="$((COLUMNS / 2 - 1 + COLUMNS / 2 - 1))" "${@-}"; }

dua() { command -p -- du --exclude=".git" --exclude=".nc_plugin"  "${@--shx}" | sort -h; }

ecc() { "$*"; echo $?; }

# fd() { command find -L -- . -path '*/.git' -prune -o -path '*/.nc_plugin' -prune -o -path './*' -iname "*${1}*" -type d -print 2>/dev/null; }

# ff() { find . -type f -iname '*'"$*"'*' ; }
ff() { command find -L -- . -path '*/.git' -prune -o -path '*/.nc_plugin' -prune -o -iname '*'"$*"'*' -type f -print 2>/dev/null; }

# ind() { ( cd "${1?no directory given}" && shift && "$@"; ) }
ind() { ( cd -- "${1?no directory given}" && eval "${2:-}"; ) }

# shellcheck disable=SC2012
lt() { count="${1-10}"; ls -Ahltr -F --color=always |tail -"${count}"; }

lw() { find -L "${@:-.}" -maxdepth 1 | wc -l; }

mkcd() { mkdir -p -- "$1" || return; cd -- "$1" || return; }

nh() { nohup "$@" >/dev/null 2>&1 & }

now() { now="$(command -p -- date -- '+%Y%m%d%H%M%S')";  printf '%s' "${now}"; }

touchf() { for f in "$@"; do mkdir -p -- "$(dirname -- "${f}")"; touch -- "${f}"; done }

sr() { grep --color=always "$*" --exclude-dir=".git" --exclude-dir="node_modules" --exclude-dir=".nc_plugin" --ignore-case --recursive . | less --no-init --raw-control-chars; }

# shellcheck disable=SC2312
u() { cd "$(seq "${1:-1}" | sed 's|.*|../|' | tr -d '\n')" || return; }

# ensure good return code
