#!/usr/bin/env bash
# .bashrc

# source global definitions
if [ -f /etc/bashrc ]; then
  . /etc/bashrc
fi

# encoding
export LANG=en_US.UTF-8

# editor
export EDITOR=vim
export VISUAL="${EDITOR}"
export FCEDIT="${EDITOR}"

# pager
export LESSHISTFILE="/dev/null"
export LESS='-XR-+I'
export PAGER="less"
export MANPAGER="${PAGER-}"
export GROFF_NO_SGR=1

# history
HISTCONTROL=ignoredups
HISTIGNORE="&:[ ]*:[bf]g:cd:exit:history:ls"
# HISTIGNORE="@(?|??|???)*( |$'\t'):*( |$'\t')"
# HISTFILESIZE=500
unset HISTFILE

# prompt
PROMPT_DIRTRIM=2
PS1='[\u \w]\\$ '

# options
stty -ixon 2>/dev/null
set -o noclobber
# shellcheck disable=SC3044
shopt -s autocd 2>/dev/null

# readline
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"'

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

# editor
# alias v='vim -u DEFAULTS --cmd '\''set lpl rnu nu hid ar ai si scs ic et ts=2 sw=2 bs=2 | nmap Y y$'\'''
alias v='vim -i NONE'
alias vi='vim -i NONE'
alias vimrc='v "${HOME%/}"'\''/.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 d='type'
alias g='git'
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 -i'
alias rm='rm -i'

# shell
alias bashrc='"${EDITOR:-vi}" -- "${HOME%/}"/.bashrc'
alias reload='. "${HOME%/}"'\''/.bashrc'\'''

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

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

cx() { chmod +x "$*"; }

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
}

fd() { find . -iname "*$1*" 2>/dev/null; }

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

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}"
}

sl() {
  printf '%-8s %-17s %-3s %-4s %-4s %-10s %-12s %-s\n' \
    'Inode' 'Permissions' 'ln' 'UID' 'GID' 'Size' 'Time' 'Name'
  command -p -- stat -c "%8i %A (%4a) %3h %4u %4g %10s (%10Y) %n" -- "${@-.}"
}

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

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

command -v tree >/dev/null \
  || tree() {
    exec find -- "${1:-.}" -path '*/.git' -prune -o -path '*/.nc_plugin' -prune -o -path './*' 2>/dev/null | sed -Ee '2,$s:[^/]*/:   :g; s:   ([^ ]+)$:|_ \1:'
  }

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

# shellcheck disable=SC1090
. "/home/${USER}/.acme.sh/acme.sh.env"

# ensure good return code
