#!/bin/bash

# build-qt.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Copyright  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.

#
# This script builds Qt, packages it without WebKit, upgrades the machine to
# a WebKit-less Qt, builds a WebKit package, merges the two packages, and
# finally upgrades the machine to a combined package.
#

##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## Notes: Set the package BUILD and VERSION in qt-nowebkit/arm/build ##
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


# Temporary build locations:
TMPBUILD=/tmp/build-qt-combiner
PKG=/tmp/package-qt-combiner
rm -rf $TMPBUILD $PKG
mkdir -vpm755 $TMPBUILD $PKG

# Any left over temporary packages in case the build failed:
removepkg qtwebkit qtnowebkit

# The SlackBuilds called from here will put their packages into here:
rm -rf /tmp/qt-packages
mkdir -vpm755 /tmp/qt-packages

echo "==========================================================="
echo "============== Building QT without Web Kit ================"
echo "==========================================================="

# Build Qt and package it without WebKit:
( cd qt-nowebkit
  dbuild )
if [ ! -r /tmp/qt-packages/qtnowebkit.t?z ]; then
  echo "FATAL:  Failed to build Qt package without WebKit."
  exit 1
fi

# Upgrade system to Qt (without WebKit):
/sbin/upgradepkg --reinstall --install-new /tmp/qt-packages/qtnowebkit.t?z

echo "==========================================================="
echo "============== Building QT __with__ Web Kit ==============="
echo "==========================================================="

# Build standalone WebKit package (qtwebkit):
( cd qtwebkit
  dbuild )
if [ ! -r /tmp/qt-packages/qtwebkit.t?z ]; then
  echo "FATAL:  Failed to build QtWebKit standalone package."
  exit 1
fi

echo "==========================================================="
echo "================Building combined package=================="
echo "==========================================================="

# We don't set this earlier, otherwise the individual component
# packages' arm/build scripts would have their variables overwritten.
export PKGNAM=qt
export PKGARCH=arm
# Take the version of qt and its build from the 'qt-nowebkit' script:
eval $( cat qt-nowebkit/arm/build | egrep "BUILD=|VERSION=" )
if [ -z "$BUILD" -o -z "$VERSION" ]; then
   echo "Failed to find the version or build of the qt-nowebkit package."
   echo "Check its arm/build script"
   exit 1
fi
export VERSION BUILD
export PKGSERIES=l
export SLACKPACKAGE=$PKGNAM-$VERSION-$PKGARCH-$BUILD.txz

# Combine the packages:
cd $PKG
/sbin/explodepkg /tmp/qt-packages/qtwebkit.t?z
mv -fv install/doinst.sh{,.qtwebkit}
/sbin/explodepkg /tmp/qt-packages/qtnowebkit.t?z
# A useful marker:
echo "## Begin qtwebkit symlinks" >> install/doinst.sh
grep '^(' install/doinst.sh.qtwebkit >> install/doinst.sh
rm -fv install/doinst.sh.qtwebkit
mv -fv usr/doc/qtwebkit-* usr/doc/qt-*

# Remove the old packages:
removepkg qtwebkit qtnowebkit

# Load in the Slackware ARM packaging library so that we
# can use the library functions to package up the contents
# of the two packages:
source /usr/share/slackdev/buildkit.sh

# 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
# We have the doinst scripts from the 2xSlackBuild runs
# The final slack-desc will come from the 'qtnowebkit' package since was 'explodepkg'd last.
#slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

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