#!/bin/sh
# $Id: calibre.SlackBuild,v 1.144 2013/11/15 09:55:10 root Exp root $
# Copyright 2010, 2011, 2012, 2013  Eric Hameleers, Eindhoven, NL
# All rights reserved.
#
#   Permission to use, copy, modify, and distribute this software for
#   any purpose with or without fee is hereby granted, provided that
#   the above copyright notice and this permission notice appear in all
#   copies.
#
#   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 AUTHORS AND COPYRIGHT HOLDERS AND THEIR
#   CONTRIBUTORS 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.
# -----------------------------------------------------------------------------
#
# Slackware SlackBuild script 
# ===========================
# By:         Eric Hameleers <alien@slackware.com>
# For:        calibre
# Descr:      e-book library management application
# URL:        http://calibre-ebook.com/
# Needs:      icu4c, podofo;
#             And if your system already has python2.7, you will also need:
#             BeautifulSoup, dnspython, pysetuptools, python-cssselect,
#             python-cssutils, python-dateutil, python-imaging, python-lxml,
#             python-mechanize, python-psutil
# Changelog:  
# 0.7.20-1:   25/Sep/2010 by Eric Hameleers <alien@slackware.com>
#             * Initial build.
# 0.7.23-1:   12/oct/2010 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.3-1:    02/jun/2011 by Eric Hameleers <alien@slackware.com>
#             * Update. Optionally, include the required python 2.7.x plus
#               additional modules in the package.
# 0.8.6-1:    17/jun/2011 by Eric Hameleers <alien@slackware.com>
#             * Update. Added dbus-python to the internal python 2.7 libraries,
# 0.8.7-1:    26/jun/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.8-1:    03/jul/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.11-1:   28/jul/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.12-1:   30/jul/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.13-1:   06/aug/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.14-1:   12/aug/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.15-1:   19/aug/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.16-1:   26/aug/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.17-1:   02/sep/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.18-1:   09/sep/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.19-1:   16/sep/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.20-1:   24/sep/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.21-1:   30/sep/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.21-2:   12/oct/2011 by Eric Hameleers <alien@slackware.com>
#             * Add pycrypto to the internal dependencies.
#               This allows you to use the instructions found at
#               http://epubee.com/remove-drm-from-epub-on-adobe.html
# 0.8.22-1:   14/oct/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.23-1:   21/oct/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.24-1:   28/oct/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.25-1:   06/nov/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.26-1:   11/nov/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.27-1:   18/nov/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.28-1:   25/nov/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.29-1:   02/dec/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.30-1:   09/dec/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.31-1:   16/dec/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.32-1:   23/dec/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.33-1:   30/dec/2011 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.34-1:   06/jan/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.35-1:   13/jan/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.36-1:   20/jan/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.37-1:   27/jan/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.38-1:   03/feb/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.39-1:   10/feb/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.40-1:   17/feb/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.41-1:   24/feb/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.42-1:   12/mar/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.43-1:   16/mar/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.44-1:   23/mar/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.45-1:   30/mar/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.46-1:   06/apr/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.47-1:   13/apr/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.48-1:   20/apr/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.49-1:   27/apr/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.49-2:   01/may/2012 by Eric Hameleers <alien@slackware.com>
#             * Update several internal libraries. Especially cssutils needed
#               an update in order to fix the broken format conversion.
# 0.8.50-1:   04/may/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.51-1:   11/may/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.52-1:   18/may/2012 by Eric Hameleers <alien@slackware.com>
#             * Update. Man pages are no longer included as all utilities
#               have more comprehensive command line "--help". 
# 0.8.53-1:   25/may/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.54-1:   31/may/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.55-1:   06/jun/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.56-1:   15/jun/2012 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 0.8.57-1:   22/jun/2012 by Eric Hameleers <alien@slackware.com>
#             * New release. Upgraded the internal pycrypto library.
# 0.8.58-1:   29/jun/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.8.59-1:   06/jul/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.8.61-1:   25/jul/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.8.62-1:   27/jul/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.8.63-1:   03/aug/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.8.64-1:   10/aug/2012 by Eric Hameleers <alien@slackware.com>
#             * New release. For Slackware < 14, the new mtp device detection
#               has been removed.
# 0.8.65-1:   17/aug/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.8.66-1:   24/aug/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.8.67-1:   31/aug/2012 by Eric Hameleers <alien@slackware.com>
#             * New release. Updatd internal Python to 2.7.3.
#               Added new python-netifaces dependency.
# 0.8.68-1:   07/sep/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.8.69-1:   14/sep/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.8.70-1:   21/sep/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.0-1:    28/sep/2012 by Eric Hameleers <alien@slackware.com>
#             * Release went up from 0.8.x to 0.9.x.
# 0.9.1-1:    05/oct/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.2-1:    11/oct/2012 by Eric Hameleers <alien@slackware.com>
#             * New release, one day earlier than expected.
# 0.9.3-1:    19/oct/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.3-2:    20/oct/2012 by Eric Hameleers <alien@slackware.com>
#             * Forgot to add two new dependencies: python-cssselect and
#               python-psutil.
# 0.9.4-1:    26/oct/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.5-1:    02/nov/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.6-1:    11/nov/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.7-1:    16/nov/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.8-1:    30/nov/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.9-1:    07/dec/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.10-1:   14/dec/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.11-1:   21/dec/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.12-1:   28/dec/2012 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.13-1:   04/jan/2013 by Eric Hameleers <alien@slackware.com>
#             * New release. Updated the internal PyQt and SIP libraries.
#               for Slackware 13.37, compile internal versions of Qt 4.8.x
#               and libmtp 1.1.x.
#               The internal copy of Qt 4.8 libraries have the 'private
#               headers' installed, so that calibre's "qt_hack" works on
#               Slackware 13.37.
# 0.9.14-1:   11/jan/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.15-1:   18/jan/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.17-1:   01/feb/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.18-1:   08/feb/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.19-1:   15/feb/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.20-1:   22/feb/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.21-1:   01/mar/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.22-1:   08/mar/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.23-1:   15/mar/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.24-1:   22/mar/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.25-1:   29/mar/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.26-1:   05/apr/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.27-1:   12/apr/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.28-1:   26/apr/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.29-1:   04/may/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.30-1:   10/may/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.31-1:   17/may/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.32-1:   24/may/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.33-1:   31/may/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.34-1:   07/jun/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.35-1:   14/jun/2013 by Eric Hameleers <alien@slackware.com>
#             * New release. Refreshed the internal Python to 3.7.5.
# 0.9.36-1:   21/jun/2013 by Eric Hameleers <alien@slackware.com>
#             * New release. Switched the internal python-lxml to a version
#               that supports html conversion better (required for .docx
#               support).
# 0.9.37-1:   28/jun/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.38-1:   05/jul/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.39-1:   12/jul/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.40-1:   19/jul/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.42-1:   05/aug/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.43-1:   09/aug/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 0.9.44-1:   16/aug/2013 by Eric Hameleers <alien@slackware.com>
#             * New release.
# 1.0.0-1:    23/aug/2013 by Eric Hameleers <alien@slackware.com>
#             * Finally, 1.0.0 ! With python-apsw as a new dependency.
# 1.1.0-1:    30/aug/2013 by Eric Hameleers <alien@slackware.com>
#             * Update to 1.1.
# 1.2.0-1:    07/sep/2013 by Eric Hameleers <alien@slackware.com>
#             * Update to 1.2.
# 1.3.0-1:    13/sep/2013 by Eric Hameleers <alien@slackware.com>
#             * Update to 1.3.
# 1.4.0-1:    20/sep/2013 by Eric Hameleers <alien@slackware.com>
#             * Update to 1.4.
# 1.5.0-1:    26/sep/2013 by Eric Hameleers <alien@slackware.com>
#             * Update to 1.5.
# 1.6.0-1:    11/oct/2013 by Eric Hameleers <alien@slackware.com>
#             * Update to 1.6.
# 1.7.0-1:    18/oct/2013 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 1.8.0-1:    25/oct/2013 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 1.10.0-1:   08/nov/2013 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 1.11.0-1:   15/nov/2013 by Eric Hameleers <alien@slackware.com>
#             * Update.  Rebased patches/calibre_noqthack.patch and no longer
#               apply it for Slackware >= 14.1.
# 
# Run 'sh calibre.SlackBuild' to build a Slackware package.
# The package (.txz) and .txt file as well as build logs are created in /tmp .
# Install it using 'installpkg'. 
#
# -----------------------------------------------------------------------------

