#echo .env

# environment
PATH="${HOME}/.local/bin:${HOME}/bin:/usr/lib/ccache:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin:/usr/games"
# shellcheck disable=SC2249
case "${HOSTNAME}" in
  *.*) HOSTNAME=$(echo "${HOSTNAME}" | cut -f1 -d.) ;;
esac
export HOSTNAME

# [ -z "${TMPDIR:-}" ] && [ -d "$HOME/tmp" ] && export TMPDIR="$HOME/tmp"

# shellcheck disable=SC2312
: "${UID:=$(id -u)}"
if [ -z "${XDG_RUNTIME_DIR:-}" ] && [ -r "/run/user/${UID}" ]; then
  export XDG_RUNTIME_DIR="/run/user/${UID}"
fi
if [ -z "${SSH_AUTH_SOCK:-}" ] && [ -S "${XDG_RUNTIME_DIR}/gnupg/S.gpg-agent.ssh" ]; then
  export SSH_AUTH_SOCK="${XDG_RUNTIME_DIR}/gnupg/S.gpg-agent.ssh"
fi
if [ -z "${GPG_AGENT_INFO:-}" ] && [ -S "${XDG_RUNTIME_DIR}/gnupg/S.gpg-agent" ]; then
  export GPG_AGENT_INFO="${XDG_RUNTIME_DIR}/gnupg/S.gpg-agent:0:1"
fi
# if [ -z "${DISPLAY:-}" ] && [ -S "/tmp/.X11-unix/X0" ]; then
#   export DISPLAY=:0
# fi

# check whether we run interactively
[ -n "${PS1}" ] || return
[ -n "${BASH_VERSION}" ] || return
#echo ".env: interactive"

# check for rxvt-(unicode-)256color support (in ncurses-term)
# shellcheck disable=SC2249
case ${TERM} in
  rxvt*)
    if [ ! -f "/lib/terminfo/r/${TERM}" ] \
      && [ ! -f "/usr/share/terminfo/r/${TERM}" ]; then
      TERM="rxvt-unicode" # ncurses-base: /lib/terminfo/r/rxvt-unicode
    fi
  ;;
esac

export LS_COLORS='di=01;34:ln=01;36:*.deb=32:*.dsc=31:*.changes=1;35:'
export QUOTING_STYLE='literal'
export PGDATABASE='postgres'

# Aliases

alias -- -='popd && ps1_set_vcs && ls'
cal() { if [ -n "$1" ]; then ncal -bw "$@"; else ncal -bw3; fi; }
alias d=date
alias diffstat='diffstat -p1 -r2'
alias f=finger
alias gf=sl
alias ip='ip -c=auto'
alias m='mutt'
alias man='man -LC'
alias ncdu='ncdu --color dark -e'
alias ping='ping -n'
alias rehash='hash -r'
alias y=sl

# functions

# set umask depending on directory group/sgid bit
# auto_umask() {
#   # local dir group
#   umask 022
#   # if [[ $(stat -c "%04a" .) == [267]* ]]; then # sgid directory
#   # shellcheck disable=SC2249
#   case $(stat -c "%04a" .) in
#     [267]*)
#       dir=$(stat -c "%g" .)
#       for group in ${GROUPS[@]}; do
#         [ "${group}" -eq "${dir}" ] && {
#           umask 002
#           break
#         }
#       done
#       ;;
#   esac
# }

