###
## A script to setup some needed variables and functions for KDE 4 development.
## This should normally go in the ~/.bashrc file of your kde-devel user, so
## that it is executed when a session for that user is started.
##
## Example:
##   su - kde-devel
##   kwrite ~/.bashrc
##   (then paste this file)
##   exit
##   su - kde-devel
##
## If you don't use a separate user, the first section with the
## environment variables should go into a separate file of course.

# Build environment

KDEDEV_HOME=/home/$USER/kf5
export KF5=$KDEDEV_HOME/install
export CMAKE_SYSTEM_PREFIX_PATH=/usr:/usr/local:$KF5
export CPUS=`cat /proc/cpuinfo |grep processor -c`
export CPUS="6"
export ICECREAM_MAX_JOBS=""
# KDE
export KDE_COLOR_DEBUG=1
export KDEDIR=$KDEDEV_HOME/install
export KDEHOME=$HOME/.kf5
export KDETMP=/tmp/$USER-kf5
mkdir -p $KDETMP
#export KDEVARTMP=/var/tmp/$USER-kf5
export KDEDIRS=$KDEDIR
# Qt
export QTDIR=/home/$USER/kf5/qt5/qtbase
#export QT_PLUGIN_PATH=$KDEDIR/lib/kde4/plugins:$QTDIR/plugins:$QT_PLUGIN_PATH
#export QT_PLUGIN_PATH=$KF5/lib/x86_64-linux-gnu/plugins/kf5/plugins:$KF5/lib/plugins/kf5/plugins:$KF5/plugins:$QTDIR/plugins
export QT_PLUGIN_PATH=$KF5/lib64/plugins:$KF5/lib/x86_64-linux-gnu/plugins:$KF5/lib/plugins/kf5/plugins:$KF5/lib/plugins:/home/sebas/kf5/qt5/plugins

#export QT_INSTALL_PREFIX=$QTDIR
export PKG_CONFIG_PATH=$QTDIR/lib/pkgconfig:$PKG_CONFIG_PATH
# DBus
export LD_LIBRARY_PATH=$QTDIR/lib:$KDEDIR/lib:$KDEDIR/lib/x86_64-linux-gnu
export DBUSDIR=$KDEDIR
export QML2_IMPORT_PATH=$KF5/lib64/qml:$KF5/lib/qml:$KDEDIR/lib/x86_64-linux-gnu/qml


# echo "----"
# echo $QTDIR
# echo $KDEDIR
# echo $LD_LIBRARY_PATH

#export LD_LIBRARY_PATH=$QTDIR/lib:$KDEDIR/lib:$LD_LIBRARY_PATH
#echo $LD_LIBRARY_PATH
export PKG_CONFIG_PATH=$DBUSDIR/lib/pkgconfig:$PKG_CONFIG_PATH
# Other
export PATH=$KF5/src/plasma-framework/tools:$KF5/bin:$QTDIR/bin:$KDEDIR/bin:/home/$USER/bin:/usr/lib/icecc/bin:/usr/local/bin:/usr/bin:/bin
export YACC='byacc -d'

export QT_MESSAGE_PATTERN='%{appname}(%{pid})/%{category} %{function}: %{message}'

#alias cmakekf5="cmake -DCMAKE_INSTALL_PREFIX=$KF5 -DCMAKE_SYSTEM_PREFIX_PATH=/usr:/usr/local:${KF5} -DCMAKE_MODULE_PATH=$KF5/share/cmake/modules .. "
# Why 2 times?
#LD_LIBRARY_PATH=/home/$USER/kf5/qt5/lib:/home/$USER/kf5/install/lib:/home/$USER/kf5/qt5/lib

#INSTALL_ROOT=/home/$USER/kf5/install/

#QT_INSTALL_PREFIX=/home/$USER/kdesvn/qt

# For Qt apps
#export INSTALL_ROOT=$KDEDIR

#QT_PLUGIN_PATH=/home/$USER/kf5/install/lib/plugins:/home/$USER/kf5/qt5/plugins

#echo "QT_PLUGIN_PATH $QT_PLUGIN_PATH"

PKG_CONFIG_PATH=/home/$USER/kf5/install/lib/pkgconfig:/home/$USER/kf5/qt5/lib/pkgconfig

#QTDIR=/home/$USER/kdesvn/qt

#echo "PATH1 : $PATH"

#export JAVA_HOME=/usr/lib/jvm/java-6-sun


# Runtime environment

# XDG
unset XDG_DATA_DIRS # unset XDG_DATA_DIRS, to avoid seeing kde3 files from /usr
unset XDG_CONFIG_DIRS

