#!/bin/bash

# qt.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
#
# Copyright 2006, 2007  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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

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

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
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 -I/usr/include/mysql" 
            export ARCH_CONFARGS="-DQT_QLOCALE_USES_FCVT" ;;
   powerpc) export SLKCFLAGS="" ;;
   sparc)   export SLKCFLAGS="" ;;
   *)       export SLKCFLAGS="" ;;
esac

# Unpack Debian's diff into temp build directory:
# (otherwise it ends up being in $PKG):
cd $TMPBUILD
zcat $PORTCWD/sources/qt-x11-free*.diff.gz | patch -p1 || failpatch 

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

# Extract source:
tar xvvf $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 ARM-specific patch from Debian's diff.
# This allows Doxygen to build -- probably amongst other things:
# It probably allows QT to build as well ;-)
patch -p1 < $TMPBUILD/debian/patches/10_arm_gcc4.dpatch || failpatch

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

# Nothing to see here:
( cd /usr/include && for i in mysql/* ; do ln -vfs $i . ; done )

# Configure:
echo yes | CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" ./configure "$ARCH_CONFARGS" \
 -prefix /usr/lib/qt-${VERSION} \
 -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

# /usr/lib/qt/bin/uic requires a library in here which isn't there until it's
# installed.
LD_LIBRARY_PATH=/usr/lib/qt/lib/ make $NUMJOBS sub-src symlinks sub-tools || failmake
LD_LIBRARY_PATH=/usr/lib/qt/lib/ make install INSTALL_ROOT=$PKG || failinstall

# Install into package:
#cp -a /usr/lib/qt $PKG/usr/lib

# The infamous qt -> qt-${VERSION} link that lets us cheat on the QTDIR ;-)
( cd $PKG/usr/lib ; ln -sf qt-${VERSION} qt)
#( cd $PKG/usr/lib
#  mv qt qt-$VERSION
#  ln -sf qt-$VERSION qt )

# Link the shared libraries into /usr/lib:
( cd $PKG/usr/lib
  for file in qt/lib/*.so* ; do
    ln -vsf $file .
  done
)

# Add man pages:
mkdir -p $PKG/usr/man
cp -a doc/man/* $PKG/usr/man

# Put this back as shipped:
cp -fav $TMPBUILD/qmake.conf $PKG/usr/lib/qt

# Profile stuff:
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 a ton of links to more "normal" places.  I'd just use a prefix of /usr, but it
# creates a ton of new (and ambiguously named) /usr directories...
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
)

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
)

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

# Add a documentation directory:
mkdir -p $PKG/usr/doc/${PKGNAM}-$VERSION
cp -a \
  FAQ INSTALL LICENSE.GPL* LICENSE.QPL MANIFEST PLATFORMS README* changes* \
  $PKG/usr/doc/${PKGNAM}-$VERSION
( cd $PKG/usr/doc/${PKGNAM}-$VERSION ; ln -vsf /usr/lib/qt-$VERSION/doc/html . )

# Now this is redundant - reinstall it from the package to test it:
rm -rf /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
slackmp --prepend # run makepkg

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