# Last Change: 2011 Dez 09 06:54:29
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# http://www.caliban.org/bash/

#                  ( O O )
#   +===========oOO==(_)==OOo==============+
#   |                                      |
#   |     °v°   Sergio Luiz Araujo Silva   |
#   |    /(_)\  Linux User #423493         |
#   |     ^ ^   voyeg3r ?  gmail.com       |
#   +======================================+

INPUTRC=~/.inputrc
# bash improviments ~/.inputrc
# wget -c https://raw.github.com/gist/1284081 -O ~/.inputrc

# avoid ctrl-s freeze your terminal
stty stop ""

# protegendo arquivos contra sobrescrita
#set -o noclobber

# bash completion to sudo command
complete -cf sudo
complete -d cd mkdir rmdir

alias xclip='xclip -selection c'
alias config-data='sudo ntpdate -u -b bonehed.lcs.mit.edu'
alias translate='zzdicbabelfish'
alias tt='zzdicbabelfish'

# testa se há o comando grin e exporta variáveis do mesmo
which grin &>/dev/null && export GRIN_ARGS="-C 2 --no-skip-dirs"

# se houver o navegador epiphany torne ele o navegador padrão
#which epiphany-browser &>/dev/null && { export BROWSER=`which epiphany-browser` ;}

shopt -s histverify                    # verifica comandos do histórico
shopt -s checkwinsize                  # ajusta janela redimensionada
shopt -s hostcomplete
[ ${BASH_VERSINFO[0]} -ge 4 ] && shopt -s globstar
shopt -s cdspell                       # fix wrong type keys
shopt -s dirspell

# convert text to lowcase
lower() { echo ${@,,}; }
expandurl() { curl -sIL $1 | awk '/^Location/ || /^Localização/ {print $2}' ; }
# calculadora com o comando bc
calc(){ echo "scale=2;[email protected]" | bc;}
ff () { find . -type f -iname '*'"[email protected]"'*' ; }
mkcd() { mkdir -p "[email protected]" && cd $_; }
function decToBin { echo "ibase=10; obase=2; $1" | bc; }

biggest (){
    du -k * | sort -nr | cut -f2 | head -20 | xargs -d "\n" du -sh
#du  | sort -r -n | awk '{split("k m g",v); s=1; while($1>1024){$1/=1024; s++} print int($1)" "v[s]"\t"$2}'
}


beep () {
echo -e -n \\a
}

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
export HISTCONTROL=ignoredups
# ... and ignore same sucessive entries.
export HISTCONTROL=ignoreboth
export HISTIGNORE="&:ls:pwd:[bf]g:ssh *:exit"

# 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

use_color=false

# Set colorful PS1 only on colorful terminals.
# dircolors --print-database uses its own built-in database
# instead of using /etc/DIR_COLORS.  Try to use the external file
# first to take advantage of user additions.  Use internal bash
# globbing instead of external grep binary.
safe_term=${TERM//[^[:alnum:]]/?}   # sanitize TERM
match_lhs=""
[[ -f ~/.dir_colors   ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
[[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
[[ -z ${match_lhs}    ]] \
        && type -P dircolors >/dev/null \
        && match_lhs=$(dircolors --print-database)
[[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color=true

MYIP=`hostname -i`
if ${use_color} ; then
        # Enable colors for ls, etc.  Prefer ~/.dir_colors #64489
        if type -P dircolors >/dev/null ; then
                if [[ -f ~/.dir_colors ]] ; then
                        eval $(dircolors -b ~/.dir_colors)
                elif [[ -f /etc/DIR_COLORS ]] ; then
                        eval $(dircolors -b /etc/DIR_COLORS)
                fi
        fi

        if [[ ${EUID} == 0 ]] ; then
                PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]${MYIP:-`hostname -I`}\[\033[01;34m\] \W \$\[\033[00m\] '
        else
                PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]${MYIP:-`hostname -I`}\[\033[01;34m\] \w \$\[\033[00m\] '
        fi

        alias ls='ls --color=auto'
        alias grep='grep --colour=auto'
else
        if [[ ${EUID} == 0 ]] ; then
                # show [email protected] when we don't have colors
                PS1='\[email protected]${MYIP:-`hostname -I`} \W \$ '
        else
                PS1='\[email protected]${MYIP:-`hostname -I`} \w \$ '
        fi
fi

# Try to keep environment pollution down, EPA loves us.
unset use_color safe_term match_lhs


# 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.

[ -f ~/.bash_aliases ] && . ~/.bash_aliases

extract() {
    for i in "[email protected]";  do
        if [ -f "$i" ]; then
            case "$i" in
                *.[tT][aA][rR].[bB][zZ]|*.[tT][bB][zZ])   tar xjvf "$i" ;;
                *.[tT][aA][rR].[bB][zZ]2|*.[tT][bB][zZ]2) tar xjvf "$i" ;;
                *.[tT][aA][rR].[gG][zZ]|*.[tT][gG][zZ])   tar xzvf "$i" ;;
                *.[gG][tT][gG][zZ])                       tar xzvf "$i" ;;
                *.[bB][zZ]2)                              bunzip2 "$i" ;;
                *.[rR][aA][rR])                           rar x -o+ "$i" ;;
                *.[gG][zZ])                               gunzip "$i" ;;
                *.[tT][aA][rR])                           tar xvf "$i" ;;
                *.[zZ][iI][pP])                           unzip -o "$i" ;;
                *.Z)                                      uncompress "$i" ;;
                *.7[zZ])                                  7z x "$i" ;;
                *)                                        echo "don't know how to extract '$i' ..." ;;
            esac
        else
               echo "$i is not a valid file"
        fi
    done
   }