# Set initial variables:

PRGNAM=calibre
VERSION=${VERSION:-1.11.0}
BUILD=${BUILD:-1}
NUMJOBS=${NUMJOBS:-" -j4 "}
TAG=${TAG:-alien}

DOCS="LICENSE* Changelog* COPYRIGHT README*"

PYTHON=$(python -c 'import sys; print sys.version.split( )[0]')
SLACKVER=$(cat /etc/slackware-version |cut -d' ' -f2)

REQUIRED_QT="4.8.0"

# Only used in case we need to build our own Qt:
INT_QT=4.8.4

REQUIRED_MTP="1.1.5"

# Only used in case we need to build our own libmtp:
INT_MTP=1.1.6

REQUIRED_PYTHON="2.7"
REQUIRED_PYTHON_MIN="1"

# Only used in case we need to build our own python:
INT_PYTHON=2.7.5
INT_SETUPTOOLS=0.6c11
INT_BEAUTIFULSOUP=3.2.0
INT_DNSPYTHON=1.9.4
INT_CSSSELECT=0.7.1
INT_CSSUTILS=0.9.9
INT_DATEUTIL=1.5
INT_IMAGING=1.1.7
INT_LXML=3.2.1
INT_MECHANIZE=0.2.5
INT_NETIFACES=0.8
INT_PSUTIL=0.6.1
INT_SIP=4.14.2
INT_DBUSPYTHON=0.84.0
INT_PYCRYPTO=2.6
INT_PYQT=4.9.6
INT_APSW=3.7.5-r1

INT_MAGICK=6.8.3-8

# Where do we look for sources?
SRCDIR=$(cd $(dirname $0); pwd)
PATCHDIR=${SRCDIR}/patches

# Place to build (TMP) package (PKG) and output (OUTPUT) the program:
TMP=${TMP:-/tmp/build}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

SOURCE[0]="$SRCDIR/${PRGNAM}-${VERSION}.tar.xz"
SRCURL[0]="http://downloads.sourceforge.net/${PRGNAM}/${PRGNAM}-${VERSION}.tar.xz"

# Allow the package builder to override BUILD_QT:
if [ -z "$BUILD_QT" ]; then
  if pkg-config --exists "QtCore >= $REQUIRED_QT" ; then
    BUILD_QT="NO"
  else
    # We need to build our own qt4:
    BUILD_QT="YES"
  fi
fi

# Allow the package builder to override BUILD_MTP:
if [ -z "$BUILD_MTP" ]; then
  if pkg-config --exists "libmtp >= $REQUIRED_MTP" ; then
    BUILD_MTP="NO"
  else
    # We need to build our own libmtp:
    BUILD_MTP="YES"
  fi
fi

# Allow the package builder to override BUILD_PYTHON:
if [ -z "$BUILD_PYTHON" ]; then
  if [ "$(echo $PYTHON |cut -d. -f1,2)" = "$REQUIRED_PYTHON" -a "$(echo $PYTHON |cut -d. -f3)" -ge "$REQUIRED_PYTHON_MIN" ]; then
    BUILD_PYTHON="NO"
  else
    # We need to build our own python 2.7 plus supporting modules:
    BUILD_PYTHON="YES"
  fi
fi

