export LC_ALL=en_US.UTF-8
alias make=makeobj

export AKONADI_INSTANCE="kf5"
export AKONADI_DISABLE_AGENT_AUTOSTART="True"
export PS1="(kf5) $PS1"

prepend() { [ -d "$2" ] && eval $1=\"$2\$\{$1:+':'\$$1\}\" && export $1 ; }
prepend PATH /usr/local/bin

export CUSTOM_PREFIX=$HOME/arbeit/kf5

export QT_LOGGING_CONF=$CUSTOM_PREFIX/src/logging.rules
export KF5=$CUSTOM_PREFIX
export QTDIR=$CUSTOM_PREFIX/qt5
export XDG_DATA_DIRS=$KF5/share:$XDG_DATA_DIRS:/usr/share
export XDG_CONFIG_DIRS=$KF5/etc/xdg:$XDG_CONFIG_DIRS:/etc/xdg
export PATH=$KF5/sbin:$KF5/bin:$QTDIR/bin:$PATH
export QT_PLUGIN_PATH=$KF5/lib/plugins:$KF5/lib64/plugins:$KF5/lib/x86_64-linux-gnu/plugins:$QTDIR/plugins:$QT_PLUGIN_PATH
export KDE_SESSION_VERSION=5
export KDE_FULL_SESSION=true

export XDG_DATA_HOME=$KF5/.local/share
export XDG_CONFIG_HOME=$KF5/.config
export XDG_CACHE_HOME=$KF5/.cache

export CMAKE_PREFIX_PATH=$KF5:$CMAKE_PREFIX_PATH

export LD_LIBRARY_PATH=$KF5/lib:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=$KF5/lib/pkgconfig:$PKG_CONFIG_PATH
export KSYCOCA=$CUSTOM_PREFIX/sycoca2


export KDE_BUILD=$CUSTOM_PREFIX/build
export KDE_SRC=$CUSTOM_PREFIX/src

function cmakekde {
        local srcFolder current

        # get srcFolder for current dir
        srcFolder=`pwd | sed -e s,$KDE_BUILD,$KDE_SRC,`
        # we are in the src folder, change to build directory
        # Alternatively, we could just use makeobj in the commands below...
        current=`pwd`
        if [ "$srcFolder" = "$current" ]; then
                cb
        fi
        # To enable tests, uncomment -DKDE4_BUILD_TESTS=TRUE.
        # If you are building to debug or develop, it is recommended to
        # uncomment the line -DCMAKE_BUILD_TYPE=debugfull. Be sure to move
        # it above the -DKDE4_BUILD_TESTS=TRUE line if you leave that one
        # commented out. You can also change "debugfull" to "debug" to save
        # disk space.
        # Some distributions use a suffix for their library directory when
        # on x86_64 (i.e. /usr/lib64) if so, you have to add -DLIB_SUFFIX=64
        # for example.
        # Some distributions use a different home folder for KDE4,
        # e.g. ~/.kde4. In that case you have to append
        # -DKDE_DEFAULT_HOME=.kde4

        cmake "$srcFolder" $@ \
            -DCMAKE_INSTALL_PREFIX=$CUSTOM_PREFIX \
            -DCMAKE_PREFIX_PATH=$CUSTOM_PREFIX \
            -DCMAKE_CXX_FLAGS=-Wall \
            -DCMAKE_VERBOSE_MAKEFILE=TRUE \
            -DCMAKE_BUILD_TYPE=debug
 
        # uncomment the following two lines to make builds wait after
        # configuration step, so that the user can check configure output
        #echo "Press <ENTER> to continue..."
        #read userinput
 
        # Note: To speed up compiling, change 'make -j2' to 'make -jx',
        #   where x is your number of processors +1
        nice make -j`nproc` && make install
        RETURN=$?
        cs
        return ${RETURN}
}

# Same as above but without the make
function cmakeconfig {
        local srcFolder current

        # get srcFolder for current dir
        srcFolder=`pwd | sed -e s,$KDE_BUILD,$KDE_SRC,`
        # we are in the src folder, change to build directory
        # Alternatively, we could just use makeobj in the commands below...
        current=`pwd`
        if [ "$srcFolder" = "$current" ]; then
                cb
        fi
        # To enable tests, uncomment -DKDE4_BUILD_TESTS=TRUE.
        # If you are building to debug or develop, it is recommended to
        # uncomment the line -DCMAKE_BUILD_TYPE=debugfull. Be sure to move
        # it above the -DKDE4_BUILD_TESTS=TRUE line if you leave that one
        # commented out. You can also change "debugfull" to "debug" to save
        # disk space.
        # Some distributions use a suffix for their library directory when
        # on x86_64 (i.e. /usr/lib64) if so, you have to add -DLIB_SUFFIX=64
        # for example.
        # Some distributions use a different home folder for KDE4,
        # e.g. ~/.kde4. In that case you have to append
        # -DKDE_DEFAULT_HOME=.kde4

        cmake "$srcFolder" $@ \
            -DCMAKE_INSTALL_PREFIX=$CUSTOM_PREFIX \
            -DCMAKE_PREFIX_PATH=$CUSTOM_PREFIX \
            -DCMAKE_CXX_FLAGS=-Wall \
            -DCMAKE_BUILD_TYPE=debug

        # uncomment the following two lines to make builds wait after
        # configuration step, so that the user can check configure output
        #echo "Press <ENTER> to continue..."
        #read userinput

        # Note: To speed up compiling, change 'make -j2' to 'make -jx',
        #   where x is your number of processors +1
        RETURN=$?
        cs
        return ${RETURN}
}
# A function to easily change to the build directory.
# Usage: cb KDE/kdebase
#   will change to $KDE_BUILD/KDE/kdebase
# Usage: cb:
#   will simply go to the build folder if you are currently in a src folder
#   Example:
#     $ pwd
#     /home/user/src/KDE/kdebase
#     $ cb && pwd
#     /home/user/build/KDE/kdebase
function cb {
        local dest

        # Make sure build directory exists.
        mkdir -p "$KDE_BUILD"

        # command line argument
        if test -n "$1"; then
                cd "$KDE_BUILD/$1"
                return
        fi
        # substitute src dir with build dir
        dest=`pwd | sed -e s,$KDE_SRC,$KDE_BUILD,`
        if test ! -d "$dest"; then
                # build directory does not exist, create
                mkdir -p "$dest"
        fi
        cd "$dest"
}

# Change to the source directory.  Same as cb, except this
# switches to $KDE_SRC instead of $KDE_BUILD.
# Usage: cs KDE/kdebase
#   will change to $KDE_SRC/KDE/kdebase
# Usage: cs
#   will simply go to the source folder if you are currently in a build folder
#   Example:
#     $ pwd
#     /home/user/build/KDE/kdebase
#     $ cs && pwd
#     /home/user/src/KDE/kdebase

function cse {
    cd "$KDE_SRC/kdesupport/emerge"
    if [ $1 eq "g" ]; then
        gvim -S emerge.vim
    fi
}
function cs {
        local dest current

        # Make sure source directory exists.
        mkdir -p "$KDE_SRC"

        # command line argument
        if test -n "$1"; then
                cd "$KDE_SRC/$1"
        else
                # substitute build dir with src dir
                dest=`pwd | sed -e s,$KDE_BUILD,$KDE_SRC,`
                current=`pwd`
                if [ "$dest" = "$current" ]; then
                        cd "$KDE_SRC"
                else
                        cd "$dest"
                fi
        fi
}
 
# Add autocompletion to cs function
function _cs_scandir
{
        local base ext

        base=$1
        ext=$2
        if [ -d $base ]; then
                for d in `ls $base`; do
                        if [ -d $base/$d ]; then
                                dirs="$dirs $ext$d/"
                        fi
                done
        fi
}

function _cs()
{
        local cur dirs
        _cs_scandir "$KDE_SRC"
        _cs_scandir "$KDE_SRC/KDE" "KDE/"
        COMPREPLY=()
        cur="${COMP_WORDS[COMP_CWORD]}"
        COMPREPLY=( $(compgen -W "${dirs}" -- ${cur}) )
}

export QT_MESSAGE_PATTERN="`echo -e "\033[32m%{time h:mm:ss.zzz}%{if-category}\033[32m %{category}:%{endif} %{if-debug}\033[35m%{function}%{endif}%{if-warning}\033[33m%{backtrace depth=3}%{endif}%{if-critical}\033[31m%{backtrace depth=3}%{endif}%{if-fatal}\033[31m%{backtrace depth=3}%{endif}\033[0m %{message}"`"

# Remove comment on next line to enable cs autocompletion
#complete -F _cs cs
