#!/bin/bash

PACKAGE=qt-x11-free-3.1.1

LOCALHOME=/usr/local
SRCHOME=$LOCALHOME/src
SOURCESDIR=$SRCHOME/sources
SOURCE=${SOURCESDIR}/${PACKAGE}.tar.bz2
SOURCE2=${SOURCESDIR}/Xinerama.tar.bz2
PATCHESDIR=${SOURCESDIR}/${PACKAGE}-patches

QTDIR=/usr/local/src/qt-x11-free-3.1.1
cd $SRCHOME
if [ -d $PACKAGE ]
then
  echo Removing old install
  rm -rf $PACKAGE
fi
#if [ ! -e /usr/lib/libXft.so ]
#then
#  ln -s /usr/lib/libXft.so.2.0 /usr/lib/libXft.so ]
#fi
if [ !-d /usr/local/qt ]
then
  mkdir -p /usr/local/qt
fi

echo Unpacking Source
tar jxf ${SOURCE}
pushd $PACKAGE
echo Installing Patches
patch -p1 < ${PATCHESDIR}/motif.patch
patch -p1 < ${PATCHESDIR}/configtest.patch

# May have to do something re Xinerama
tar jxf $SOURCE2
gcc -fno-use-cxa-atexit -fPIC -c -I/usr/X11R6/include/X11/extensions -I. Xinerama.c
ar r libXinerama.a Xinerama.o

find . -type d -name CVS|xargs rm -rf
find mkspecs -name "*.stdc++" | xargs rm -f
perl -pi -e "s,\@QMAKE_CFLAGS\@,\@QMAKE_CFLAGS\@ -O2,g" qmake/GNUmakefile.in
perl -pi -e "s,-fpic,-fPIC,g" mkspecs/*/qmake.conf
perl -pi -e "s|-Wl,-rpath,| |" mkspecs/*/qmake.conf
find . -type d -name CVS | xargs rm -rf
export LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH
export PATH=$QTDIR/bin:$PATH
INCLUDES="-I/usr/include/fontconfig -I/usr/include/Xft2"

perl -pi -e "s,-O2,$INCLUDES $OPTFLAGS -fno-use-cxa-atexit -fno-exceptions,g" mkspecs/*/qmake.conf

QTDEST=/usr/local/lib/qt
export QTDEST

echo yes|./configure -verbose \
  -prefix /usr/local/qt -release -shared -qt-gif \
  -system-zlib -system-libpng -system-libmng -system-libjpeg -xft \
  -no-g++-exceptions -enable-opengl -enable-tools -enable-kernel \
  -enable-widgets -xrender -xkb \
  -enable-dialogs -enable-iconview -enable-workspace -enable-network \
  -enable-canvas -enable-table -enable-xml -enable-sql -qt-style-motif \
  -plugin-sql-mysql -plugin-sql-psql -plugin-sql-odbc -qt-style-cde \
  -qt-style-motifplus -qt-style-platinum -qt-style-sgi -qt-style-windows \
  -qt-style-compact -qt-style-interlace -qt-style-common -qt-imgfmt-png \
  -qt-imgfmt-jpeg -qt-imgfmt-mng \
  -sm -xinerama -stl -thread -I/usr/include/Xft2 \
  -I/usr/include/pgsql -I/usr/include/pgsql/server \
  -I/usr/include/pgsql/internal -lpq -I/usr/include/mysql -L/usr/lib/mysql \
  -lmysqlclient -lodbc

echo
echo making synmlinks and src-qmake
make symlinks src-qmake
echo Making SQL Plugin Makefiles
echo PostgreSQL ...
cd plugins/src/sqldrivers/psql
qmake -o Makefile "INCLUDEPATH+=/usr/include/pgsql /usr/include/pgsql/server /usr/include/pqsql/internal" "LIBS+=-lpq" psql.pro
echo MySql ...
cd ../mysql
qmake -o Makefile "INCLUDEPATH+=/usr/inlcludemysql" "LIBS+=-L/usr/lib/mysql -lmysqlclient" mysql.pro
echo ODBC ...
cd ../odbc
qmake -o Makefile "LIBS+=-lodbc" odbc.pro
cd ../../../..
#make src-moc sub-src sub-tools
#for i in designer linguist assistant qtconfig; do
#  make -C tools/$i
#done

echo Doing Main Build
make && make install
unalias cp
cp lib/*\.a /usr/local/qt/lib