if [ "$BUILD_PYTHON" = "YES" ]; then
  SOURCE[1]="$SRCDIR/Python-${INT_PYTHON}.tar.bz2"
  SRCURL[1]="http://python.org/ftp/python/${INT_PYTHON}/Python-${INT_PYTHON}.tar.bz2"

  SOURCE[2]="$SRCDIR/setuptools-${INT_SETUPTOOLS}.tar.gz"
  SRCURL[2]="http://pypi.python.org/packages/source/s/setuptools/setuptools-${INT_SETUPTOOLS}.tar.gz"

  SOURCE[3]="$SRCDIR/BeautifulSoup-${INT_BEAUTIFULSOUP}.tar.gz"
  SRCURL[3]="http://www.crummy.com/software/BeautifulSoup/download/3.x/BeautifulSoup-${INT_BEAUTIFULSOUP}.tar.gz"

  SOURCE[4]="$SRCDIR/dnspython-${INT_DNSPYTHON}.tar.gz"
  SRCURL[4]="http://www.dnspython.org/kits/${INT_DNSPYTHON}/dnspython-${INT_DNSPYTHON}.tar.gz"

  SOURCE[5]="$SRCDIR/cssutils-${INT_CSSUTILS}.zip"
  SRCURL[5]="http://bitbucket.org/cthedot/cssutils/downloads/cssutils-${INT_CSSUTILS}.zip"

  SOURCE[6]="$SRCDIR/python-dateutil-${INT_DATEUTIL}.tar.gz"
  SRCURL[6]="http://labix.org/download/python-dateutil/python-dateutil-${INT_DATEUTIL}.tar.gz"

  SOURCE[7]="$SRCDIR/Imaging-${INT_IMAGING}.tar.gz"
  SRCURL[7]="http://effbot.org/media/downloads/Imaging-${INT_IMAGING}.tar.gz"

  SOURCE[8]="$SRCDIR/lxml-${INT_LXML}.tgz"
  SRCURL[8]="http://lxml.de/files/lxml-${INT_LXML}.tgz"

  SOURCE[9]="$SRCDIR/mechanize-${INT_MECHANIZE}.tar.gz"
  SRCURL[9]="http://wwwsearch.sourceforge.net/mechanize/src/mechanize-${INT_MECHANIZE}.tar.gz"

  SOURCE[10]="$SRCDIR/PyQt-x11-gpl-${INT_PYQT}.tar.gz"
  SRCURL[10]="http://sourceforge.net/projects/pyqt/files/PyQt4/PyQt-${INT_PYQT}/PyQt-x11-gpl-${INT_PYQT}.tar.gz"

  SOURCE[11]="$SRCDIR/sip-${INT_SIP}.tar.gz"
  SRCURL[11]="http://sourceforge.net/projects/pyqt/files/sip/sip-${INT_SIP}/sip-${INT_SIP}.tar.gz"

  SOURCE[12]="$SRCDIR/dbus-python-${INT_DBUSPYTHON}.tar.gz"
  SRCURL[12]="http://dbus.freedesktop.org/releases/dbus-python/dbus-python-${INT_DBUSPYTHON}.tar.gz"

  SOURCE[13]="$SRCDIR/pycrypto-${INT_PYCRYPTO}.tar.gz"
  SRCURL[13]="http://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-${INT_PYCRYPTO}.tar.gz"

  SOURCE[14]="$SRCDIR/netifaces-${INT_NETIFACES}.tar.gz"
  SRCURL[14]="http://alastairs-place.net/projects/netifaces//netifaces-${INT_NETIFACES}.tar.gz"

  SOURCE[15]="$SRCDIR/cssselect-${INT_CSSSELECT}.tar.gz"
  SRCURL[15]="http://pypi.python.org/packages/source/c/cssselect/cssselect-${INT_CSSSELECT}.tar.gz"

  SOURCE[16]="$SRCDIR/psutil-${INT_PSUTIL}.tar.gz"
  SRCURL[16]="http://psutil.googlecode.com/files/psutil-${INT_PSUTIL}.tar.gz"

  SOURCE[17]="$SRCDIR/apsw-${INT_APSW}.zip"
  SRCURL[17]="https://apsw.googlecode.com/files/apsw-${INT_APSW}.zip"

  SOURCE[18]="$SRCDIR/ImageMagick-${INT_MAGICK}.tar.xz"
  SRCURL[18]="ftp://ftp.ImageMagick.org/pub/ImageMagick/ImageMagick-${INT_MAGICK}.tar.xz"

  NEXT_ARRAYEL=${#SOURCE[@]}
else
  NEXT_ARRAYEL=1
fi

if [ "$BUILD_MTP" = "YES" ]; then
  SOURCE[$NEXT_ARRAYEL]="$SRCDIR/libmtp-${INT_MTP}.tar.gz"
  SRCURL[$NEXT_ARRAYEL]="http://sourceforge.net/projects/libmtp/files/libmtp/${INT_MTP}/libmtp-${INT_MTP}.tar.gz/download"

  NEXT_ARRAYEL=$(( ${NEXT_ARRAYEL} + 1 ))
fi

if [ "$BUILD_QT" = "YES" ]; then
  SOURCE[$NEXT_ARRAYEL]="$SRCDIR/qt-everywhere-opensource-src-${INT_QT}.tar.gz"
  SRCURL[$NEXT_ARRAYEL]="http://releases.qt-project.org/qt4/source/qt-everywhere-opensource-src-${INT_QT}.tar.gz"

  NEXT_ARRAYEL=$(( ${NEXT_ARRAYEL} + 1 ))
fi

##
## --- with a little luck, you won't have to edit below this point --- ##
##

# Automatically determine the architecture we're building on:
MARCH=$( uname -m )
if [ -z "$ARCH" ]; then
  case "$MARCH" in
    i?86)    export ARCH=i486 ;;
    armv7hl) export ARCH=$MARCH ;;
    arm*)    export ARCH=arm ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
    *)       export ARCH=$MARCH ;;
  esac
fi

case "$ARCH" in
  i486)      SLKCFLAGS="-O2 -march=i486 -mtune=i686"
             SLKLDFLAGS=""; LIBDIRSUFFIX=""
             ;;
  x86_64)    SLKCFLAGS="-O2 -fPIC"
             SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"
             ;;
  armv7hl)   SLKCFLAGS="-O2 -march=armv7-a -mfpu=vfpv3-d16"
             SLKLDFLAGS=""; LIBDIRSUFFIX=""
             ;;
  *)         SLKCFLAGS="-O2"
             SLKLDFLAGS=""; LIBDIRSUFFIX=""
             ;;
esac

# Exit the script on errors:
set -e
trap 'echo "$0 FAILED at line ${LINENO}" | tee $OUTPUT/error-${PRGNAM}.log' ERR
# Catch unitialized variables:
set -u
P1=${1:-1}

# Save old umask and set to 0022:
_UMASK_=$(umask)
umask 0022