export GTK2_RC_FILES=/home/$USER/kdesvn/install/share/themes/oxygen-gtk/gtk-2.0/gtkrc:$GTK2_RC_FILES


export XDG_DATA_DIRS=$KF5/share:/usr/share
export XDG_CONFIG_DIRS=$KF5/etc/xdg:$XDG_CONFIG_DIRS
#export PATH=$KF5/bin:$PATH

export XDG_DATA_HOME=$HOME/.local5
export XDG_CONFIG_HOME=$HOME/.config5
export XDG_CACHE_HOME=$HOME/.cache5

export CMAKE_PREFIX_PATH=$KF5:$CMAKE_PREFIX_PATH


# build and src folders
# you might want to change these!
export KDE_BUILD=$KDEDEV_HOME/build
export KDE_SRC=$KDEDEV_HOME/src

# Make
# Tell many scripts how to switch from source dir to build dir:
export OBJ_REPLACEMENT="s#$KDE_SRC#$KDE_BUILD#"
# Use makeobj instead of make, to automatically switch to the build dir.
# If you don't have makeobj, install the package named kdesdk-scripts or
# kdesdk, or check out kdesdk/scripts from svn, or just don't set the alias
# yet.
#alias make=makeobj

##
# IMPORTANT!  Uncomment the following lines if DBus does not work. DBus is not
# working if, when you run `dbus-uuidgen --ensure && qdbus`, you get an error.
#
# alias dbusstart="eval `PATH=$DBUSDIR/bin \
#  $DBUSDIR/bin/dbus-launch --auto-syntax`"

##
# 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 {
        # Make sure build directory exists.
        mkdir -p $KDE_BUILD

        # command line argument
        if test -n "$1"; then
                cd $KDE_BUILD/$1
                return
        fi
        # substitue 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 cs {
        # Make sure source directory exists.
        mkdir -p $KDE_SRC

        # command line argument
        if test -n "$1"; then
                cd $KDE_SRC/$1
        else
                # substitue build dir with src dir
                dest=`pwd | sed -e s,$KDE_BUILD,$KDE_SRC,`
                if [ $dest = `pwd` ]; then
                cd $KDE_SRC
                else
                cd $dest
                fi
        fi
}


##
# A function to easily build the current directory of KDE.
#
# This builds only the sources in the current ~/{src,build}/KDE subdirectory.
# Usage: cs KDE/kdebase && cmakekde
#   will build/rebuild the sources in ~/src/KDE/kdebase
#

# cmake 
#     -DCMAKE_INSTALL_PREFIX=$KF5 
#     -DCMAKE_SYSTEM_PREFIX_PATH=/usr:/usr/local:${KF5} 
#     -DCMAKE_MODULE_PATH=$KF5/share/cmake/modules ..

function cmakekde {
        T1="`date +%s`"
        if test -n "$1"; then
                # srcFolder is defined via command line argument
                srcFolder=$1
        else
                # get srcFolder for current dir
                srcFolder=`pwd | sed -e s,$KDE_BUILD,$KDE_SRC,`
        fi
        # we are in the src folder, change to build directory
        # Alternatively, we could just use makeobj in the commands below...
        if [ "$srcFolder" = `pwd` ]; then
                cb
        fi
        # cmake
        #echo "cmake $srcFolder -DCMAKE_INSTALL_PREFIX=$KF5 -DCMAKE_BUILD_TYPE=debugfull"
        cmake -DCMAKE_INSTALL_PREFIX=$KF5 \
            -DCMAKE_PREFIX_PATH=$KF5 \
            -DCMAKE_SYSTEM_PREFIX_PATH=/usr:/usr/local:${KF5} \
            -DCMAKE_MODULE_PATH=$KF5/share/cmake/modules .. \
            $srcFolder &&
        make -j${CPUS} && \
        make install;
        T2="`date +%s`"
        TT=`expr $T2 - $T1`
        echo "Build finished in $TT seconds using $JOBS concurrent jobs."
        export TT

}

alias cmakekf5="cmakekde"
alias tp2="plasma-shell 2>&1 |g -v Glib"
alias ps2="plasma-shell 2>&1 |grep -v Glib|grep -v QPainter"
alias pw2="plasma-shell --windowed 2>&1 |grep -v Glib|grep -v QPainter"

EXTRAPROMPT="[kf5] "

unset INSTALL_ROOT
unset QT_INSTALL_PREFIX
#export DISPLAY=:1
eval "$(gpg-agent --daemon)"

#echo "PATH $PATH"
echo "Happy Frameworks 5 hacking!"
