#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# fontconfig.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
#
# Copyright 2008, 2009, 2010, 2013  Patrick J. Volkerding, Sebeka, Minnesota, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Set this to liberation, dejavu, or noto:
PREFERRED_FONT=${PREFERRED_FONT:-liberation}

#
# This code is copied from d/doxygen.SlackBuild:
#
# Some times bits of the documentation fails to build, but this fixes it:
# This is a bug in some of the ARM packages, but I'm not sure which yet.
# mktexlsr > yes | updmap-sys --syncwithtrees -> updmap-sys
# fixes a build problem with the PDF documentation, so I assume that some
# other packages installed subsequently to texlive are overwriting the files.
# Needs investigation, but for now, this is the hack.
rm -rf ~/.texlive/
mktexlsr
yes | updmap-sys --syncwithtrees
updmap-sys
# and for good measure:
texconfig rehash
texconfig-sys rehash

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
#tar xvvf $PORTCWD/sources/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-*/ || exit 1
slackhousekeeping

# Apply patches:
#
# Prefer DejaVu fonts in 60-latin.conf:
#zcat $CWD/fontconfig.dejavu.diff.gz | patch -p1 --verbose || exit 1

# Prefer Liberation fonts in 60-latin.conf (these work better with hinting):
zcat $CWD/fontconfig.liberation.diff.gz | patch -p1 --verbose || exit 1

# Hardcode the default font search path rather than having fontconfig figure
# it out (and possibly follow symlinks, or index ugly bitmapped fonts):
zcat $CWD/fontconfig.font.dir.list.diff.gz | patch -p1 --verbose --backup --suffix=.orig || exit 1

# Add some patches from the Fedora SRPM:
zcat $CWD/fedora-patches/fontconfig-sleep-less.patch.gz | patch -p1 --verbose || exit 1
#zcat $CWD/fedora-patches/fontconfig-required-freetype-version.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/fedora-patches/fontconfig-score-hint-on-match.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/fedora-patches/fontconfig-fix-1744377.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/fedora-patches/fontconfig-drop-lang-from-pkgkit-format.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/fedora-patches/fontconfig-sysroot.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/fedora-patches/fontconfig-read-latest-cache.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/fedora-patches/fontconfig-mt.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/fedora-patches/fontconfig-fix-test.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/fedora-patches/fontconfig-fix-assertion.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/fedora-patches/fontconfig-fix-dtd.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/fedora-patches/fontconfig-fix-dtd-id.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/fedora-patches/fontconfig-0-timestamp-dir-on-sb.patch.gz | patch -p1 --verbose || exit 1

# Slackware ARM patches:
#for pf in \
#   fontconfig-disable-network-required-test.patch.xz \
#   fontconfig-hotfix.patch.xz \
#      auto_apply_patch $PORTCWD/sources/$pf || failpatch
#done

# Configure:
autoreconf -vif
CFLAGS=$SLKCFLAGS \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --mandir=/usr/man \
   --sysconfdir=/etc \
   --with-templatedir=/etc/fonts/conf.avail \
   --with-baseconfigdir=/etc/fonts \
   --with-configdir=/etc/fonts/conf.d \
   --with-xmldir=/etc/fonts \
   --enable-libxml2 \
   --enable-static=no \
   --localstatedir=/var \
   --build=${SLK_ARCH_BUILD} || exit 1

# Build:
# -i because it needs some doc tools which aren't available during
# bootstrapping the new OS.
#make -i $NUMJOBS ##### || failmake
make $NUMJOBS || make || failmake

# Install into package:
make install DESTDIR=$PKG

# Upstream has changed the default templatedir to /usr/share/fontconfig/conf.avail.
# This change, if accepted, would break any existing font package containing a
# conf.avail directory.  The safest thing to do is to keep things in the
# traditional location, but put a link in the new place so that font packages
# following the new standard location will work.  Let's hear it for being
# "more correct" at the expense of having things "just work"!
mkdir -p $PKG/usr/share/fontconfig
( cd $PKG/usr/share/fontconfig ; ln -vsf ../../../etc/fonts/conf.avail . )

# And onto the filesystem so we can run fc-cache which needs the
# shared object created by this package:
#make -i install
#ldconfig

# This got removed by Pat, but I'll leave it in to save me a few mins
# if it ever gets back.
## Don't scan the whole X11 fonts dir -- we only want scalable fonts as a default.
#( cd $PKG/etc/fonts
## This patch doesn't work for ARMedslack, so let's just do it with sed:
##  zcat $CWD/fontconfig.fc_fontdir.diff.gz | patch -p1 --backup --suffix=.orig || exit 1
##  rm fonts.conf.orig
#  sed -i 's?/usr/share/fonts?/usr/lib/X11/fonts/TTF?g'     fonts.conf
#  sed -i 's?/usr/lib/X11/fonts?/usr/lib/X11/fonts/Type1?g' fonts.conf
#) || exit 1

# Copy docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a AUTHORS COPYING INSTALL NEWS README \
      $PKG/usr/doc/fontconfig-$VERSION
# You can shop for this kind of stuff in the source tarball.
rm -rf $PKG/usr/share/doc
rmdir $PKG/usr/share 2>/dev/null
changelogliposuction ChangeLog $PKGNAM $VERSION

mkdir -p $PKG/var/log/setup
cat $CWD/setup.05.fontconfig > $PKG/var/log/setup/setup.05.fontconfig
chmod 755 $PKG/var/log/setup/setup.05.fontconfig

# Set up the default options in /etc/fonts/conf.d:
(  cd $PKG/etc/fonts/conf.d
   for fontconf in \
        20-fix-globaladvance.conf \
        20-unhint-small-vera.conf \
        30-metric-aliases.conf \
        40-nonlatin.conf \
        45-latin.conf \
        49-sansserif.conf \
        50-user.conf \
        51-local.conf \
        60-latin.conf \
        65-fonts-persian.conf \
        65-nonlatin.conf \
        69-unifont.conf \
        80-delicious.conf \
        90-synthetic.conf ; do
          [ -r ../conf.avail/$fontconf ] && ln -vsf ../conf.avail/$fontconf .
        done
   if [ ! $? = 0 ]; then
     exit 1
   fi
)
if [ ! $? = 0 ]; then
  echo "Missing /etc/fonts/$fontconf default.  Exiting"
  exit 1
fi

# This is a really ugly default.  If you like it, you'll have to link this
# one yourself:
rm -f $PKG/etc/fonts/conf.d/10-hinting-slight.conf

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

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