#
# Editor
#
export EDITOR="vim"

#
# Pager
#

if (( $+commands[vim] )) && [[ -f ~/.vim/bin/vimpager ]]; then
    export PAGER=~/.vim/bin/vimpager
    alias less="$PAGER"
    alias more="$PAGER"
elif (( $+commands[less] )); then
    export PAGER=less
    alias more="$PAGER"
elif (( $+commands[more] )); then
    export PAGER=more
fi
export GIT_PAGER="$PAGER"

#
# Common aliases / functions
#

alias l='ls -lsh'

alias rm='rm -i'
alias mv='mv -i'
alias cp='cp -i'

alias vlock='clear;vlock'
alias q='clear;exit'

export GREP_OPTIONS="--color=auto"
alias mgrep="grep -rnisH"

alias rsu="sudo -E zsh"
alias reloadsh="exec zsh"

alias tmux="tmux -2"

alias md="mkdir -p"
mcd() { mkdir -p "$@" && cd "$@" }

ffi() {
    find -iname "$@"
}

#
# Log viewers aliases
#
if (( $+commands[ccze] )); then
    alias alog="tail -n 300 -F /var/log/auth.log | ccze"
    alias mlog="tail -n 300 -F /var/log/mail.log | ccze"
    alias slog="tail -n 300 -F /var/log/syslog | ccze"
    alias xlog="tail -n 300 -F /var/log/Xorg.0.log | ccze"
else
    alias alog="tail -n 300 -F /var/log/auth.log"
    alias mlog="tail -n 300 -F /var/log/mail.log"
    alias slog="tail -n 300 -F /var/log/syslog"
    alias xlog="tail -n 300 -F /var/log/Xorg.0.log"
fi
# using colortail if available (freebsd)
if (( $+commands[colortail] )); then
    alias slog="colortail -n 60 -k /usr/local/share/examples/colortail/conf.messages -f /var/log/syslog"
fi

#
# System tools aliases
#
alias df='df -h'
if (( $+commands[pydf] )); then
    alias df="pydf -h"
fi

function mount() {
    if [ $# = 0 ]; then
        command mount -lv | column -t
    else
        command mount $@
    fi
}

#
# Sysadmin stuff
#
if (( $+commands[dig] )); then
    alias mx="dig +short MX"
    alias ns="dig +short NS"
elif (( $+commands[host] )); then
    alias mx="host -t MX"
    alias ns="host -t NS"
fi

alias scp_nosec="scp -o 'StrictHostKeyChecking=no' -o 'UserKnownHostsFile=/dev/null'"
alias ssh_nosec="ssh -o 'StrictHostKeyChecking=no' -o 'UserKnownHostsFile=/dev/null'"

# OpenSSL
for i in sha512 sha256 sha1 md5 ; do
    ssl-cert-$i() {
        openssl x509 -noout -fingerprint -$i -in $1
    }
done

ssl-crt-fp() {
    for i in sha512 sha256 sha1 md5; do
        ssl-cert-$i $1
    done
}

ssl-crt-info() {
    openssl x509 -noout -text -in $1
}

ssl-csr-info() {
    openssl req -noout -text -in $1
}

ssl-csr-gen() {
    local domain="${1:-mydomain}"
    local keysize="${2:-4096}"
    local keytype="${3:-rsa}"
    local digest="${4:-sha256}"
    printf "Generating csr/key for ${domain} with ${keytype}/${keysize} key and ${digest} digest...\n"
    openssl req \
        -nodes -${digest} \
        -newkey ${keytype}:${keysize} \
        -keyout ${domain}.key \
        -out ${domain}.csr
}

#
# Devel tools aliases
#

if (( $+commands[colormake] )); then
    alias make="colormake"
fi

#
# Mssh stuff
#
if [ -f ~/.ssh/config ]; then
    hosts=($(grep '^Host [^*]' ~/.ssh/config | sed 's/Host //'))
    compctl -k hosts mssh
fi

#
# Hosting stuff
#
http_auth_ssha() {
    read "username?username: "
    read "password?password: "
    salt="$(openssl rand -base64 3)"
    sha1="$(printf "%s%s" "${password}" "${salt}" | openssl dgst -binary -sha1 | sed 's#$#'"${salt}"'#' | base64)"
    printf "%s:{SSHA}%s\n" "${username}" "${sha1}"
}