alias syncode='hg push ssh://[email protected]/sergio/vimfiles'

alias ssh="ssh -C"
alias scp='scp -r '
alias gril="grep -ril"

# xterm
alias xterm='xterm -ls -bg black -fg white -cr -fs 11 white -hc white rightbar'

alias apt-get="apt-get -o Acquire::http::Dl-Limit=15"

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

# some more ls aliases
alias ll='ls -l'
alias la='ls -A'
alias l='ls -CF'

# alias para abrir o último arquivo editado pelo vim
alias lvim="vim -c \"normal '0\""

alias cdesk="cd ${HOME}/Desktop"

# Dica retirada do blog do mitre
# http://jfmitre.blogspot.com/2006/05/convertendo-arquivos-utf-8-em-isso-8859.html
alias iso2utf='iconv -f iso-8859-1 -t utf-8'
alias utf2iso='iconv -f utf-8 -t iso-8859-1'

# mostra os dez comandos mais utilizados
# history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}' | sort | uniq -c | sort -n | tail | sort -nr

#alias grep="grep --color=always"
# comentei por problemas com o grep

# 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 ] && ! shopt -oq posix; then
    . /etc/bash_completion
fi

# Evita histórico com linhas duplicadas
export HISTCONTROL=ignoredups

# configuração do path
#PATH=$PATH:~/bin

alias path='echo -e ${PATH//:/\\n}'

# para chamar o pythonrc
PYTHONSTARTUP="$HOME/.pythonstartup"
export PYTHONSTARTUP

myip (){
	clear
	echo
	DEV=`awk '/eth/ {print $1}' <(netstat -i)` # indentifica o device
	MEUIP=`hostname -I`
	#echo "       IP LOCAL: .....`awk '/inet end/ {print $3}' <(ifconfig eth0)`"
	echo "        IP LOCAL: .....${MEUIP:-'Not connected'}"
	echo "        NETMASK: ......`awk -F':' '/Mas/ {print $4}' <(ifconfig $DEV)`"
	echo "        MAC ADDRESS: ..`awk '/HW/ {print $7}' <(ifconfig $DEV)`"
	echo "        ROUTER: .......`awk '/UG/ {print $2}' <(route -n)`"
    echo "        IP EXTERNO: ...`curl --connect-timeout 4 -s sputnick-area.net/ip`"
    echo
    read -sn 1 -p "     Pressione uma tecla para continuar..."
	clear
}


export LESS="-P ?c<- .?f%f:Standard input.  ?n:?eEND:?p%pj\%.. .?c%ccol . ?mFile %i of %m  .?xNext\ %x.%t   Press h for help"

# set o vim como editor padrão
export EDITOR=vim
export VISUAL=vim

CDPATH=.:..:~:~/docs/img:~/docs:~/bin:~/tmp


export PROMPT_COMMAND="history -a"
export HISTFILESIZE=2000

# manpages coloridas
# begin blinking
# export LESS_TERMCAP_mb=$'\E[01;31m'
# # begin bold
# export LESS_TERMCAP_md=$'\E[01;33m'
# # end mode
# export LESS_TERMCAP_me=$'\E[0m'
# # begin standout-mode - info box
# export LESS_TERMCAP_so=$'\E[01;41;37m'
# # end standout-mode
# export LESS_TERMCAP_se=$'\E[0m'
# # begin underline
# export LESS_TERMCAP_us=$'\E[01;32m'
# # end underline
# export LESS_TERMCAP_ue=$'\E[0m'

# desabilitando a biblioteca pango para o firefox
export MOZ_DISABLE_PANGO=1

# http://ubuntuforums.org/archive/index.php/t-80289.html
export FLASH_GTK_LIBRARY=libgtk-x11-2.0.so.0

export PATH=/var/lib/gems/1.8/bin:$PATH

# manpages coloridas
#export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking
#export LESS_TERMCAP_md=$'\E[01;38;5;74m' # begin bold
#export LESS_TERMCAP_me=$'\E[0m' # end mode
#export LESS_TERMCAP_se=$'\E[0m' # end standout-mode
#export LESS_TERMCAP_so=$'\E[38;5;246m' # begin standout-mode - info box export LESS_TERMCAP_ue=$'\E[0m' # end underline
#export LESS_TERMCAP_us=$'\E[04;38;5;146m' # begin underline

# Instalacao das Funcoes ZZ (www.funcoeszz.net)
#export ZZOFF=""  # desligue funcoes indesejadas
#export ZZPATH="${HOME}/bin/funcoeszz"  # script
#source "$ZZPATH"