# # ps1 {{{
# ps1_vcs_git() {
#   local branch
#   branch=$(git branch | sed -ne 's/^\* \(.*\)/\1/p')
#   PS1_VCS=" $branch"
# }
# ps1_quilt() {
#   unset PS1_QUILT
#   local top="$(quilt top 2>/dev/null)"
#   if [ "$top" ]; then
#     PS1_QUILT=" q:$(echo $top | sed -e 's/^\(.\{12\}\).\+/\1../')"
#   fi
# }
# ps1_vcs() { # executed on every prompt
#   case $VCS in
#     git) ps1_vcs_git ;;
#   esac
#   if [ "$QUILT" ]; then
#     ps1_quilt
#   fi
# }
# ps1_set_vcs() { # executed on dir change
#   unset VCS PS1_VCS QUILT PS1_QUILT
#   local d
#   for d in . .. ../.. ../../.. ../../../..; do
#     if git ls-files --error-unmatch . >/dev/null 2>&1; then
#       VCS="git"
#       if [ "$(find .git/FETCH_HEAD -mtime +1 2>/dev/null)" ]; then
#         printf '%b\n' "\e[43mLast git pull: $(date -r .git/FETCH_HEAD +%x)\e[0m"
#       fi
#       break
#     elif [ -d $d/.svn ] && [ -r $d/.svn ]; then
#       local info branch
#       info=$(LC_ALL=C svn info 2>/dev/null)
#       if [ "$info" ]; then
#         branch=$(echo "$info" | sed -ne 's/^URL: .*branches\/\([^/]*\).*/:\1/p')
#         PS1_VCS=" svn$branch"
#       fi
#       break
#     elif [ -d $d/.bzr ] && [ -r $d/.bzr ]; then
#       PS1_VCS=" bzr"
#       break
#     elif [ -d $d/.hg ] && [ -r $d/.hg ]; then
#       PS1_VCS=" hg"
#       break
#     elif [ -d $d/CVS ] && [ -r $d/CVS ]; then
#       PS1_VCS=" cvs"
#       break
#     fi
#   done
#   # support quilt on top of the other VCS
#   for d in . .. ../.. ../../.. ../../../..; do
#     if [ -d $d/.pc ] || [ -f $d/debian/changelog ]; then
#       QUILT="1"
#       break
#     fi
#   done
#
#   if [ "$VTE_VERSION" ]; then
#     PS1_VTE=$(printf "\033]7;file://%s%s\033\\" "${HOSTNAME}" "$(/usr/libexec/vte-urlencode-cwd)")
#   fi
# }
# ps1_set_vcs
# # }}}
# # cd with some nice options {{{
# cd() {
#   local cur dir d d0 arg opt OPTIND OPTERR
#   while getopts "10nprLP" opt; do
#     case $opt in
#       1)
#         set */
#         dir="$1"
#         [ "$dir" ] || return 1
#         ;;
#       0)
#         for dir in */; do :; done
#         [ "$dir" ] || return 1
#         ;;
#       n)
#         cur="../$(basename "$PWD")/"
#         for d in ../*/; do
#           [ "$d0" = "$cur" ] && {
#             dir="$d"
#             break
#           }
#           d0="$d"
#         done
#         [ -z "$dir" ] && return 1
#         ;;
#       p | r)
#         cur="../$(basename "$PWD")/"
#         for d in ../*/; do
#           [ "$d" = "$cur" ] && {
#             dir="$d0"
#             break
#           }
#           d0="$d"
#         done
#         [ -z "$dir" ] && return 1
#         ;;
#       L | P) arg="-$opt" ;;
#     esac
#   done
#   shift "$((OPTIND - 1))"
#   [ -n "${DIRSTACK[10]}" ] && popd +10 -n >/dev/null
#   # support "cd %1"
#   case "$1" in
#     %*) dir="$(jobs "$1" | perl -ne 's/wd: ~/wd: $ENV{HOME}/; print $1 if /wd: (.*)\)/')" ;;
#   esac
#   pushd "${arg}" "${dir:-${1:-$HOME}}" >/dev/null || return "$?"
#   [ -n "${AUTO_UMASK}" ] && auto_umask
#   ps1_set_vcs
#   ls
# }
# # }}}

# mkdir/cd wrappers
nd() { mkdir "$1" && cd "$1" || return; }
nv() {
  nv=$(nv.pl "$@") && cd "${nv}" || return
}

# untar and cd into dir
unalias ttar xtar 2>/dev/null || : # remove system default
if type -p xtar.pl >/dev/null; then
  xtar() {
    xtar=$(xtar.pl "$@") && cd "${xtar}" || return
  }
else
  alias xtar='tar xvzf'
fi

# PostgreSQL
pgenv() {
  [ -n "$1" ] || return 1
  export PGVERSION="$1"
  if pg_lsclusters "${PGVERSION}" main >/dev/null 2>&1; then
    export PGCLUSTER="${PGVERSION}/main"
    # shellcheck disable=SC2155
    export PGPORT="$(pg_conftool -s "${PGVERSION}" main show port || echo 5432)"
  fi
  export PG_CONFIG="/usr/lib/postgresql/${PGVERSION}/bin/pg_config"
  PATH="/usr/lib/postgresql/${PGVERSION}/bin:${PATH#/usr/lib/postgresql/*:}"
}

# personal stuff
if [ "${USER}" = "cb\*" ] || [ "${USER}" = myon ]; then
  export DEBEMAIL="myon@debian.org"
  #export DEBEMAIL="christoph.berg@credativ.de"
fi
