#!/bin/sh
# Build Qt

VERSION=3.0.4
ARCH=i386
BUILD=1

CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-qt
HTMLDOCS=$TMP/package-qt-htmldocs
EXAMPLES=$TMP/package-qt-examples

# Trust me, we avoid a lot of problems building in the target location. :-)
cd /usr/lib
# NOTE THIS NUKES THE EXISTING QT
rm -rf qt*
tar xjvf $CWD/qt-copy-$VERSION.tar.bz2
mv qt-copy-$VERSION qt
cd qt
QTDIR=`pwd`
export QTDIR
export YACC='byacc -d'
zcat $CWD/qt.cflags.diff.gz | patch -p1
zcat $CWD/qt-x11.diff.gz | patch -p1
# Fix file perms:
chown -R root.root .
find . -type f -perm 775 -exec chmod 755 {} \;
find . -type f -perm 555 -exec chmod 755 {} \;
find . -type f -perm 664 -exec chmod 644 {} \;
find . -type f -perm 444 -exec chmod 644 {} \;

# Copy the examples right away:
mkdir -p $PKG/usr/doc/qt-$VERSION
cp -a examples $PKG/usr/doc/qt-$VERSION
# These are the flags recommended by KDE:
#./configure -system-zlib -qt-gif -system-libpng -system-libjpeg -plugin-imgfmt-mng -thread -no-stl -no-xinerama -no-g++-exceptions
# And here's what we are currently using:
./configure -prefix /usr/lib/qt -docdir /usr/doc/qt-$VERSION/html -release -system-zlib -qt-gif -system-libpng -system-libjpeg -plugin-imgfmt-mng -thread -no-stl -no-xinerama -no-g++-exceptions -xft
# Build the important stuff:
make symlinks sub-src sub-tools
# Then, package things up in $PKG:
mkdir -p $PKG/usr/lib
make INSTALL_ROOT=$PKG moc-install src-install tools-install qmake-install plugins-install  

cp -a \
  FAQ INSTALL LICENSE.GPL LICENSE.QPL MANIFEST PLATFORMS README README.QT changes-3.0.4 \
  README.qt-copy \
  $PKG/usr/doc/qt-$VERSION

( cd $PKG/usr/lib
  mv qt qt-$VERSION
  ln -sf qt-$VERSION qt )

mkdir -p $PKG/usr/lib/qt-$VERSION/doc
( cd doc
  cp -a man $PKG/usr/lib/qt-$VERSION/doc
  gzip -9 $PKG/usr/lib/qt-$VERSION/doc/man/man?/* )

mkdir -p $PKG/usr/bin
( cd $PKG
  for file in assistant designer linguist lrelease lupdate moc qm2ts qmake qtconfig uic ; do
    ( cd usr/bin ; rm -rf $file )
    ( cd usr/bin ; ln -sf /usr/lib/qt/bin/$file $file )
  done
)
chown -R root.bin $PKG/usr/bin

# Strip binaries:
( cd $PKG/usr/lib/qt/bin
  strip *
  cd ../lib
  strip --strip-unneeded *.so )
find $PKG/usr/lib/qt/plugins -name "*.so" | xargs strip --strip-unneeded

mkdir -p $PKG/etc/profile.d
cat $CWD/profile.d/qt.sh > $PKG/etc/profile.d/qt.sh
cat $CWD/profile.d/qt.csh > $PKG/etc/profile.d/qt.csh
chmod 755 $PKG/etc/profile.d/*

# Put this back as shipped:
( cd $PKG/usr/lib/qt
  zcat $CWD/qt.cflags.diff.gz | patch -p1 -R )

mkdir -p $PKG/install
zcat $CWD/doinst.sh > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc

## Maybe something for later?  :-)
##
## HTML documentation and examples are HUGE so they go in their own packages.
#rm -rf $HTMLDOCS $EXAMPLES
#mkdir -p $HTMLDOCS/usr/doc/qt-$VERSION $EXAMPLES/usr/doc/qt-$VERSION
#mv $PKG/usr/doc/qt-$VERSION/html $HTMLDOCS/usr/doc/qt-$VERSION
#mv $PKG/usr/doc/qt-$VERSION/examples $EXAMPLES/usr/doc/qt-$VERSION
#cd $HTMLDOCS
#makepkg -l y -c n $TMP/qt-htmldocs-$VERSION-noarch-$BUILD.tgz
#cd $EXAMPLES
#makepkg -l y -c n $TMP/qt-examples-$VERSION-noarch-$BUILD.tgz

# Build package:
cd $PKG
makepkg -l y -c n $TMP/qt-copy-$VERSION-$ARCH-$BUILD.tgz