# Maintainer: Calvin Morrison <mutantturkey@gmail.com>

pkgname=trinity-kdebase
pkgver=3513
pkgrel=1
arch=('i686' 'x86_64')
url='http://www.trinitydesktop.org'
license=('GPL')
groups=('trinity-base')
pkgdesc="Trinity - kdebase"
depends=('hal'
         'trinity-dbus-1-qt3'
         'trinity-arts'
         'trinity-qt3'
         'trinity-pyqt3'
         'trinity-dbus-tqt-1'
         'trinity-dbus-tqt'
         'trinity-kdelibs'
         'openexr'
         'libxtst'
         'libxkbfile'
         'xorg-server'
         'xorg-xset'
         'xorg-xsetroot'
         'xorg-xprop'
         'xorg-xmessage'
         'xorg-xinit'
         'xorg-iceauth')
makedepends=('pkgconfig' 'cmake' 'autoconf' 'imake' 'xorg-bdftopcf')
optdepends=('desktop-file-utils: Command line utilities for working with desktop entries'
        'pciutils: PCI bus configuration space access library and tools'
        'xscreensaver: Screen saver and locker for the X Window System')
options=('libtool' '!strip')
source=('http://mirror.its.uidaho.edu/pub/trinity/releases/3.5.13/kdebase-3.5.13.tar.gz' 'dbusfix.diff' 'kdm-trinity')
md5sums=('d1f7e1f94eeb46b981fe1ba144179840'
         'c9ce810c281002440a6de8cfd8735d07'
         '523838d16b38fc62445863ca762c17f8')

_qtdir=/opt/qt
_prefix="/opt/trinity"

_tdesktop() {
    # create/install desktop
    install -d -m755 ${pkgdir}/etc/X11/sessions
    echo '[Desktop Entry]' > ${pkgdir}/etc/X11/sessions/trinity.desktop
    echo 'Encoding=UTF-8' >> ${pkgdir}/etc/X11/sessions/trinity.desktop
    echo 'Type=XSession' >> ${pkgdir}/etc/X11/sessions/trinity.desktop
    echo "Exec=${_prefix}/bin/startkde" >> ${pkgdir}/etc/X11/sessions/trinity.desktop
    echo "TryExec=${_prefix}/bin/startkde" >> ${pkgdir}/etc/X11/sessions/trinity.desktop
    echo 'Name=Trinity' >> ${pkgdir}/etc/X11/sessions/trinity.desktop
    echo 'Comment=The Trinity Desktop Environment. A powerful Open Source graphical desktop environment' >> ${pkgdir}/etc/X11/sessions/trinity.desktop
    chmod 0755 ${pkgdir}/etc/X11/sessions/trinity.desktop
}

build() {

  msg "Setting PATH, CMAKE and Trinity Environment variables"
  if [[ -r /etc/profile.d/qt3.sh ]]; then
    . /etc/profile.d/qt3.sh
    [[ -n $QTDIR ]] && _qtdir=$QTDIR
  else
    [[ ${PATH%%:*} =~ ${_qtdir}/bin ]] || export PATH=${_qtdir}/bin:$PATH
  fi
  export CMAKE_PREFIX_PATH=/opt/qt:/opt/trinity
  export CMAKE_INCLUDE_PATH=/opt/trinity/include/dbus-1-tqt/:/opt/trinity/include/dbus-1.0/:/opt/trinity/include:/opt/trinity/include/libkrandr
  export LD_LIBRARY_PATH=/opt/trinity/lib:/opt/trinity/lib/kde3:/opt/qt/lib:$LD_LIBRARY_PATH
  export PKG_CONFIG_PATH=/opt/trinity/lib/pkgconfig:/opt/qt/lib/pkgconfig:$PKG_CONFIG_PATH
  echo $PKG_CONFIG_PATH

  patch -N ${srcdir}/kdebase/kioslave/media/mediamanager/halbackend.h < ${srcdir}/dbusfix.diff 
  cd $srcdir

  msg "Creating out-of-source build directory: ${srcdir}/build"
  mkdir -p build
  cd build

  msg "Starting cmake..."
  cmake ${srcdir}/kdebase --debug-output \
    -DCMAKE_INSTALL_PREFIX=${_prefix} \
    -DCMAKE_VERBOSE_MAKEFILE=ON \
    -DQT_VERSION=3 \
        -DSYSCONF_INSTALL_DIR=/etc \
        -DWITH_XFIXES=ON \
        -DWITH_HAL=ON \
        -DWITH_ARTS=ON \
        -DWITH_SASL=ON \
        -DWITH_OPENEXR=ON \
        -DWITH_XCOMPOSITE=ON \
        -DWITH_XCURSOR=ON \
        -DWITH_XRANDR=ON \
        -DWITH_XRENDER=ON \
        -DWITH_XDAMAGE=ON \
        -DWITH_SHADOW=ON \
        -DWITH_XDMCP=ON \
        -DWITH_XINERAMA=ON \
        -DWITH_XEXT=ON \
    -DWITH_PAM=ON \
    -DBUILD_ALL=ON \
    -DCMAKE_INCLUDE_DIR=/opt/trinity/include/dbus-1.0:/opt/qt/include/tqt:/opt/trinity/include/dbus-1.0
  make
#     -DWITH_QT3=ON \
#     -DQTDIR=/opt/qt \
#     -DQT_LIBRARY_DIRS=/opt/qt/lib \
}

package() {
  msg "Packaging - $pkgname-$pkgver"
#   cd ${srcdir}/${_svnmod}
  cd ${srcdir}/build

  make DESTDIR="$pkgdir/" install

  cd ${startdir}

  # install desktop and update kdmrc and Xsession
#   [[ -f trinity.desktop ]] && {
#     install -d -m755 ${pkgdir}/etc/X11/sessions
#     install -m644 trinity.desktop ${pkgdir}/etc/X11/sessions
#   }
  _tdesktop
  [[ -f ${pkgdir}/etc/X11/sessions/trinity.desktop ]] || msg "ERROR - Install Failure: ${pkgdir}/etc/X11/sessions/trinity.desktop"

  # set kdm directory
  _kdmdir=${_prefix}/share/config/kdm
  [[ -d ${pkgdir}/${_kdmdir} ]] || install -d -m755 ${pkgdir}/${_kdmdir}

  # install kdmrc
  [[ -f kdmrc ]] && install -m644 kdmrc ${pkgdir}/${_kdmdir}

  # update the Xsession file
  [[ -f Xsession ]] && install -m755 Xsession ${pkgdir}/${_kdmdir}

  # create the starttrinity link
  [[ -d ${pkgdir}/usr/bin ]] || mkdir -p ${pkgdir}/usr/bin
  cd ${pkgdir}/usr/bin
  ln -sf /opt/trinity/bin/startkde starttrinity

  cd ${startdir}
  install -D -m755 ./kdm-trinity "${pkgdir}"/etc/rc.d/kdm-trinity	

}