#
# ${ZDOTDIR}/.zshrc (root)
#

umask 022

# get global profile configuration
if [[ -f "/etc/profile.env" ]]; then
    source /etc/profile.env
fi

# define different environment variables
HOME=/home/`whoami`

LANG=en_US.UTF-8
LC_ALL=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8

#unmess control chars
export LESS="-F -X -R"

export KDE_COLOR_DEBUG=true

BLACK="%{"$'\033[01;30m'"%}"
GREEN="%{"$'\033[01;32m'"%}"
RED="%{"$'\033[01;31m'"%}"
YELLOW="%{"$'\033[01;33m'"%}"
BLUE="%{"$'\033[01;34m'"%}"
BOLD="%{"$'\033[01;39m'"%}"
NORM="%{"$'\033[00m'"%}"


if [[ $HOST =~ .*miro.* ]]
then
	export JOBS="10"
else
	# icecream
	export JOBS="12"
	export PATH="/usr/lib/icecc/bin:$PATH"
fi


export EDITOR="/usr/bin/vim"
export JAVA_HOME=/usr/lib/jvm/java
#export JAVA_HOME=/usr/lib/jvm/java-1.6.0-sun-1.6.0.u11
ZDOTDIR=$HOME/.zsh

if [ `/usr/bin/whoami` = 'root' ]
then
	HOME=/root
fi

PATH="/home/sebas/bin:/home/test/bin:/bin:/usr/bin:/sbin:/usr/bin/:/usr/local/sbin:/usr/local/bin:/usr/NX/bin:$PATH:."
if [[ -d "${ZDOTDIR}/bin" ]]; then
    PATH="${ZDOTDIR}/bin:${PATH}"
fi
#PATH="/usr/lib/ccache/:$PATH"

 # load some aliases
if [[ -f "${ZDOTDIR}/zalias" ]]; then
    source ${ZDOTDIR}/zalias
fi

#  load array for host-completion
if [[ -f "${ZDOTDIR}/.zhosts" ]]; then
    source ${ZDOTDIR}/.zhosts
fi
#
# set up directory listing colors
if [[ -f "/etc/DIR_COLORS" ]]; then
    eval `dircolors -b /etc/DIR_COLORS`
fi

#
# define different arrays
#
# search path for the cd command
cdpath=(.)
#
# where to look for autoloaded function definitions
if [[ -d "${ZDOTDIR}/fpath" ]]; then
    fpath=($fpath ${ZDOTDIR}/fpath)
else
    fpath=($fpath)
fi
#
# set man path
manpath=($manpath)
export MANPATH
#
# automatically remove duplicates from these arrays
typeset -U path cdpath fpath manpath

# Use hard limits, except for a smaller stack and no core dumps
#
unlimit
limit stack 8192
limit core 0
limit -s

# set prompts
#
# left prompt

if [ `/usr/bin/whoami` = 'root' ]
then
	PROMPT="${YELLOW}%m${NORM}.${RED}%n${BLUE}(${RED}%~${BLUE})${NORM}: "
else
	PROMPT="${YELLOW}%m${NORM}.${GREEN}%n${BLUE}(${RED}%~${BLUE})${NORM}: "
fi
#PROMPT='%{[33m%}%m%{[37m%}.%{[32m%}%n%{[34m%}(%{[31m%}%~%{[34m%})%{[37m%}: '
#

# right prompt

