# $Id: PKGBUILD 4788 2008-07-10 10:21:45Z pierre $
# Maintainer: Tobias Powalowski <tpowa@archlinux.org>
# Contributor: John Proctor <jproctor@prium.net>
pkgname=qt3
pkgver=3.3.8
pkgrel=9
pkgdesc="The QT gui toolkit."
arch=(i686 x86_64)
license=('GPL')
url="http://www.trolltech.com/products/qt/index.html"
pkgfqn=qt-x11-free-${pkgver}
install=qt.install
depends=('libpng' 'libjpeg' 'libxmu' 'libxcursor' 'libxinerama' 'mesa' \
	 'libxft' 'libxrandr')
makedepends=('mysql' 'postgresql>=8.2.3' 'sqlite3' 'unixodbc' 'libmng')
source=(ftp://ftp.trolltech.com/qt/source/${pkgfqn}.tar.bz2 qt.profile \
        qt-copy-kde-patches.tar.bz2 qt-patches.tar.bz2 utf8-bug-qt3.diff \
	qt-font-default-subst.diff mysql.patch)
options=(!libtool)
md5sums=('cf3c43a7dfde5bfb76f8001102fe6e85'
         'f72d1eb4eb49b9a9467c1f6035194266'
         'f2a2dbdbfee9422c90efc3ef3f86197c'
         'c8937968e9807b3cdcdf0505728d99ce'
         'f6b3b39040f2b8f19ba1cf1445468c28'
         '9370d82e85f2c799335ed0dcc1d53189'
         '7d40ed1bd40d33d8b9b27a2076a5d22a')

# qt-copy-kde-patches come from http://websvn.kde.org/trunk/qt-copy/patches/
# other qt-patches come from fedora and gentoo

build() {
  unset QMAKESPEC
  export QTDIR=$startdir/src/$pkgfqn
  export PATH=${QTDIR}/bin:${PATH}
  export LD_LIBRARY_PATH=${QTDIR}/lib:${LD_LIBRARY_PATH}
  export QMAKESPEC=$QTDIR/mkspecs/linux-g++
  cd $startdir/src/$pkgfqn
  # apply qt patches from kde.org
  for i in ../qt-copy-kde-patches/*; do
    patch -Np0 -i $i || return 1
  done
  # apply other qt patches and one security fix from debian/gentoo
  for i in ../qt-patches/*; do
    patch -Np1 -i $i || return 1
  done
  # fix utf8 bug
  patch -Np0 -i ../utf8-bug-qt3.diff || return 1
  # fix asia fonts
  patch -Np0 -i ../qt-font-default-subst.diff || return 1
  # fix segfaults on exit when using mysql DB driver
  patch -Np0 -i ../mysql.patch || return 1
  # start compiling qt
  sed -i 's|-cp -P -f|-cp -L -f|' qmake/Makefile.unix
  rm -rf doc/html examples tutorial
  sed -i "s|sub-tutorial sub-examples||" Makefile
  sed -i "s|-O2|$CXXFLAGS|" mkspecs/linux-g++/qmake.conf
  sed -i "s|-I. |$CXXFLAGS -I. |" qmake/Makefile.unix
  sed -i "s|read acceptance|acceptance=yes|" configure

 # remove unwanted mkspecs
  rm -rf mkspecs/{*aix*,*bsd*,cygwin*,dgux*,darwin*,hpux*,hurd*,irix*,lynxos*,macx*,qnx*,reliant*,sco*,solaris*,tru64*,unixware*,win32*}

  if [ "$CARCH" = "x86_64" ]; then
      export ARCH="-64"	
    else unset ARCH
  fi

  ./configure -prefix /opt/qt -platform linux-g++$ARCH \
    -system-zlib -qt-gif -release -shared -sm -nis -thread -stl \
    -{system-lib,plugin-imgfmt-}{png,jpeg,mng} \
    -no-g++-exceptions -plugin-sql-{mysql,psql,sqlite,odbc}

  # fix /opt/qt/lib path
  [ "$CARCH" = "x86_64" ] && sed -i "s|/opt/qt/lib64|/opt/qt/lib|g" $startdir/src/$pkgfqn/src/Makefile
  [ "$CARCH" = "x86_64" ] && sed -i "s|/opt/qt/lib64|/opt/qt/lib|g" $startdir/src/$pkgfqn/tools/designer/designer/Makefile
  [ "$CARCH" = "x86_64" ] && sed -i "s|/opt/qt/lib64|/opt/qt/lib|g" $startdir/src/$pkgfqn/tools/designer/editor/Makefile
  [ "$CARCH" = "x86_64" ] && sed -i "s|/opt/qt/lib64|/opt/qt/lib|g" $startdir/src/$pkgfqn/tools/assistant/lib/Makefile
  [ "$CARCH" = "x86_64" ] && sed -i "s|/opt/qt/lib64|/opt/qt/lib|g" $startdir/src/$pkgfqn/tools/designer/uilib/Makefile

  cd $startdir/src/$pkgfqn
  make -C qmake || return 1
  cd $startdir/src/$pkgfqn/plugins/src/sqldrivers/mysql
  $startdir/src/$pkgfqn/bin/qmake -o Makefile "INCPATH+=/usr/include/mysql" "LIBS+=-L/usr/lib/mysql -lmysqlclient" mysql.pro
  cd $startdir/src/$pkgfqn/plugins/src/sqldrivers/psql
  $startdir/src/$pkgfqn/bin/qmake -o Makefile "INCPATH+=/usr/src/include /usr/include/postgresql/server" "LIBS+=-L/usr/lib -lpq" psql.pro

  cd $startdir/src/$pkgfqn
  # fix the broken makefiles
  #sed -i 's|[[:space:]]*strip.*doc/html.*$|#|g' src/Makefile
  make || return 1
  make INSTALL_ROOT=$startdir/pkg install
  rm -rf $startdir/pkg/opt/qt/{phrasebooks,templates,translations}
  sed -i "s|-L$startdir/src/$pkgfqn/lib ||g" $startdir/pkg/opt/qt/lib/*.prl
  install -D -m755 qmake/qmake $startdir/pkg/opt/qt/bin/qmake
  install -D -m755 $startdir/qt.profile $startdir/pkg/etc/profile.d/qt3.sh
  rm -f $startdir/pkg/opt/qt/mkspecs/linux-g++$ARCH/linux-g++$ARCH

  # install man pages
  mkdir -p $startdir/pkg/opt/qt/man
  cp -r $startdir/src/$pkgfqn/doc/man/{man1,man3} $startdir/pkg/opt/qt/man/
}