#!/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=armv5te -pipe -fno-strict-volatile-bitfields" 
            export SLKCONFARGS="-opengl desktop -no-egl" ;;
   *)       export SLKCFLAGS="" ;;
esac

# Extract source:
tar xf $CWD/qt-everywhere-opensource-src-$VERSION.tar.xz     # For qt releases
#tar xf $PORTCWD/sources/qt-everywhere-opensource-src-$VERSION.tar.xz  # For qt releases
cd qt-everywhere-opensource-src-* || exit 1
slackhousekeeping

# Apply ARM-specific or critical bug fixes from Debian's diff:
tar xvvf $PORTCWD/sources/qt*debian*z || exit 1
for ptch in \
 Add_support_for_QT_USE_DRAG_DISTANCE_env_var.patch \
 92_armel_gcc43_valist_compat.diff ; do
  auto_apply_patch debian/patches/$ptch || exit 1
done

# 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

# Fix path to mysql header
auto_apply_patch $CWD/qt.mysql.h.diff.gz || exit 1

# gcc doesn't support flag "-fuse-ld=gold":
auto_apply_patch $CWD/qt.ld-gold.patch.gz || exit 1

# Stupid idea - remove it:
auto_apply_patch $CWD/qt.webkit-no_Werror.patch.gz || exit 1

# Fix cursor position bug within tables:
zcat $CWD/qt.ca89c49.cursor.position.diff.gz | patch -p1 --verbose || exit 1

# Fix assistant crash:
zcat $CWD/qt.assistant.memcpy-crash.diff.gz | patch -p0 --verbose || exit 1

# Fix webkit compiling with recent glib:
zcat $CWD/qt.webkit.glib.diff.gz | patch -p1 --verbose || exit 1

# Some patches from upstream:
zcat $CWD/QElfParser-fix-type-of-sh_size.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/QTBUG-14724_close_orphaned_file_descriptors_after_printing.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/QTBUG-21900_Buttons_in_Qt_applications_not_clickable_when_run_under_gnome-shell.patch.gz | patch -p1 --verbose || 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 \
   -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} )

# We said no phonon, so it built phonon headers which conflict with 
# the l/phonon package:
rm -rvf $PKG/usr/lib/qt/include/phonon

# Add a missing Qt.pc which is needed by KDE applications:
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/qt/lib/pkgconfig
cat <<EOF > $PKG/usr/lib${LIBDIRSUFFIX}/qt/lib/pkgconfig/Qt.pc
prefix=/usr/lib${LIBDIRSUFFIX}/qt
bindir=\${prefix}/bin
datadir=\${prefix}
docdir=\${prefix}/doc
headerdir=\${prefix}/include
importdir=\${prefix}/imports
libdir=\${prefix}/lib
moc=\${bindir}/moc
plugindir=\${prefix}/plugins
qmake=\${bindir}/qmake
sysconfdir=\${prefix}/etc/settings
translationdir=\${prefix}/translations

Name: Qt
Description: Qt Configuration
Version: $VERSION
EOF

# libjscore is used internally.  Prevent a false dependency on this in the .la and .pc files:
sed -i "s,-ljscore,,g" $PKG/usr/lib${LIBDIRSUFFIX}/qt/lib/*.la $PKG/usr/lib${LIBDIRSUFFIX}/qt/lib/pkgconfig/*.pc
sed -i "s,-L../JavaScriptCore/release,,g" $PKG/usr/lib${LIBDIRSUFFIX}/qt/lib/*.la $PKG/usr/lib${LIBDIRSUFFIX}/qt/lib/pkgconfig/*.pc

# libwebcore is used internally.  Prevent a false dependency on this in the .la and .pc files:
sed -i "s#-L/usr/lib${LIBDIRSUFFIX}/qt/lib -L../../WebCore/release -L../../JavaScriptCore/release -L/usr/X11R6/lib$LIBDIRSUFFIX  -lwebcore##g" $PKG/usr/lib${LIBDIRSUFFIX}/qt/lib/libQtWebKit.la $PKG/usr/lib${LIBDIRSUFFIX}/qt/lib/pkgconfig/QtWebKit.pc

# 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 menu entries for all those hidden but great Qt applications:
# Qt logo:
convert $PKG/usr/lib$LIBDIRSUFFIX/qt/doc/src/images/qt-logo.png  -resize 48x48 $PKG/usr/share/icons/hicolor/48x48/apps/qt-logo.png
convert $PKG/usr/lib$LIBDIRSUFFIX/qt/doc/src/images/qt-logo.png  -resize 48x48 $PKG/usr/share/icons/hicolor/48x48/apps/qt-logo.png
# Assistant icons
install -p -m644 -D tools/assistant/tools/assistant/images/assistant.png $PKG/usr/share/icons/hicolor/32x32/apps/assistant.png
install -p -m644 -D tools/assistant/tools/assistant/images/assistant-128.png $PKG/usr/share/icons/hicolor/128x128/apps/assistant.png
# Designer icon
install -p -m644 -D tools/designer/src/designer/images/designer.png $PKG/usr/share/icons/hicolor/128x128/apps/designer.png
# Linguist icons
for icon in tools/linguist/linguist/images/icons/linguist-*-32.png ; do
  size=$(echo $(basename ${icon}) | cut -d- -f2)
  install -p -m644 -D ${icon} $PKG/usr/share/icons/hicolor/${size}x${size}/apps/linguist.png
done

# And the .desktop files
mkdir -p $PKG/usr/share/applications
cat <<EOF > $PKG/usr/share/applications/designer.desktop
[Desktop Entry]
Name=Qt4 Designer
GenericName=Interface Designer
Comment=Design GUIs for Qt4 applications
Exec=designer
Icon=designer
MimeType=application/x-designer;
Terminal=false
Encoding=UTF-8
Type=Application
Categories=Qt;Development;
EOF
cat <<EOF > $PKG/usr/share/applications/assistant.desktop
[Desktop Entry]
Name=Qt4 Assistant
Comment=Shows Qt4 documentation and examples
Exec=assistant
Icon=assistant
Terminal=false
Encoding=UTF-8
Type=Application
Categories=Qt;Development;Documentation;
EOF
cat <<EOF > $PKG/usr/share/applications/linguist.desktop
[Desktop Entry]
Name=Qt4 Linguist
Comment=Add translations to Qt4 applications
Exec=linguist
Icon=linguist
MimeType=text/vnd.trolltech.linguist;application/x-linguist;
Terminal=false
Encoding=UTF-8
Type=Application
Categories=Qt;Development;
EOF
cat <<EOF > $PKG/usr/share/applications/qtconfig.desktop
[Desktop Entry]
Name=Qt4 Config
Comment=Configure Qt4 behavior, styles, fonts
Exec=qtconfig
Icon=qt-logo
Terminal=false
Encoding=UTF-8
Type=Application
Categories=Qt;Settings;
EOF

# 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