# Create working directories:
mkdir -p $OUTPUT          # place for the package to be saved
mkdir -p $TMP/tmp-$PRGNAM # location to build the source
mkdir -p $PKG             # place for the package to be built
rm -rf $PKG/*             # always erase old package's contents
rm -rf $TMP/tmp-$PRGNAM/* # remove the remnants of previous build
rm -rf $OUTPUT/{configure,make,install,error,makepkg}-$PRGNAM.log
                          # remove old log files

# Source file availability:
for (( i = 0; i < ${#SOURCE[*]}; i++ )) ; do
  if ! [ -f ${SOURCE[$i]} ]; then
    echo "Source '$(basename ${SOURCE[$i]})' not available yet..."
    # Check if the $SRCDIR is writable at all - if not, download to $OUTPUT
    [ -w "$SRCDIR" ] || SOURCE[$i]="$OUTPUT/$(basename ${SOURCE[$i]})"
    if [ -f ${SOURCE[$i]} ]; then echo "Ah, found it!"; continue; fi
    if ! [ "x${SRCURL[$i]}" == "x" ]; then
      echo "Will download file to $(dirname $SOURCE[$i])"
      wget -nv -T 20 -O "${SOURCE[$i]}" "${SRCURL[$i]}" || true
      if [ $? -ne 0 -o ! -s "${SOURCE[$i]}" ]; then
        echo "Fail to download '$(basename ${SOURCE[$i]})'. Aborting the build."
        mv -f "${SOURCE[$i]}" "${SOURCE[$i]}".FAIL
        exit 1
      fi
    else
      echo "File '$(basename ${SOURCE[$i]})' not available. Aborting the build."
      exit 1
    fi
  fi
done

if [ "$P1" == "--download" ]; then
  echo "Download complete."
  exit 0
fi

# --- PACKAGE BUILDING ---

echo "++"
echo "|| $PRGNAM-$VERSION"
echo "++"

if [ "$BUILD_PYTHON" = "YES" -o "$BUILD_MTP" = "YES" -o  "$BUILD_QT" = "YES" ]; then
  # Sanity check: in case we need to build an internal set of python interpreter
  # plus support modules, then you must not compile this package when calibre
  # is already installed!
  if [ -d /usr/lib${LIBDIRSUFFIX}/calibre/lib ]; then
    echo ""
    echo "** You have calibre installed in /usr/lib${LIBDIRSUFFIX}/calibre"
    echo "** Compiling this package means you have to remove the existing"
    echo "** calibre installation first, it conflicts with the compilation!"
    echo ""
    exit 1
  fi
fi

cd $TMP/tmp-$PRGNAM
echo "Extracting the source archive(s) for $PRGNAM..."
tar -xvf ${SOURCE[0]}
if [ $NEXT_ARRAYEL -gt 1 ]; then
for (( i = 1; i < ${#SOURCE[*]}; i++ )) ; do
  if unzip -l ${SOURCE[$i]} 1>/dev/null 2>&1 ; then
    unzip ${SOURCE[$i]}
  else
    tar -xvf ${SOURCE[$i]}
  fi
done
fi

chown -R root:root .
chmod -R u+w,go+r-w,a+X-s .

echo Building ...

#
# What follows is a series of subroutines for building all the parts:
#

#
# Build Qt 4.x:
#
make_qt() {

  echo -e "\n**  qt  **\n"

  cd $TMP/tmp-$PRGNAM/qt-everywhere-opensource-src-${INT_QT}

  # If we build our own Qt4, unset the QT-related environment variables,
  # so that a pre-existing Qt4 will not be picked up and create errors later:
  unset QTDIR
  unset QT4DIR
  NEWPATH="/usr/lib${LIBDIRSUFFIX}/calibre/bin"
  for elem in $(echo $PATH | cut -d: --output-delimiter=' ' -f1-) ; do
    if ! $(echo "$elem"| grep -q /qt) ; then
      NEWPATH="$NEWPATH:$elem"
    fi
  done
  export PATH="$NEWPATH"

  if [ $ARCH = "i486" ]; then
    sed -i -e "s/QMAKE_CFLAGS_RELEASE        += -O2/QMAKE_CFLAGS_RELEASE        += $SLKCFLAGS/" mkspecs/common/gcc-base.conf || exit 1
  fi

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

  # gcc doesn't support flag "-fuse-ld=gold":
  zcat $SRCDIR/qt.ld-gold.patch.gz | patch -p1 --verbose || exit 1

  # Stupid idea - remove it:
  zcat $SRCDIR/qt.webkit-no_Werror.patch.gz | patch -p1 --verbose || exit 1

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

  make $NUMJOBS
  make install INSTALL_ROOT=$PKG 2>&1 | tee $OUTPUT/install-${PRGNAM}_qt4.log

  # install private headers (calibre wants them),
  # using `rsync -R` as easy way to preserve relative path names:
  rsync -aR \
    include/Qt{Core,Declarative,Gui,Script}/private \
    src/{corelib,declarative,gui,script}/*/*_p.h \
    ${PKG}/usr/lib${LIBDIRSUFFIX}/calibre/

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/qt
  cp -a GPL_EXCEPTION*.TXT LICENSE* README* \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/qt || true

}

#
# Build libmtp:
#

make_mtp() {

  echo -e "\n**  libmtp  **\n"

  cd $TMP/tmp-$PRGNAM/libmtp-${INT_MTP}

  CFLAGS="$SLKCFLAGS" \
  ./configure \
    --prefix=/usr/lib${LIBDIRSUFFIX}/calibre \
    --disable-static

  # Build and install:
  make $NUMJOBS || make || exit 1
  make install DESTDIR=$PKG || exit 1

  # Guess someone didn't have enough to drink:
  ( cd $PKG/usr/lib${LIBDIRSUFFIX}/calibre/bin
    rm -f mtp-sendtr mtp-sendfile mtp-newfolder mtp-getfile mtp-delfile
    ln -s mtp-connect mtp-sendtr
    ln -s mtp-connect mtp-sendfile
    ln -s mtp-connect mtp-newfolder
    ln -s mtp-connect mtp-getfile
    ln -s mtp-connect mtp-delfile
  )

  # No need for this in the calibre package:
  rm -rf $PKG/lib

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/libmtp
  cp -a COPYING README* \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/libmtp || true
}

#
# Build ImageMagick:
#

