# ~/.bashrc: this file contains global defaults used by all Bourne (and
# related) shells. This file can be copied freely to other hosts.

# Set beautiful colors :)
# Use actual , as Bash only understands \033, whereas sed only
# understands \x1B…
standard="[0m"

black="[0;30m"
darkgray="[1;30m"
darkred="[0;31m"
red="[1;31m"
darkgreen="[0;32m"
green="[1;32m"
darkyellow="[0;33m"
yellow="[1;33m"
darkblue="[0;34m"
blue="[1;34m"
darkpurple="[0;35m"
purple="[1;35m"
darkcyan="[0;36m"
cyan="[1;36m"
gray="[0;37m"
white="[1;37m"

# Default shell colors
usercolor=$green
hostcolor=$white

# Fix $PATH when changing users too
if [ -e ~/env/bash/profile.d/path.sh ]; then
    . ~/env/bash/profile.d/path.sh
fi

# Set the values for some environment variables
export HOSTNAME="`hostname`"

if [[ `uname -a` == FreeBSD* ]]; then
    export LS_OPTIONS='-FGTh'
    export LSCOLORS='EaFacafaCadabaDaDaGaHa'
else # Linux
    export LS_OPTIONS="-Fh --color=auto --group-directories-first --time-style=+%Y-%m-%d\ %H:%M:%S"
    export LSCOLORS='di=1;;40:ln=1;;40:so=32;40:pi=35;40:ex=1;;40:bd=33;40:cd=31;40:su=1;;40:sg=1;;40:tw=1;;40:ow=1;;40:'
fi

# Set timezone
export TZ='Europe/Amsterdam'

# Editing
export EDITOR="vim"
export VISUAL="vim"

# History
export HISTCONTROL=erasedups
export HISTSIZE=10000

# Screen
# Used e.g. in ../scripts/osc52.sh
export SCREEN_LEVEL=1

# Set default POSIX locale
export LC_ALL="en_US.UTF-8"
export LANG="en_US.UTF-8"

# Searching files using fzf and ag
export FZF_DEFAULT_COMMAND='ag --nocolor -g "" -l'

# Enable fuzzy searching in bash using Ctrl-T or **<TAB>
[ -f ~/.fzf.bash ] && source ~/.fzf.bash

# Enable fast searching through directories using Alt+C
export FZF_ALT_C_COMMAND='blsd'
export FZF_ALT_C_OPTS="--preview 'tree -C {} | head -$LINES'"

# XXX: see $a_cmds in ~/env/scripts/fzf/shell/completion.bash for extra supported commands

# Check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# Set host autocomplete for SSH
if [ -e ~/.ssh/config ]; then
    # Don't include scp, because it'll break filename completion
    complete -W "$(echo `cat ~/.ssh/config | grep "Host " | sed 's/Host //'`)" ssh
fi

# Set autocomplete for AirPods
if [ -e ~/env/scripts/airpods.sh ]; then
  complete -W "$(~/env/scripts/airpods.sh)" airpods.sh
fi

# Set some aliases
alias 444='find . -type f -exec chmod 444 {} \;'
alias 644='find . -type f -exec chmod 644 {} \;'
alias 664='find . -type f -exec chmod 664 {} \;'
alias 666='find . -type f -exec chmod 666 {} \;'
alias 555='find . -type d -exec chmod 555 {} \;'
alias 755='find . -type d -exec chmod 755 {} \;'
alias 775='find . -type d -exec chmod 775 {} \;'
alias 777='find . -type d -exec chmod 777 {} \;'
# Force case-insensitive search, because smart case matching doesn't work when searching for filenames...
alias ag='ag -if'
alias cp='cp -i'
alias duh='du -h --max-depth=1'
alias find-non-exif='for file in `find . -type f -iname "*.jpg"`; do exiftool $file|grep "Exif Version" >/dev/null || echo $file; done'
alias jq='jq -S' # run with '.' (filter) if you can't pipe it
alias l="ls $LS_OPTIONS -la"
alias la="ls $LS_OPTIONS -a"
alias ll="ls $LS_OPTIONS -l"
alias ls="ls $LS_OPTIONS"
alias mv='mv -i'
alias parent='ps -f $@'
alias phplint='find . -type f -name \*.php -exec php -l "{}" \; | grep "Parse error"'
alias ping='ping -c4'
alias ping6='ping6 -c4'
alias psx='ps aux --forest'
# screen needs $CAPTION_OFFSET to properly align the caption
alias screen='CAPTION_OFFSET=$((`whoami|wc -c`+`hostname -s|wc -c`+18)) screen'

# Set some aliases for root
if [ "`id -u`" = "0" ]; then
    root () { chown -R root:root "$@"; }
    nobody () { chown -R nobody:nogroup "$@"; }
fi


# Set up prompt
ttynr=$(tty|cut -c9-11)
# Root gets red
if [ $(whoami) = 'root' ]; then
    usercolor=$red
fi

# Make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# Show smileys indicating success/failure per piped command
smileys () { echo ":-${PIPESTATUS[@]}" | sed 's/ //g' | sed 's/0/)/g' | sed 's/[^:)-]/(/g'; }

# Grep code (show matching files, or open those files in vim)
agf () { ag -l "$@"; }
# Pass last argument to Vim for searching (so not e.g. "--php")
agv () { (IFS=$'\n'; set -f; exec vim `ag -l "$@"` -c "/$1"); }

# Handy git wrappers
gitb() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/ (\1)/"; }
gitdiff () { git diff --ignore-space-change "$@" | vim -; }
gitdiffsplit() { git difftool "$@"; }
gitlog() { git log --name-status "$@"; }
gitpull() { git pull --rebase "$@"; }
gitst() { git status "$@"; }

# Handy subversion wrappers
svnann () { svn ann "$@" | vim -; }
svndiff () { svn diff --diff-cmd diff -x -uw "$@" | vim -c "set ft=diff" -; }
svndiffsplit () { vimdiff <(svn diff "$@"|grep -E '^-[^-]'|cut -c 2-) <(svn diff "$@"|grep -E '^\+[^\+]'|cut -c 2-); }
svnlog () { svn log -v "$@" | less; }
svnst () { svn st "$@"; }
svnup () {
    revision1="`svn info \"$@\"|grep Revision|expr \`awk '{ print $2 }'\` + 1`";
    svn up "$@";
    revision2="`svn info \"$@\"|grep Revision|awk '{ print $2 }'`";
    if [ $revision1 -le $revision2 ]; then
        svn log -r$revision1:$revision2 -v "$@";
    fi
}


# Include system-wide definitions
if [ -e /etc/env/bash/bash_include.$HOSTNAME ]; then
    . /etc/env/bash/bash_include.$HOSTNAME
fi

# Include user definitions
if [ -e ~/.bash_include ]; then
    . ~/.bash_include
fi

# Set up prompt (last, after bash_include!)
# Escape colors using \[\] to prevent the cursor going haywire
export PS1="\$(smileys) \t \["$usercolor"\]\u\["$standard"\]@\["$hostcolor"\]"$HOSTNAME"\["$standard"\]@\["$blue"\]tty"$ttynr"\["$standard"\]:\w>"
export PS2='> '