# Git magic
#
#
parse_git_branch() {
     git branch --no-color 2> /dev/null \
      | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
#
precmd() {
    GIT_BRANCH="`git branch 2>&1 |g -v fatal |grep -v Stopping |grep \* | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1) /'`"
    RPROMPT="${BLACK}${EXTRAPROMPT}${YELLOW}${GIT_BRANCH}${GREEN}[${BLUE}%*${GREEN}]${NORM}"
}


#RPROMPT=${YELLOW}[%*]${NORM}
#RPROMPT="${ORANGE}${GIT_BRANCH}${GREEN}[${BLUE}%*${GREEN}]${NORM}"

#RPROMPT="%{^[[33m%}[%{^[[37m%}%{^[[33m%}%*%{^[[37m%}{^[[33m%}]%{^[[37m%}"
#PROMPT="%{^[[33m%}[%{^[[37m%}%{^[[33m%}%*%{^[[37m%} %{^[[33m%}]%{^[[37m%}"
battery(){
        CBS=`acpi 2>/dev/null | awk '{print $4}' | awk -F % '{print $1}'`
        if [[ ${CBS} -lt 35 ]] && [[ ${CBS} -gt 10 ]]; then
                echo " ${YELLOW}${CBS}%%${NORM}"
        elif [[ ${CBS} -lt 10 ]] || [[ ${CBS} -eq 10 ]]; then
                echo " ${RED}${CBS}%%${NORM}"
        else
                echo " ${GREEN}${CBS}%%${NORM}"
        fi
}

SHELL=/bin/zsh
# some environment variables
HISTSIZE=100000
HISTFILE="${HOME}/.zhistory"
SHARE_HISTORY=true
DIRSTACKSIZE=16
WORDCHARS="*?_-.[]~=&;!#$%^(){}<>"

# set up run-help function
#unalias run-help
#autoload run-help
#export HELPDIR=~/.zsh/help

# Autoload all shell functions from all directories in $fpath (following
# symlinks) that have the executable bit on (the executable bit is not
# necessary, but gives you an easy way to stop the autoloading of a
# particular shell function). $fpath should not be empty for this to work.
for func in $^fpath/*(N-.x:t); autoload $func

#
# load and unload options
#
# navigate options
setopt auto_cd
setopt auto_param_slash
setopt auto_pushd
setopt cdable_vars
#setopt glob_dots # show .-files in completion
setopt pushd_to_home
setopt pushd_silent
setopt pushd_ignore_dups
#
# job control
setopt no_check_jobs # don't check for running jobs when closing session
setopt no_hup # and don't kill these jobs
setopt long_list_jobs
#
# history settings
setopt extended_history
setopt share_history
setopt append_history
setopt hist_ignore_all_dups
#
# list options
setopt auto_list # automatically list choices on an ambiguous completion
setopt auto_menu # auto choice after the second consecutive request for completion
setopt list_types
setopt list_packed # try to make the completion list smaller
#setopt nomatch
setopt null_glob
setopt clobber
#
# misc options
setopt chase_links
setopt flow_control
setopt rm_star_silent
#
# shell expressions
#
setopt rc_expand_param
# testing options
setopt notify # jobs specified
setopt autoresume # see above
setopt extended_glob
#setopt bad_pattern
#
#
# unset options
unsetopt auto_remove_slash
unsetopt beep
unsetopt chase_dots
#unsetopt clobber
unsetopt complete_in_word
unsetopt correct
unsetopt correct_all
unsetopt global_export
unsetopt menucomplete

# Autoload zsh modules when they are referenced
zmodload -a zsh/stat stat
zmodload -a zsh/zpty zpty
zmodload -a zsh/zprof zprof
zmodload -a zsh/mapfile mapfile

# Some nice key bindings
#bindkey -v			# vi key bindings
# bindkey -a			# vi command bindings
# bindkey -e    		# emacs key bindings

bindkey ' ' magic-space		# also do history expansion on space
bindkey '^I' complete-word	# complete on tab, leave expansion to _expand

# CTRL + left/right moves per word
bindkey ';5D' emacs-backward-word
bindkey ';5C' emacs-forward-word

if [[ $TERM = "linux" || $TERM = "screen.linux" || $TERM = "screen" ]]; then
    bindkey "[1~" beginning-of-line
    bindkey "[4~" end-of-line
    bindkey "[3~" delete-char
    bindkey "[2~" kill-line
    bindkey "[5~" history-search-backward
    bindkey "[6~" history-search-forward

elif [[ $TERM = "rxvt" || $TERM = "Eterm" ]]; then
    bindkey "[7~" beginning-of-line
    bindkey "[8~" end-of-line
    bindkey "[3~" delete-char
    bindkey "[2~" kill-line
    bindkey "[5~" history-search-backward
    bindkey "[6~" history-search-forward

elif [[ $TERM = "xterm" ]]; then
    bindkey "OH" beginning-of-line
    bindkey "OF" end-of-line
    bindkey "[3~" delete-char
    bindkey "[2~" kill-line
    bindkey "[5~" history-search-backward
    bindkey "[6~" history-search-forward
fi

# Setup new style completion system. To see examples of the old style (compctl
# based) programmable completion, check Misc/compctl-examples in the zsh
# distribution.
autoload -U compinit; compinit -u

# Completion Styles

# list of completers to use
zstyle ':completion:*::::' completer _oldlist _expand _complete _ignored _match _correct _approximate 

# allow one error for every three characters typed in approximate completer
#zstyle -e ':completion:*:approximate:*' max-errors \
#    'reply=( $(( ($#PREFIX+$#SUFFIX)/3 )) numeric )'

# insert all expansions for expand completer
zstyle ':completion:*:expand:*' tag-order all-expansions

# formatting and messages
zstyle ':completion:*' verbose yes
zstyle ':completion:*:descriptions' format '%B%d%b'
zstyle ':completion:*:messages' format '%d'
zstyle ':completion:*:warnings' format 'No matches for: %d'
zstyle ':completion:*:corrections' format '%B%d (%{[30m%}errors%{[37m%}: %e)%b'
zstyle ':completion:*' group-name ''

# match uppercase from lowercase
#zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'

# offer indexes before parameters in subscripts
zstyle ':completion:*:*:-subscript-:*' tag-order parameters indexes

# command for process lists, the local web server details and host completion
zstyle ':completion:*:processes' command 'ps -o pid,s,nice,stime,args'
#zstyle ':completion:*:urls' local 'www' '/var/www/htdocs' 'public_html'
zstyle '*' hosts $hosts

# Filename suffixes to ignore during completion (except after rm command)
#zstyle ':completion:*:*:(^rm):*:*files' ignored-patterns '*?.o' '*?.c~' \
#    '*?.old' '*?.pro'
# the same for old style completion
#fignore=(.o .c~ .old .pro)

# ignore completion functions (until the _ignored completer)
zstyle ':completion:*:functions' ignored-patterns '_*'

# and some stuff by groover
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}

if [ "$KONSOLE_DCOP_SESSION" ]
then
        if [ `/usr/bin/whoami` != 'root' ]
                then
                alias ssh='konsolewrap  ssh'
                alias lftp='konsolewrap  lftp'
                alias sftp='konsolewrap  sftp'
                alias ssh='konsolewrap  ssh'
                alias su='konsolewrap su'
                alias ipy='konsolewrap ipython'
        #preexec () {konsole-rename-cmd $1}
        precmd () {
            konsole-rename-path
            if [ `/bin/hostname` = 'luna' ]; then
                RPROMPT="${BLUE}[${YELLOW}%*${NORM} `battery`${BLUE}]${NORM}"
            fi
        }
        fi
fi

function svndiff () { svn diff $@ | colordiff |less -R; }
function wl-env () { . $ZDOTDIR/wl-env }
function kf5-env () { . $ZDOTDIR/kf5-env }
function kde4-env () { . $ZDOTDIR/kde4-env }
function kde3-env () { . $ZDOTDIR/kde3-env }

if [ "$KDE_SESSION_VERSION" = "4" ]
then
    #echo "Welcome to your KDE4 environment."
    #export PATH=:/home/kdedev/kde/bin/:$PATH
    #. $HOME/.zsh/kde4-env
    # KDE
    #KDEDEV_HOME=/home/sebas/kdesvn
    #export KDEDIR=$KDEDEV_HOME/install
    #export KDEHOME=$HOME/.kde4
    #export KDETMP=/tmp/$USER-kde4
    #mkdir -p $KDETMP
    #export KDEVARTMP=/var/tmp/$USER-kde4
    #export KDEDIRS=$KDEDIR
    #echo "KDEDIR: $KDEDIR" 
    # Qt
    #export QTDIR=$KDEDEV_HOME/qt-copy
    #export QT_PLUGIN_PATH=$KDEDIR/lib/kde4/plugins:$QT_PLUGIN_PATH
    #export PKG_CONFIG_PATH=$QTDIR/lib/pkgconfig:$PKG_CONFIG_PATH
    # DBus
    #export DBUSDIR=$KDEDIR
    #export LD_LIBRARY_PATH=$QTDIR/lib:$KDEDIR/lib:$LD_LIBRARY_PATH
    #export PKG_CONFIG_PATH=$DBUSDIR/lib/pkgconfig:$PKG_CONFIG_PATH
    # Other
    #export PATH=$QTDIR/bin:$KDEDIR/bin:$PATH
    #export YACC='byacc -d'
    # XDG
    #unset XDG_DATA_DIRS # unset XDG_DATA_DIRS, to avoid seeing kde3 files from /usr
    #unset XDG_CONFIG_DIRS
    echo $PATH|grep kdesvn >/dev/null && kde4-env
fi


function colortest () {
    echo $fg[blue]TESTCOLORS
}


#unset GTK2_RC_FILES
#unset GTK_RC_FILES