make_magick() {

  echo -e "\n**  magick  **\n"

  cd $TMP/tmp-$PRGNAM/ImageMagick-${INT_MAGICK}

  LDFLAGS="-L$(pwd)/magick/.libs ${LDFLAGS}" \
  ./configure \
    --prefix=/usr/lib${LIBDIRSUFFIX}/calibre \
    --enable-shared \
    --disable-static \
    --disable-openmp \
    --without-modules \
    --without-frozenpaths \
    --without-perl \
    --without-x \
    --enable-zero-configuration \
    --build=$ARCH-slackware-linux || exit 1

  # Build (no parallel build!) and install:
  make || exit 1
  make install DESTDIR=$PKG || exit 1

  # .la files in libdir should be removed.
  # Other .la files should be left alone, as ImageMagick uses them internally
  # to locate modules.
  rm -f $PKG/usr/lib${LIBDIRSUFFIX}/calibre/lib/*.la

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/ImageMagick
  cp -a AUTHORS LICENSE NEWS NOTICE README* \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/ImageMagick || true
}

#
# Build python 2.7:
#
make_python() {

  echo -e "\n**  python  **\n"

  cd $TMP/tmp-$PRGNAM/Python-${INT_PYTHON}

  zcat $PATCHDIR/python.readline.set_pre_input_hook.diff.gz | \
    patch -p1 --verbose

  # We are going to install python inside the calibre package.
  # This requires some changes to the standard build:
  # Creating the 'lib' directory below is needed! We are going to use rpath
  # so that we can run python from this non-standard path:
  mkdir -p /usr/lib${LIBDIRSUFFIX}/calibre/lib

  LDFLAGS="-Wl,-rpath /usr/lib${LIBDIRSUFFIX}/calibre/lib" \
  ./configure \
    --prefix=/usr/lib${LIBDIRSUFFIX}/calibre \
    --bindir=/usr/lib${LIBDIRSUFFIX}/calibre \
    --with-threads \
    --enable-ipv6 \
    --enable-shared \
    --build=$ARCH-slackware-linux \
    2>&1 | tee $OUTPUT/configure-${PRGNAM}_python.log

  make $NUMJOBS || make 2>&1 | tee $OUTPUT/make-${PRGNAM}_python.log
  make install DESTDIR=$PKG 2>&1 | tee -a $OUTPUT/make-${PRGNAM}_python.log

  # No need for man pages now:
  rm -rf $PKG/usr/lib${LIBDIRSUFFIX}/calibre/share/man

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/python
  cp -a LICENSE README \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/python || true

} # End of make_python

#
# Build pyseuptools:
#
make_setuptools() {

  echo -e "\n**  setuptools  **\n"

  cd $TMP/tmp-$PRGNAM/setuptools-${INT_SETUPTOOLS}

  # Compile and install
  python setup.py install --root=$PKG \
    2>&1 | tee $OUTPUT/make-${PRGNAM}_setuptools.log

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/setuptools
  cp -a *.txt \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/setuptools || true

} # End make_setuptools

#
# Build BeautifulSoup:
#
make_BeautifulSoup() {

  echo -e "\n**  BeautifulSoup  **\n"

  cd $TMP/tmp-$PRGNAM/BeautifulSoup-${INT_BEAUTIFULSOUP}

  # Compile and install
  python setup.py install --root=$PKG \
    2>&1 | tee $OUTPUT/make-${PRGNAM}_BeautifulSoup.log

} # End make_BeautifulSoup

#
# Build dnspython:
#
make_dnspython() {

  echo -e "\n**  dnspython  **\n"

  cd $TMP/tmp-$PRGNAM/dnspython-${INT_DNSPYTHON}

  # Compile and install
  python setup.py install --root=$PKG \
    2>&1 | tee $OUTPUT/make-${PRGNAM}_dnspython.log

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/dnspython
  cp -a ChangeLog LICENSE README TODO \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/dnspython || true

} # End make_dnspython

#
# Build cssutils:
#
make_cssutils() {

  echo -e "\n**  cssutils  **\n"

  cd $TMP/tmp-$PRGNAM/cssutils-${INT_CSSUTILS}

  # The source is infested with DOS files:
  sed -i -e 's/\r//' $( find . -type f -exec file "{}" ";" | \
    grep CRLF | cut -f1 -d: ) \
    || true

  # Compile and install
  python setup.py install --root=$PKG \
    2>&1 | tee $OUTPUT/make-${PRGNAM}_cssutils.log

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/cssutils
  cp -a COPYING* *.txt \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/cssutils || true

} # End make_cssutils

#
# Build dateutil:
#
make_dateutil() {

  echo -e "\n**  dateutil  **\n"

  cd $TMP/tmp-$PRGNAM/python-dateutil-${INT_DATEUTIL}

  # Compile and install
  python setup.py install --root=$PKG \
    2>&1 | tee $OUTPUT/make-${PRGNAM}_dateutil.log

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/dateutil
  cp -a LICENSE NEWS README \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/dateutil || true

} # End make_dateutil

#
# Build imaging (this package is called "pil" in Slackware):
#
make_imaging() {

  echo -e "\n**  imaging  **\n"

  cd $TMP/tmp-$PRGNAM/Imaging-${INT_IMAGING}

  # Installation path for headers:
  PYTHONINC=$( python -c 'from distutils.sysconfig import *;print get_python_inc()' )

  # Support transparent gifs:
  cat $PATCHDIR/python-imaging.giftrans.diff | patch -p1 --verbose \
    2>&1 | tee -a $OUTPUT/patch-$PRGNAM.log

  # Fix the python path for Scripts/*.py :
  for script in Scripts/*.py ; do
    sed -i -e "s#/usr/local/bin/python#python#"  $script
    chmod 755 $script
  done

  # Since we use a non-standard python prefix, the setup.py must be told to
  # look into Slackware's system directories too, or we lose jpeg/zlib/freetype
  # etc... support:
  sed -i \
    -e 's#^TCL_ROOT =.*#TCL_ROOT = libinclude("/usr")#' \
    -e 's#^JPEG_ROOT =.*#JPEG_ROOT = libinclude("/usr")#' \
    -e 's#^ZLIB_ROOT =.*#ZLIB_ROOT = libinclude("/usr")#' \
    -e 's#^TIFF_ROOT =.*#TIFF_ROOT = libinclude("/usr")#' \
    -e 's#^FREETYPE_ROOT =.*#FREETYPE_ROOT = libinclude("/usr")#' \
    -e 's#^LCMS_ROOT =.*#LCMS_ROOT = libinclude("/usr")#' \
    setup.py

  # ... and compensate for 64bit:
  if [ "$ARCH" = "x86_64" ]; then
    sed -i -e "s#\([^0-9a-zA-Z]\)\(lib\)\([\"';,/)]\)#\1\264\3#" setup.py
  fi

  # Compile and install
  CFLAGS="$SLKCFLAGS" \
  python setup.py build_ext build 2>&1 | tee $OUTPUT/make-${PRGNAM}_imaging.log
  ( cd Sane
    CFLAGS="$SLKCFLAGS" python setup.py build \
      2>&1 | tee -a $OUTPUT/make-${PRGNAM}_imaging.log
  )

  # Install all of this:
  mkdir -p $PKG/${PYTHONINC}/Imaging
  install -m 644 libImaging/*.h $PKG/${PYTHONINC}/Imaging
  python setup.py install --skip-build --root $PKG
  ( cd Sane
    python setup.py install --skip-build --root $PKG
  )

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/imaging
  cp -a README CHANGES \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/imaging || true

} # End make_imaging

#
# Build lxml:
#
make_lxml() {

  echo -e "\n**  lxml  **\n"

  cd $TMP/tmp-$PRGNAM/lxml-${INT_LXML}

  # Compile and install
  python setup.py install --root=$PKG \
    2>&1 | tee $OUTPUT/make-${PRGNAM}_lxml.log

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/lxml
  cp -a *.txt doc/licenses \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/lxml || true

} # End make_lxml

#
# Build mechanize:
#
make_mechanize() {

  echo -e "\n**  mechanize  **\n"

  cd $TMP/tmp-$PRGNAM/mechanize-${INT_MECHANIZE}

  # Compile and install
  export CFLAGS="$SLKCFLAGS"
  python setup.py install --root=$PKG \
    2>&1 | tee $OUTPUT/make-${PRGNAM}_mechanize.log

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/mechanize
  cp -a *.txt \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/mechanize || true

} # End make_mechanize

#
# Build sip:
#
make_sip() {

  echo -e "\n**  sip  **\n"

  cd $TMP/tmp-$PRGNAM/sip-${INT_SIP}

  # Compile and install:
  CFLAGS="$SLKCFLAGS" \
  CXXFLAGS="$SLKCFLAGS" \
    python configure.py 2>&1 | tee $OUTPUT/configure-${PRGNAM}_sip.log
  make $NUMJOBS || make 2>&1 | tee $OUTPUT/make-${PRGNAM}_sip.log
  make install DESTDIR=$PKG 2>&1 | tee -a $OUTPUT/make-${PRGNAM}_sip.log

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/sip
  cp -a LICENSE COPYING* NEWS README TODO \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/sip || true

} # End make_sip

#
# Build dbus-python:
#
make_dbuspython() {

  echo -e "\n**  dbus-python  **\n"

  cd $TMP/tmp-$PRGNAM/dbus-python-${INT_DBUSPYTHON}

  LDFLAGS="" \
  LD_LIBRARY_PATH="/usr/lib${LIBDIRSUFFIX}/calibre/lib" \
  PYTHON_INCLUDES="-I/usr/lib${LIBDIRSUFFIX}/calibre/include/python${REQUIRED_PYTHON}" \
  PYTHON_LIBS="-L/usr/lib${LIBDIRSUFFIX}/calibre/lib -lpython${REQUIRED_PYTHON}" \
  ./configure \
    --prefix=/usr/lib${LIBDIRSUFFIX}/calibre \
    --enable-shared=yes \
    --enable-static=no \
    --build=$ARCH-slackware-linux \
    --host=$ARCH-slackware-linux \
    2>&1 | tee $OUTPUT/configure-${PRGNAM}_dbuspython.log

  make 2>&1 | tee $OUTPUT/make-${PRGNAM}_dbuspython.log
  make install DESTDIR=$PKG 2>&1 | tee -a $OUTPUT/make-${PRGNAM}_dbuspython.log

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/dbus-python
  cp -a AUTHORS COPYING* INSTALL NEWS README* \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/dbus-python || true

}

#
# Build PyQt:
#
make_pyqt() {

  echo -e "\n**  pyqt  **\n"

  cd $TMP/tmp-$PRGNAM/PyQt-x11-gpl-${INT_PYQT}

  if [ "${BUILD_QT}" = "NO" ]; then
    # Fix phonon detection:
    zcat $PATCHDIR/PyQt.phonon.diff.gz | patch -p1 --verbose

    export QTDIR=/usr/lib${LIBDIRSUFFIX}/qt
  else
    unset QTDIR
    unset QT4DIR
    export QTDIR=/usr/lib${LIBDIRSUFFIX}/calibre
  fi

  export CFLAGS="$SLKCFLAGS"
  export CXXFLAGS="$SLKCFLAGS"

  python configure.py --confirm-license --verbose \
    2>&1 | tee $OUTPUT/configure-${PRGNAM}_pyqt.log
  make $NUMJOBS || make \
    2>&1 | tee $OUTPUT/make-${PRGNAM}_pyqt.log
  make install DESTDIR=$PKG \
    2>&1 | tee -a $OUTPUT/make-${PRGNAM}_pyqt.log

  if [ "${BUILD_QT}" = "NO" ]; then
    # Remove ugly bits that threaten to overwrite a system file:
    rm -r $PKG/usr/lib${LIBDIRSUFFIX}/qt
  fi

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/pyqt
  cp -a GPL_EXCEPTION*.TXT LICENSE.* NEWS OPENSOURCE-NOTICE.TXT README THANKS \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/pyqt || true

} # End make_pyqt

#
# Build pycrypto:
#
make_pycrypto() {

  echo -e "\n**  pycrypto  **\n"

  cd $TMP/tmp-$PRGNAM/pycrypto-${INT_PYCRYPTO}

  # Compile and install
  export CFLAGS="$SLKCFLAGS"
  python setup.py install --root=$PKG \
    2>&1 | tee $OUTPUT/make-${PRGNAM}_crypto.log

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/pycrypto
  cp -a ACKS ChangeLog COPYRIGHT README TODO Doc \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/pycrypto || true

} # End make_pycrypto

#
# Build netifaces:
#
make_netifaces() {

  echo -e "\n**  netifaces  **\n"

  cd $TMP/tmp-$PRGNAM/netifaces-${INT_NETIFACES}

  # Compile and install
  export CFLAGS="$SLKCFLAGS"
  python setup.py install --root=$PKG \
    2>&1 | tee $OUTPUT/make-${PRGNAM}_netifaces.log

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/netifaces
  cp -a README \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/netifaces || true

} # End make_netifaces


#
# Build cssselect:
#
make_cssselect() {

  echo -e "\n**  cssselect  **\n"

  cd $TMP/tmp-$PRGNAM/cssselect-${INT_CSSSELECT}

  # Compile and install
  export CFLAGS="$SLKCFLAGS"
  python setup.py install --root=$PKG \
    2>&1 | tee $OUTPUT/make-${PRGNAM}_cssselect.log

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/cssselect
  cp -a AUTHORS CHANGES LICENSE PKG-INFO README.rst \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/cssselect || true

} # End make_cssselect


#
# Build apsw:
#
make_apsw() {

  echo -e "\n**  apsw  **\n"

  cd $TMP/tmp-$PRGNAM/apsw-${INT_APSW}

  # Compile and install
  export CFLAGS="$SLKCFLAGS"
  python setup.py install --root=$PKG \
    2>&1 | tee $OUTPUT/make-${PRGNAM}_apsw.log

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/apsw
  cp -a PKG-INFO doc/copyright.html \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/apsw || true

} # End make_apsw


#
# Build psutil:
#
make_psutil() {

  echo -e "\n**  psutil  **\n"

  cd $TMP/tmp-$PRGNAM/psutil-${INT_PSUTIL}

  # Compile and install
  export CFLAGS="$SLKCFLAGS"
  python setup.py install --root=$PKG \
    2>&1 | tee $OUTPUT/make-${PRGNAM}_psutil.log

  # Add documentation:
  mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/psutil
  cp -a CREDITS HISTORY INSTALL LICENSE PKG-INFO README \
    $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/psutil || true

} # End make_psutil


# --------------
# Build calibre:
# --------------

make_calibre () {

  echo -e "\n**  calibre  **\n"

  cd $TMP/tmp-$PRGNAM/${PRGNAM}

  # Calibre 0.8.6 has a version bug:
  INTERNAL_VERSION=$(echo $VERSION | sed -e 's/\./, /g')
  VERSION_CONSTANT=$(grep "^numeric_version" src/calibre/constants.py)
  if [ "$VERSION_CONSTANT" != "numeric_version = ($INTERNAL_VERSION)" ]; then
    echo "** Changing '$VERSION_CONSTANT' to:"
    echo "**          'numeric_version = ($INTERNAL_VERSION)'"
    sed -i -e "s/^numeric_version.*$/numeric_version = ($INTERNAL_VERSION)/" \
      src/calibre/constants.py
  fi

  ## Slackware 13.37 and earlier has a libmtp which is too old,
  ## so we remove support for mtp device dection:
  #if [ $(echo $SLACKVER | cut -f1 -d.) -lt 14 ]; then
  #  touch $OUTPUT/patch-$PRGNAM.log
  #  cat $PATCHDIR/calibre_nomtp.patch | patch -p1 --verbose \
  #    2>&1 | tee -a $OUTPUT/patch-$PRGNAM.log
  #fi

  if [ "${BUILD_QT}" = "NO" ]; then
    # Remove the unneeded 'qt_hack' which seems to be specific to MacOS/Windows
    # (correct me if I am wrong). This extension refuses to compile on Slackware
    # 13.37 as well as 14.0.
    case "$SLACKVER" in
      13.*|14.0)
        cat $PATCHDIR/calibre_noqthack.patch | patch -p1 --verbose \
          2>&1 | tee -a $OUTPUT/patch-$PRGNAM.log
        ;;
      *)
        ;;
    esac
  fi

  # Without the creation of these directories, calibre-0.9.33 refuses to build:
  cat $PATCHDIR/calibre.missingdirs.patch | patch -p1 --verbose \
      2>&1 | tee -a $OUTPUT/patch-$PRGNAM.log

  # This dropping of privilege is unneccessary and breaks the SlackBuild:
  sed -i -e "s:if os.geteuid() == 0:if False and os.geteuid() == 0:" \
    setup/install.py

  # See if we picked up any compiler or linker flags for python:
  LDFLAGS=${LDFLAGS:-""}
  CFLAGS=${CFLAGS:-""}

  # Place where we have to install stuff:
  PYTHONLIB=$( python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()" )

  # Create the directory for calibre's environment module:
  mkdir -p $PKG$PYTHONLIB

  # Find the prefix python was installed to (may be different from calibre's):
  PYTHONROOT=$( python -c "from distutils.sysconfig import get_python_lib; print get_python_lib(prefix='/')" )
  PYTHONPREF="${PYTHONLIB%$PYTHONROOT}"

  # Massage the install script to actually install to that python libdir:
  sed -e "/s.get_python_lib(prefix=/s,(prefix=.*),(prefix='$PKG$PYTHONPREF')," \
      -i setup/install.py 

  # Cut out the DOS crap:
  sed -i -e 's/\r//' src/calibre/web/feeds/recipes/*

  OVERRIDE_LDFLAGS="$LDFLAGS $SLKLDFLAGS" \
  OVERRIDE_CFLAGS="$CFLAGS $SLKCFLAGS" \
  python setup.py build \
    2>&1 | tee $OUTPUT/make-${PRGNAM}.log

  # Create the directories the xdg-utils expects to be present:
  mkdir -p $PKG/usr/share/{applications,icons/hicolor,mime/packages}
  mkdir -p $PKG/usr/share/{desktop-directories,packages}

  XDG_DATA_DIRS="$PKG/usr/share" \
  XDG_UTILS_INSTALL_MODE="system" \
  LIBPATH="/usr/lib${LIBDIRSUFFIX}" \
  python setup.py install \
    --prefix=/usr \
    --libdir=/usr/lib${LIBDIRSUFFIX} \
    --staging-root=$PKG/usr \
    --staging-libdir=$PKG/usr/lib${LIBDIRSUFFIX} \
    2>&1 | tee $OUTPUT/install-$PRGNAM.log

  # OK... first some fixes due to the below-par installer:

  # The bash completion files need to be in /etc :
  mv $PKG/usr/etc $PKG/

  ## Mime type definition files end in .xml :
  # Fixed in calibre-0.8.70
  #mv $PKG/usr/share/mime/packages/calibre-mimetypes{,.xml}

  # This does not belong in a package - use 'removepkg' instead:
  rm -f $PKG/usr/bin/calibre-uninstall

  # I'd rather not ship 1.6MB of redundant fonts:
  ( cd $PKG/usr/share/calibre/fonts/liberation/
    for FONT in *.ttf ; do
      rm $FONT
      ln -sf /usr/share/fonts/TTF/$FONT
    done
  )

  # All these files will be generated by update-mime-database:
  find $PKG/usr/share/mime -maxdepth 1 -type f | xargs rm -f

  # These files are generated by update-desktop-database while installing:
  rm -f $PKG/usr/share/applications/defaults.list
  rm -f $PKG/usr/share/applications/mimeinfo.cache

  # These two served their purpose:
  rmdir $PKG/usr/share/{desktop-directories,packages}

  # And then some additions or it won't all look nice in your menu:

  # Add a MimeType icon for lrf, and add an application icon:
  for i in 16 24 32 48 64 96 128; do
    mkdir -p $PKG/usr/share/icons/hicolor/${i}x${i}/{apps,mimetypes}
    convert imgsrc/mimetypes/lrf.svg -scale $i \
      $PKG/usr/share/icons/hicolor/${i}x${i}/mimetypes/application-x-sony-bbeb.png
    convert resources/images/library.png -resize ${i}x${i} \
      $PKG/usr/share/icons/hicolor/${i}x${i}/apps/calibre-gui.png
    convert imgsrc/viewer.svg -scale $i \
      $PKG/usr/share/icons/hicolor/${i}x${i}/apps/calibre-viewer.png
  done

} # End of make_calibre

#
# End of the subroutines.
#

# Compilation of the software:
if [ "$BUILD_PYTHON" = "YES" -o "$BUILD_QT" = "YES" -o "$BUILD_MTP" = "YES" ]; then
  if  [ -f $SRCDIR/calibre_pythondeps-${SLACKVER}-${ARCH}.tar.bz2 ]; then
    # We unwrap the tarball of compiled python internal libraries instead of
    # having to build them - this saves a _lot_ of compile time:
    mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/calibre
    ( cd $PKG/usr/lib${LIBDIRSUFFIX}/calibre
      echo "**  Extracting dependencies tarball... **"
      tar -jxf $SRCDIR/calibre_pythondeps-${SLACKVER}-${ARCH}.tar.bz2
      # We also need a copy in the filesystem root!
      rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre /usr/lib${LIBDIRSUFFIX}/
    )
  fi

  # Ensure that our non-standard python2.7 is found and used:
  echo "** Configuring build environment for preferring internal libraries **"
  export PATH="/usr/lib${LIBDIRSUFFIX}/calibre:/usr/lib${LIBDIRSUFFIX}/calibre/bin:$PATH"
  export PKG_CONFIG_PATH="/usr/lib${LIBDIRSUFFIX}/calibre/lib/pkgconfig:$PKG_CONFIG_PATH"
  export LDFLAGS="-Wl,-rpath /usr/lib${LIBDIRSUFFIX}/calibre/lib -L/usr/lib${LIBDIRSUFFIX}/calibre/lib"

  # In order to prevent a gcc error if we have to compile libmtp:
  mkdir -p /usr/lib${LIBDIRSUFFIX}/calibre/lib

fi

if [ "$BUILD_MTP" = "YES" ]; then
  # No sufficiently new libmtp was found so we need to supply it ourself.

  if  [ ! -f $SRCDIR/calibre_pythondeps-${SLACKVER}-${ARCH}.tar.bz2 ]; then
    # No tarball containing pre-compiled libraries was found.
    # We need to build libmptp ourself.
    
    # After each part is built we will sync to here because in the end, calibre
    # will wipe the origin and we will have to sync it all back:
    mkdir -p /usr/lib${LIBDIRSUFFIX}/calibre

    make_mtp
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

  fi
fi

if [ "$BUILD_QT" = "YES" ]; then
  # No sufficiently new Qt was found so we need to supply it ourself.

  if  [ ! -f $SRCDIR/calibre_pythondeps-${SLACKVER}-${ARCH}.tar.bz2 ]; then
    # No tarball containing pre-compiled libraries was found.
    # We need to build Qt ourself.
    
    # After each part is built we will sync to here because in the end, calibre
    # will wipe the origin and we will have to sync it all back:
    mkdir -p /usr/lib${LIBDIRSUFFIX}/calibre

    make_qt
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

  fi
fi

if [ "$BUILD_PYTHON" = "YES" ]; then
  # No python2.7 was found on the computer so we need to supply it ourself.

  if  [ ! -f $SRCDIR/calibre_pythondeps-${SLACKVER}-${ARCH}.tar.bz2 ]; then
    # No tarball containing pre-compiled python libraries was found.
    # We need to build python2.7 ourself.
    
    # After each part is built we will sync to here because in the end, calibre
    # will wipe the origin and we will have to sync it all back:
    mkdir -p /usr/lib${LIBDIRSUFFIX}/calibre

    make_python
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

    ## Ensure that all our binaries and libraries are found and used:
    #export PATH="/usr/lib${LIBDIRSUFFIX}/calibre:/usr/lib${LIBDIRSUFFIX}/calibre/bin:$PATH"
    #export PKG_CONFIG_PATH="/usr/lib${LIBDIRSUFFIX}/calibre/lib/pkgconfig:$PKG_CONFIG_PATH"
    #export LDFLAGS="-Wl,-rpath /usr/lib${LIBDIRSUFFIX}/calibre/lib -L/usr/lib${LIBDIRSUFFIX}/calibre/lib"

    make_setuptools
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

    make_dbuspython
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

    make_BeautifulSoup
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

    make_apsw
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

    make_dnspython
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

    make_cssutils
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

    make_dateutil
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

    make_imaging
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

    make_lxml
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

    make_mechanize
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

    make_pycrypto
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

    make_netifaces
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

    make_cssselect
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

    make_psutil
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

    make_sip
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

    make_pyqt
    rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

    # Does not work yet, linker errors:
    #make_magick
    #rsync -a $PKG/usr/lib${LIBDIRSUFFIX}/calibre/ /usr/lib${LIBDIRSUFFIX}/calibre/

  fi
fi

if [ "$BUILD_PYTHON" = "YES" -o "$BUILD_QT" = "YES" -o "$BUILD_MTP" = "YES" ]; then
  # At this point we can wrap the compiled python etc. internal libraries
  # into a tarball which we can re-use in a future build (so that we do not
  # have to recompile them every time).
  # If you want to make use of this, then just copy the resulting tarball
  # into the same directory as this SlackBuild script:
  if  [ ! -f $SRCDIR/calibre_pythondeps-${SLACKVER}-${ARCH}.tar.bz2 ]; then
    ( cd $PKG/usr/lib${LIBDIRSUFFIX}/calibre
      echo "** Creating dependencies tarball $OUTPUT/calibre_pythondeps-${SLACKVER}-${ARCH}.tar.bz2"
      echo "** Move this file to $SRCDIR for future compilation speedups."
      tar -jcf $OUTPUT/calibre_pythondeps-${SLACKVER}-${ARCH}.tar.bz2 .
    )
  fi
fi

#if [ "$BUILD_QT" = "YES" -o "$BUILD_MTP" = "YES" ]; then
  # We need to point calibre's compilation at our custom headers:
  export CFLAGS="-I/usr/lib${LIBDIRSUFFIX}/calibre/include"
#fi

# The main course - compile calibre:
make_calibre

if [ "$BUILD_PYTHON" = "YES" ]; then

  # Compiling calibre will have wiped all previously compiled python stuff,
  # so we need to put it back into the package, and then it can go:
  rsync -a /usr/lib${LIBDIRSUFFIX}/calibre/ $PKG/usr/lib${LIBDIRSUFFIX}/calibre/
  rm -rf /usr/lib${LIBDIRSUFFIX}/calibre

  ##!/bin/sh
  #path=`readlink -e $0`
  #base=`dirname $path`
  #export PATH=$base/bin:$PATH
  #export LD_LIBRARY_PATH=$base/lib:$LD_LIBRARY_PATH
  #$base/bin/calibre "$@"

  # We need to tell calibre to use the internal python:
  for FILE in $PKG/usr/bin/* ; do
   if ! file $FILE | grep -q ELF ; then
    sed -i \
     -e "s,^#!/usr/bin/env .*$,#!/usr/lib${LIBDIRSUFFIX}/calibre/python," \
     $FILE
   fi
  done

  # This is not needed in the package:
  rm -rf $PKG/usr/lib${LIBDIRSUFFIX}/calibre/{bin,lib/pkgconfig,share,usr/man}

fi

# Create a doinst.sh:
mkdir -p $PKG/install
cat <<EOT >> $PKG/install/doinst.sh
# Update the desktop and mime database:
if [ -x usr/bin/update-desktop-database ]; then
  chroot . /usr/bin/update-desktop-database usr/share/applications > /dev/null 2>&1
fi
if [ -x usr/bin/update-mime-database ]; then
  chroot . /usr/bin/update-mime-database usr/share/mime > /dev/null 2>&1
fi
if [ -x usr/bin/gtk-update-icon-cache ] ; then
  chroot . /usr/bin/gtk-update-icon-cache --quiet usr/share/icons/hicolor 
fi

EOT

# Add documentation:
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
# First, the calibre documentation files:
cp -a $DOCS $PKG/usr/doc/$PRGNAM-$VERSION || true
if [ "$BUILD_PYTHON" = "YES" ]; then
  # Add documentation (mainly licenses) of the internal python deps:
  mv $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc/internaldeps/* $PKG/usr/doc/$PRGNAM-$VERSION/
  rm -rf $PKG/usr/lib${LIBDIRSUFFIX}/calibre/doc
fi
# Add all the patches we used to build this package:
cp -a $PATCHDIR $PKG/usr/doc/$PRGNAM-$VERSION
# And finally, add this SlackBuild script:
cat $SRCDIR/$(basename $0) > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
chown -R root:root $PKG/usr/doc/$PRGNAM-$VERSION
find $PKG/usr/doc -type f -exec chmod 644 {} \;

if [ "$BUILD_PYTHON" = "YES" ]; then
  # If we built our own Qt then this can be removed from the package:
  rm -rf $PKG/usr/lib${LIBDIRSUFFIX}/calibre/{demos,doc,examples,tests}
fi

# Strip binaries (if any):
find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

# Add a package description:
mkdir -p $PKG/install
cat $SRCDIR/slack-desc > $PKG/install/slack-desc
if [ "$BUILD_PYTHON" = "YES" ]; then
  cat $SRCDIR/slack-required \
    | grep -Ev "BeautifulSoup|dnspython|pycrypto|pysetuptools|python-apsw|python-cssselect|python-cssutils|python-dateutil|python-imaging|python-lxml|python-mechanize|python-netifaces|python-psutil" \
    > $PKG/install/slack-required
else
  cat $SRCDIR/slack-required > $PKG/install/slack-required
fi
if [ $(cat /etc/slackware-version | cut -f2 -d' ' | cut -f1 -d.) -ge 14 ]; then
  # Slackware 14 and onwards ship with icu4c
  sed -i -e '/icu4c/d' $PKG/install/slack-required
fi

# Build the package:
cd $PKG
makepkg --linkadd y --chown n $OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.${PKGTYPE:-txz} 2>&1 | tee $OUTPUT/makepkg-${PRGNAM}.log
cd $OUTPUT
md5sum ${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.${PKGTYPE:-txz} > ${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.${PKGTYPE:-txz}.md5
cd -
cat $PKG/install/slack-desc | grep "^${PRGNAM}" > $OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.txt
cat $PKG/install/slack-required > $OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}${TAG}.dep

# Restore the original umask:
umask ${_UMASK_}

