#!/bin/bash

# qt.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
#
# 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 $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t -pipe" ;;
   *)       export SLKCFLAGS="" ;;
esac

# Extract source:
tar xvvf $CWD/qt-$VERSION.tar.xz 
cd qt-$VERSION || exit 1
slackhousekeeping

# Apply ARM-specific patch from Debian's diff.
tar xvvf $PORTCWD/sources/qt*debian*z || exit 1
auto_apply_patch debian/patches/30_webkit_unaligned_access.diff || exit 1
auto_apply_patch debian/patches/92_armel_gcc43_valist_compat.diff || exit 1

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

# Just skip the patches it complains about...  easy enough.
## Get rid of already applied patches (if any):
#if [ -r $CWD/remove-already-applied-patches.diff.gz ]; then
#  zcat $CWD/remove-already-applied-patches.diff.gz | patch -p1 --verbose || exit 1
#fi

# apply_patches looks for a ".svn" directory, which doesn't exist in the used snapshot.
# removing the check from apply_patches to not try to apply already applied patches
sed -i "s| test -d .svn &&||" apply_patches

# This is only needed/wanted for qt-copy (the r###### version) that we're
# building right now. It might require manual intervention, so don't walk
# away during the build
[ -x ./apply_patches ] && ./apply_patches

# Fix path to mysql header
zcat $CWD/qt.mysql.h.diff.gz | patch -p1 || exit 1

# Allow building with distcc unless we've flagged it not to:
# Don't build this with distcc.  I think it causes problems.
#[ ! -f $PORTCWD/.no-distcc ] && export CC="distcc gcc" CXX="distcc g++"

# Configure:
export CFLAGS="$SLKCFLAGS"
export CXXFLAGS="$SLKCFLAGS"
export OPENSOURCE_CXXFLAGS="$SLKCFLAGS"
./configure \
   -confirm-license \
   -opensource \
   -prefix /usr/lib${LIBDIRSUFFIX}/qt \
   -qt-gif \
   -fast \
   -system-libpng \
   -system-libjpeg \
   -system-zlib \
   -system-sqlite \
   -plugin-sql-sqlite \
   -dbus \
   -webkit \
   -no-phonon \
   -nomake examples \
   -nomake demos \
   -no-separate-debug-info \
   -no-pch || exit 1

# Sometimes a failure happens when parallelizing make.  Try again if make fails,
# but make a failure the second time around (single threaded) a fatal error:
make $NUMJOBS || make || exit 1

# Install into package:
make install INSTALL_ROOT=$PKG || exit 1

# Put back as shipped - dunno why - Slackware does it.
install -vpm644 $TMPBUILD/g++.conf mkspecs/common/

# The infamous qt -> qt-${VERSION} link that keeps the full path out of .la files:
( cd $PKG/usr/lib${LIBDIRSUFFIX} ; ln -sf qt qt-${VERSION} )

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

# Add profile scripts
mkdir -p $PKG/etc/profile.d
sed -e "s#usr/lib/#usr/lib${LIBDIRSUFFIX}/#g" $CWD/profile.d/qt4.sh \
  > $PKG/etc/profile.d/qt4.sh
sed -e "s#usr/lib/#usr/lib${LIBDIRSUFFIX}/#g" $CWD/profile.d/qt4.csh \
  > $PKG/etc/profile.d/qt4.csh
chmod 0755 $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/lib${LIBDIRSUFFIX}/qt/bin
  for file in * ; do
    ( cd $PKG/usr/bin ; ln -sf /usr/lib${LIBDIRSUFFIX}/qt/bin/$file . )
  done
)

mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig
( cd $PKG/usr/lib${LIBDIRSUFFIX}/qt/lib/pkgconfig
  for file in *.pc ; do
    ( cd $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig ; ln -sf /usr/lib${LIBDIRSUFFIX}/qt/lib/pkgconfig/$file . )
  done
)

# Add a documentation directory:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a *GPL_EXCEPTION* FAQ* INSTALL KNOWN* LICENSE* README* changes-* \
  $PKG/usr/doc/$PKGNAM-$VERSION
( cd $PKG/usr/doc/$PKGNAM-$VERSION
  ln -sf /usr/lib${LIBDIRSUFFIX}/qt/doc/html .
)

# 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
slackmp         # run makepkg -l y -c n

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