#!/bin/bash

# qtwebkit.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
#
# Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015  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

export PKGNAM=qtwebkit
export VERSION=2.3.4 ## of qtwebkit, not 'qt'

# Paths to skeleton port's source & real Slackware source tree:
# qtwebkit is within the 'qt' parent directory.
export CWD=$SLACKSOURCE/$PKGSERIES/qt/$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 -fstack-protector-strong -Wformat -Werror=format-security -fno-strict-volatile-bitfields -D_FORTIFY_SOURCE=2"
            export SLKLDFLAGS="-Wl,-z,relro -Wl,--as-needed"
            export WEBKITFLAGS="--no-force-sse2"
            export SLKCONFARGS="" ;;
   *)       export SLKCFLAGS="" ;;
esac

# Extract source:
tar xf $CWD/$PKGNAM-*.tar.xz
cd $PKGNAM-* || exit 1
slackhousekeeping

# Patch to fix compiling with gcc5:
zcat $CWD/qtwebkit.putByIndexBeyondVectorLengthWithArrayStorage.diff.gz | patch -p1 --verbose || exit 1

# Apply ARM-specific or critical bug fixes from Debian's diff:
#tar xvvf $PORTCWD/sources/qtwebkit*debian*z || exit 1
#for ptch in \
#   \
#  jsobject_ensure_indexingtype_should_handle_InterceptsGetOwn.patch \
#  fix_linking_issue_on_some_platforms_when_building_against_gstreamer_1.0.patch \
#  do_not_use_jit_on_big_endian_machines.patch \
#  fix_cloop_on_big_endian_machines.patch \
#  02_add_nostrip_for_debug_packages.diff \
#  03_hide_std_symbols.diff \
#  04_enable_debug_information.diff \
#  05_sparc_unaligned_access.diff \
#  no_gc_sections.diff \
#  defines_qt_webkit.diff \
#  glibc_hurd_mkspecs.diff \
#  hurd.diff \
#  webkit_qt_hide_symbols.diff \
#  ignore-unused-local-typedefs_error.diff \
#  hide_wtf_symbols.diff \
#  hide_jsc_symbols.diff \
#  fix_nonlinux_glibc_linkage.diff \
#  reduce_memory_usage.patch \
#  explicit_template_instantiation.patch \
#   \
#   ; do
#  auto_apply_patch debian/patches/$ptch || exit 1
#done

# Configure & build:
# For ARM we added:
# QMAKE_LFLAGS_RELEASE=\"$SLKLDFLAGS\"  QMAKE_CFLAGS_RELEASE=\"$SLKCFLAGS\" QMAKE_CXXFLAGS_RELEASE=\"$SLKLDFLAGS\"
# DEFINES+=ENABLE_JIT=0 DEFINES+=ENABLE_YARR_JIT=0 DEFINES+=ENABLE_ASSEMBLER=0

QTDIR="/usr/lib$LIBDIRSUFFIX/qt" \
Tools/Scripts/build-webkit \
   --qt \
   DEFINES+=ENABLE_JIT=0 DEFINES+=ENABLE_YARR_JIT=0 DEFINES+=ENABLE_ASSEMBLER=0 \
   --prefix=/usr/lib${LIBDIRSUFFIX}/qt \
   --release \
   --no-webkit2 \
   --makeargs="$NUMJOBS" \
   --qmakearg="CONFIG+=production_build QMAKE_LFLAGS_RELEASE=\"$SLKLDFLAGS\"   QMAKE_CFLAGS=\"$SLKCFLAGS\"   QMAKE_CFLAGS_RELEASE=\"$SLKCFLAGS\" QMAKE_CXXFLAGS_RELEASE=\"$SLKLDFLAGS\"  QMAKE_CXXFLAGS=\"$SLKCFLAGS\"" \
   $WEBKITFLAGS || exit 1

# To add support for WebP images add this to --qmakearg
# DEFINES+=HAVE_LIBWEBP=1

# Install into package:
make INSTALL_ROOT=$PKG -C WebKitBuild/Release install || exit 1

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

# libwebcore is used internally.  Prevent a false dependency on this in the .pc files:
sed -i \
  -e "s#-L/usr/lib${LIBDIRSUFFIX}/qt/lib -L../../WebCore/release -L../../JavaScriptCore/release -L/usr/X11R6/lib$LIBDIRSUFFIX  -lwebcore##g" \
  -e "s#-L/usr/lib${LIBDIRSUFFIX}/qt/lib -L../../WebCore/release -L../../JavaScriptCore/release -L/usr/X11R6/lib  -lwebcore##g" \
  -e "s#-L$TMP/qtwebkit-$VERSION/WebKitBuild/Release/Source/WebKit/release##g" \
  -e "s#-L$TMP/qtwebkit-$VERSION/WebKitBuild/Release/Source/WebCore/release##g" \
  -e "s#-L$TMP/qtwebkit-$VERSION/WebKitBuild/Release/Source/ThirdParty/ANGLE/release##g" \
  -e "s#-L$TMP/qtwebkit-$VERSION/WebKitBuild/Release/Source/JavaScriptCore/release##g" \
  -e "s#-L$TMP/qtwebkit-$VERSION/WebKitBuild/Release/Source/WTF/release##g" \
  -e "s# -lwebcore##g" \
  $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
)

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 ChangeLog $PKG/usr/doc/$PKGNAM-$VERSION
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

# 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

# The 'qt' and 'qtwebkit' packages are merged afterwards so we'll store them in
# a temporary directory:
makepkg -l y -c n /tmp/qt-packages/qtwebkit.tgz

#EOF
