# OS specifics

OS=$(uname -s | tr '[:upper:]' '[:lower:]')
export OS

# shellcheck disable=SC2312
if [ "$(uname -s)" = "NetBSD" ]; then
  PREFIX=/usr/pkg
  is_netbsd=yes
else
  PREFIX=/usr/local

  # [ "$(uname -s)" = "Linux" ] && is_linux=yes
fi

# [ "$(uname -s)" = "Linux" ] && is_linux=yes

EDITOR=vim
VISUAL=${EDITOR}
CVSEDITOR=${EDITOR}
PKGEDITOR=${EDITOR}
PAGER=less
ENV=${HOME}/.shrc

export EDITOR VISUAL CVSEDITOR PKGEDITOR PAGER ENV GOPATH

# [ -z "${ZSH}" ] \
#   && PS1="(\u@\[\e[1;32m\]\h\[\e[0m\]):[\w]\n$ " \
#     HISTTIMEFORMAT="$(echo -e '\r\e[K')"

LANG="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_COLLATE="C"
LC_TIME="C"
LC_NUMERIC="C"
LC_MONETARY="C"
LC_ALL=""
export LANG LC_CTYPE LC_MESSAGES
export LC_COLLATE LC_TIME LC_NUMERIC LC_MONETARY LC_ALL

# allow ^Q for screen / tmux
stty -ixon

# [ "${OS}" = "linux" ] && lscolors="--color"

# alias ls="ls -F ${lscolors}"
alias nocom="egrep -v '^[[:space:]]*(#|$)'"
alias en2es="trans --brief -to es"

alias myip="curl -o- -s imil.net/if" # use at your own risk
alias aws_enable=". ~/ve/aws/bin/activate"
alias xc="xclip -selection clipboard"

# Do not reload path is already set by us
# [[ "${PATH}" = *"/path_loaded"* ]] \
#   || PATH=${PATH}:${PREFIX}/sbin:${PREFIX}/bin:/sbin:/usr/sbin:/usr/X11R7/bin:${HOME}/bin:${HOME}/node_modules/.bin:${HOME}/pkg/bin:${HOME}/ssh:${GOPATH}/bin:/path_loaded
# export PATH

# Set colors for less. Borrowed from https://wiki.archlinux.org/index.php/Color_output_in_console#less .
# export LESS_TERMCAP_mb=$'\E[1;31m'     # begin bold
# export LESS_TERMCAP_md=$'\E[1;36m'     # begin blink
# export LESS_TERMCAP_me=$'\E[0m'        # reset bold/blink
# export LESS_TERMCAP_so=$'\E[01;44;33m' # begin reverse video
# export LESS_TERMCAP_se=$'\E[0m'        # reset reverse video
# export LESS_TERMCAP_us=$'\E[1;32m'     # begin underline
# export LESS_TERMCAP_ue=$'\E[0m'        # reset underline

export LESS='--quit-if-one-screen --ignore-case --status-column --LONG-PROMPT --RAW-CONTROL-CHARS --HILITE-UNREAD --tabs=4 --no-init --window=-4'

# if [ -d ~/.fzf ]; then
#   if [ -z "$ZSH" ]; then
#     . ~/.fzf/completion.bash
#     . ~/.fzf/key-bindings.bash
#   else
#     . ~/.fzf/completion.zsh
#     . ~/.fzf/key-bindings.zsh
#   fi
# fi

# load specific profile defs
[ -f "${HOME}/.local_profile" ] && . "${HOME}"/.local_profile

# helper functions

sslconnect() {
  openssl s_client -connect "$1:$2" -crlf
}

ssh_copy_id() {
  [ -z "${is_netbsd}" ] && ssh-copy-id "$@" && return

  [ -f ~/.ssh/id_rsa.pub ] && PK=id_rsa || PK=id_dsa

  cat ~/.ssh/"${PK}.pub" \
    | ssh "$1" "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
}

dos2unix() {
  mv -f "$1" "$1.dos"
  tr -d '\r' <"$1.dos" >"$1"
}

isomount() {
  [ -z "$1" ] || [ -z "$2" ] \
    && echo "usage: isomount <iso> <dir>" && return

  [ -z "${is_netbsd}" ] && sudo mount -o loop "$1" "$2" && return

  sudo vnconfig -v -c /dev/vnd0d "$1"
  sudo mount -t cd9660 /dev/vnd0a "$2"
}

isoumount() {
  [ -z "$1" ] && echo "usage: isoumount <dir>" && return

  sudo umount "$1"

  [ -n "${is_netbsd}" ] && sudo vnconfig -u vnd0d
}

rmknownhost() {
  [ "${OS}" != "linux" ] && SED="gsed" || SED="sed"
  [ -z "$1" ] && echo "usage: $0 /path/to/known_hosts:line" && return
  ${SED} -i "${1#*:}d" "${1%:*}"
}

sslexpire() {
  echo | openssl s_client -showcerts -connect "${1}:443" 2>/dev/null \
    | openssl x509 -noout -dates
}

# killhup() {
#   kill -HUP $(cat /var/run/${1}.pid)
# }

sshvia() {
  ssh -o ProxyCommand="ssh -A $1 -W $2:${3:-22}" "$2"
}

chktls() {
  openssl s_client -showcerts -connect "${1}:443"
}

# convert big endian to little endian
be2le() {
  echo "$1" | rev | dd conv=swab 2>/dev/null
}
be2sc() {
  be2le "$1" | sed -E 's/../&\\x/g;s/^(.+)\\x$/\\x\1/'
}

strlen() {
  str=$(cat "$1")
  echo ${#str}
}

bin2sc() {
  hexdump -v -e '"\\""x" 1/1 "%02x" ""' "${1}"
}

mdread() {
  pandoc "$1" -f markdown -t html | lynx -stdin
}
