# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return
export NETHACKOPTIONS="name:Yency"

export EDITOR=vim
export VISUAL=vim

# don't put duplicate lines in the history. See bash(1) for more options
export HISTCONTROL=ignoredups

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

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

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

BLUE="\[\033[0;34m\]"
BLUE_BOLD="\[\033[1;34m\]"
RED="\[\033[0;31m\]"
RED_BOLD="\[\033[1;31m\]"
GREEN="\[\033[0;32m\]"
GREEN_BOLD="\[\033[1;32m\]"
YELLOW="\[\033[0;33m\]"
YELLOW_BOLD="\[\033[1;33m\]"
BLUE="\[\033[0;34m\]"
BLUE_BOLD="\[\033[1;34m\]"
MAGENTA="\[\033[0;35m\]"
MAGENTA_BOLD="\[\033[1;35m\]"
CYAN="\[\033[0;36m\]"
CYAN_BOLD="\[\033[1;36m\]"
WHITE="\[\033[0;37m\]"
WHITE_BOLD="\[\033[1;37m\]"
GRAY="\[\033[1;30m\]"
NO_COLOUR="\[\033[0m\]"

MAIN_COLOR=$WHITE
SUB_COLOR=$RED

PRIM_COL=$SUB_COLOR
PRIM_COL_CLEAN=${PRIM_COL#\\[}
PRIM_COL_CLEAN=${PRIM_COL_CLEAN%\\\]}
SEC_COL=$MAIN_COLOR
SEC_COL_CLEAN=${SEC_COL#\\[}
SEC_COL_CLEAN=${SEC_COL_CLEAN%\\]}
GIT_BR_COL_CLEAN=${YELLOW#\\[}
GIT_BR_COL_CLEAN=${GIT_BR_COL_CLEAN%\\]}
YELLOW_CLEAN=${YELLOW#\\[}
YELLOW_CLEAN=${GIT_BR_COL_CLEAN%\\]}

function get_git_reponame {
    v=`realpath .`
    while [ ! -d "$v/.git" -a "$v" != "/" ]; do
        v=`dirname "$v"`
    done
    if [ ! -d "$v/.git" -a "$v" == "/" ]; then
        echo 
    else
        echo `basename "$v"`
    fi
}

function get_git_branch {
    CURR_BRANCH=$(git branch --no-color 2> /dev/null | grep '\*')
    if test $? -eq 0 ; then
        CURR_BRANCH=$(echo "$CURR_BRANCH" | cut --delimiter=" " --fields=2-)
        if [ "$CURR_BRANCH" == '(no branch)' ]; then
            CURR_BRANCH=$(git show | head -1 | cut --delimiter=" " --fields=2)
        fi
        rn=`get_git_reponame`
        unstaged_changes_format="${YELLOW_CLEAN}*"
        git diff --no-ext-diff --ignore-submodules --quiet --exit-code || unstaged_changes=$unstaged_changes_format
        if [ "x"$rn == "x" ]; then
            echo -e $PRIM_COL_CLEAN:$GIT_BR_COL_CLEAN$CURR_BRANCH$unstaged_changes
        else
            echo -e $PRIM_COL_CLEAN:$GIT_BR_COL_CLEAN$rn$PRIM_COL_CLEAN:$GIT_BR_COL_CLEAN$CURR_BRANCH$unstaged_changes
        fi
    fi
}
PS1="$PRIM_COL[$SEC_COL\h$PRIM_COL\$(get_git_branch)$PRIM_COL:$SEC_COL\W$PRIM_COL]$NO_COLOUR "

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

#if [ -f ~/.bash_aliases ]; then
#    . ~/.bash_aliases
#fi

# enable color support of ls and also add handy aliases
if [ "$TERM" != "dumb" ]; then
    eval "`dircolors -b`"
    alias ls='ls --color=auto -X --group-directories-first'
    #alias dir='ls --color=auto --format=vertical'
    #alias vdir='ls --color=auto --format=long'
fi

LS_COLORS='no=00:fi=00:di=00;36:ln=01;34:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.flac=01;35:*.mp3=01;35:*.mpc=01;35:*.ogg=01;35:*.wav=01;35:';
export LS_COLORS

#fortune -a

alias s="sudo su -"
# some more ls aliases
#alias ll='ls -l'
#alias la='ls -A'
#alias l='ls -CF'

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

set -o vi

if [ -f /usr/share/doc/cdargs/examples/cdargs-bash.sh ]; then
    . /usr/share/doc/cdargs/examples/cdargs-bash.sh
fi

alias gs="git status -s"
alias gd="git diff"
alias gti=git
alias gsd="git svn dcommit"
alias gsr="git svn rebase"
alias sfh="urxvt -e bash &"
alias s7="ssh server7"
alias s8="ssh server8"
alias meteocolombia="ssh -p 10022 geoentropica.com"
alias geo1="ssh jerojasro@190.84.255.109"

# lock the screen; start xscreensaver if necessary
function lock {
    xscreensaver-command -lock 
    if [ $? -ne 0 ]; then
        xscreensaver &
        xscreensaver-command -lock
    fi
}

function window_decoration {
    cmd=$(expr match "$BASH_COMMAND" '\([^ ]\+\)')
    if [ x$cmd == "xls" ] ; then
        echo $PWD
    else
        if [ x$cmd == "xcd" ] ; then
            echo $PWD
        else
            if [ x$cmd == "xcv" ] ; then
                echo $PWD
            else
                if [ x$cmd == "xprompt_command" ] ; then
                    echo $PWD
                else
                    echo $BASH_COMMAND
                fi
            fi
        fi
    fi
}

trap 'printf "\33]2;%s\007" "$(window_decoration)"' DEBUG

# copy the full path of the received argument to the clipboard
# when no arguments are received, copy full path of current dir to the clipboard
function cpl {
    echo -n "$PWD/$1" | xclip -i
}

# a wrapper for git commit -am
function gca {
    git commit -am "$*"
}
function gc {
    git commit -m "$*"
}

function prompt_command {
    last_command=$(history | tail -1 | awk '{print $2}')
    if [ x$last_command == xcd ] || [ x$last_command == xcv ] || [ x$last_command == xrm ] || [ x$last_command == xmkdir ] || [ x$last_command == xmv ]; then
        ls
    fi
}

function rae {
    dict -d drae $*
}

function enes {
    dict -d fd-eng-spa $*
}

function esen {
    dict -d fd-spa-eng $*
}

function den {
    dict -d gcide $*
}

export PROMPT_COMMAND=prompt_command

export PATH=$PATH:$HOME/bin

function sex {
    s=$(sex.py $*)
    test -n "$s" && cd $s && ls
}

function see {
    /usr/bin/see $* &
}

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
