summaryrefslogtreecommitdiffstats
path: root/calibre/build/calibre.SlackBuild
blob: bd4311f96f7b378e2c31bab69b7aed01915783c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
#!/bin/sh
# $Id$
# Copyright 2010, 2011  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-cssutils,
#             python-imaging, python-lxml, python-dateutil, python-mechanize 
# 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.
# 
# 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:-0.8.18}
BUILD=${BUILD:-1}
NUMJOBS=${NUMJOBS:" -j4 "}
TAG=${TAG:-alien}

DOCS="LICENSE* Changelog* COPYRIGHT README*"

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

REQUIRED_PYTHON="2.7"

# Only used in case we need to build our own python:
INT_PYTHON=2.7.1
INT_SETUPTOOLS=0.6c11
INT_BEAUTIFULSOUP=3.2.0
INT_DNSPYTHON=1.9.4
INT_CSSUTILS=0.9.7
INT_DATEUTIL=1.5
INT_IMAGING=1.1.7
INT_LXML=2.3
INT_MECHANIZE=0.2.5
INT_SIP=4.12.3
INT_DBUSPYTHON=0.84.0
INT_PYQT=4.8.4

# 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.gz"
SRCURL[0]="http://downloads.sourceforge.net/${PRGNAM}/${PRGNAM}-${VERSION}.tar.gz"

if [ "$PYTHON" = "$REQUIRED_PYTHON" ]; then
  BUILD_PYTHON="NO"
else
  # We need to build our own python 2.7 plus supporting modules:
  BUILD_PYTHON="YES"

  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://cssutils.googlecode.com/files/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://codespeak.net/lxml/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://www.riverbankcomputing.co.uk/static/Downloads/PyQt4/PyQt-x11-gpl-${INT_PYQT}.tar.gz"

  SOURCE[11]="$SRCDIR/sip-${INT_SIP}.tar.gz"
  SRCURL[11]="http://www.riverbankcomputing.co.uk/static/Downloads/sip4/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"

fi

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

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) export ARCH=i486 ;;
    arm*) export ARCH=arm ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
       *) export ARCH=$( uname -m ) ;;
  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"
             ;;
  *)         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" ]; 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 [ "$BUILD_PYTHON" = "YES" ]; then
for (( i = 1; i < ${#SOURCE[*]}; i++ )) ; do
  if $(file ${SOURCE[$i]} | grep -qi ": zip"); 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 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: )

  # 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}

  # Fix phonon detection:
  zcat $SRCDIR/patches/PyQt.phonon.diff.gz | patch -p1 --verbose

  # Compile and install
  # This avoids compiling a version number into KDE's .la files:
  export QTDIR=/usr/lib${LIBDIRSUFFIX}/qt

  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

  # ... but remove ugly bits that threaten to overwrite a system file:
  rm -r $PKG/usr/lib${LIBDIRSUFFIX}/qt

  # 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 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

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

  # 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/*

  # Use standard manpage names:
  cat $PATCHDIR/calibre.manpages.diff | patch -p1 --verbose \
    2>&1 | tee $OUTPUT/patch-$PRGNAM.log

  OVERRIDE_LDFLAGS="$LDFLAGS $SLKLDFLAGS" \
  OVERRIDE_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/

  # Move the man pages to the standard location:
  mv $PKG/usr/share/calibre/man $PKG/usr
  ln -sf /usr/man $PKG/usr/share/calibre/man

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

  # This is stupid:
  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" ]; 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
    # 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
      tar -jxvf $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}/
    )

    # Ensure that our non-standard python2.7 is found and used:
    export PATH="/usr/lib${LIBDIRSUFFIX}/calibre:$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"

  else

    # 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 /usr/lib${LIBDIRSUFFIX}/calibre

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

    # Ensure that our non-standard python2.7 is found and used:
    export PATH="/usr/lib${LIBDIRSUFFIX}/calibre:$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_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_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/

    # At this point we can wrap the compiled python 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:
    ( cd $PKG/usr/lib${LIBDIRSUFFIX}/calibre
      tar -jcvf $OUTPUT/calibre_pythondeps-${SLACKVER}-${ARCH}.tar.bz2 .
    )

  fi
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}

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 $SRCDIR/patches $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 {} \;

# Compress the man page(s):
if [ -d $PKG/usr/man ]; then
  find $PKG/usr/man -type f -name "*.?" -exec gzip -9f {} \;
  for i in $(find $PKG/usr/man -type l -name "*.?") ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
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|pysetuptools|python-cssutils|python-dateutil|python-imaging|python-mechanize|python-lxml" \
    > $PKG/install/slack-required
else
  cat $SRCDIR/slack-required > $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_}