#!/bin/bash

# qt.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

# Currently this file contains arch-neutral options and hopefully always
# will, but it's worth keeping an eye on it (the differ script will find them).
# The global options may be overridden here (if needed):
if [ -r $PORTCWD/local.options ]; then
   . $PORTCWD/local.options
fi

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export SLKCFLAGS="-march=armv3 -mtune=xscale" 
            export ARCH_CONFARGS="-DQT_QLOCALE_USES_FCVT" ;;
   powerpc) export SLKCFLAGS="" ;;
   sparc)   export SLKCFLAGS="" ;;
   *)       export SLKCFLAGS="" ;;
esac

# Pat says: Trust me, we avoid a lot of problems building in the target location. :-)
cd /usr/lib
rm -rf qt*

# Extract source:
tar jxvvf $CWD/qt-x11-free-$VERSION.tar.bz2
mv qt-x11-free-$VERSION qt
cd qt
slackhousekeeping
QTDIR=$PWD
export QTDIR
export YACC='byacc -d'

# Apply patches (these are from Slackware):
zcat $CWD/kubuntu_01_arabic_fonts.dpatch.gz | patch -p1 --verbose || failpatch
zcat $CWD/qt-x11.diff.gz | patch -p1 --verbose                    || failpatch
zcat $CWD/qt.mysql.h.diff.gz | patch -p1 --verbose                || failpatch
# Apply ARM-specific patch from Debian's diff. It's quite some patch to lug around when
# I only need the single patch from it, but I may want something else from it later:
# 13-July-2005: This has been incorporated upstream.
#if [ $PORTARCH = arm ]; then
#   zcat $PORTCWD/sources/qt-x11-free_3.3.3-4.1.diff.gz | patch -p1
#   patch -p1 < debian/patches/20_qlocale_compile_arm.dpatch
#fi

# Change CFLAGS (Slackware uses a patch but we'll just sed it):
( cd mkspecs/linux-g++
  install -m644 qmake.conf $TMPBUILD # we want to restore this version later
  sed -i 's/^QMAKE_CFLAGS_RELEASE.*=\(.*\)$/QMAKE_CFLAGS_RELEASE = '"$SLKCFLAGS"' \1/' qmake.conf )

# Make symlinks for the examples/docs right away:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
( cd $PKG/usr/doc/$PACKAGE-$VERSION
  ln -sf /usr/lib/$PACKAGE-$VERSION/examples .
  ln -sf /usr/lib/$PACKAGE-$VERSION/tutorial .
  ln -sf /usr/lib/$PACKAGE-$VERSION/doc/html .
  ln -sf /usr/lib/$PACKAGE-$VERSION/doc/man . )

# Configure:
echo "yes" | \
./configure $ARCH_CONFARGS \
   -prefix /usr/lib/qt \
   -release \
   -system-zlib \
   -system-libpng \
   -qt-imgfmt-png \
   -system-libmng \
   -qt-imgfmt-mng \
   -system-libjpeg \
   -qt-imgfmt-jpeg \
   -qt-gif \
   -thread \
   -stl \
   -no-g++-exceptions \
   -xft \
   -plugin-sql-mysql \
   -plugin-style-cde \
   -plugin-style-compact \
   -qt-style-motif \
   -plugin-style-motifplus \
   -plugin-style-platinum \
   -plugin-style-sgi \
   -plugin-style-windows \
   -enable-opengl || failconfig

# We're using '-i' to work around a bug where the build tries to strip html files.
# Build the important stuff:
make -i symlinks sub-src sub-tools || failmake

# Add libqt symlinks:
#( cd /usr/lib/qt/lib
#  for link in libqt.so libqt.so.3 libqt.so.`echo $VERSION | cut -f 1-2 -d .` libqt.so.${VERSION} ; do
#    ln -sf libqt-mt.so.${VERSION} $link
#  done )

# Then, package things up in $PKG:
mkdir -p $PKG/usr/lib
cp -a /usr/lib/qt $PKG/usr/lib
( cd $PKG/usr/lib
  mv qt qt-$VERSION
  ln -sf qt-$VERSION qt )

# Move misplaced docs:
( cd $PKG/usr/lib/$PACKAGE-$VERSION
  mv FAQ INSTALL LICENSE.GPL LICENSE.QPL MANIFEST PLATFORMS README* changes* \
      $PKG/usr/doc/$PACKAGE-$VERSION )

# gzip the man pages:
gzip -9 $PKG/usr/lib/qt/doc/man/man*/*

( cd $PKG/usr/lib/qt/include/private
  mkdir tmp
  cp *.h tmp
  rm *.h
  mv tmp/* .
  rmdir tmp
  cd ..
  mkdir tmp
  cp *.h tmp
  rm *.h
  mv tmp/* .
  rmdir tmp )

( cd $PKG/usr/lib/qt/bin
  cp qmake qmake.bin
  rm qmake
  mv qmake.bin qmake 
  strip * )

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

( cd $PKG/usr/lib/qt/lib
  strip --strip-unneeded *.so
  rm README )

( cd $PKG/usr/lib/qt/plugins
  strip --strip-unneeded */*.so
  cd ..
  find . -type d -name .obj -exec rm -r {} \;
  find . -type d -name .moc -exec rm -r {} \; )

( cd $PKG/usr/lib/qt
  rm -r config.tests qmake src tools translations 
  rm config.status configure Makefile )

mkdir -p $PKG/usr/lib/pkgconfig
( cd $PKG/usr/lib/pkgconfig
  ln -sf /usr/lib/qt-${VERSION}/lib/qt-mt.pc . )

# Install profile scripts:
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:
cp -f $TMPBUILD/qmake.conf $PKG/usr/lib/qt

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot    
  
# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp --prepend # run makepkg
  else
   makepkg -l y -c n --prepend $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

# Now this is redundant - reinstall it from the package to test it:
rm -rf /usr/lib/qt*

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
