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

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

# Detect whether we're building for /patches:
# This function sets the variable 'SLACKPATCHING'
slack_findpkgstore_is_stablerelease

# Temporary build locations:
shm_tmp # Use /dev/shm if >8GB RAM is available & not mounted 'noexec'
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Extract source:
tar xf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-$VERSION || failextract
slackhousekeeping

# Patches from Fedora and Debian:
cat $CWD/patches/02_indic_names.patch | patch -p1 --verbose || exit 1
cat $CWD/patches/04_mgopen_fonts.patch | patch -p1 --verbose || exit 1
cat $CWD/patches/disable-crbug1004354.patch | patch -p1 --verbose || exit 1
cat $CWD/patches/fontconfig-sleep-less.patch | patch -p1 --verbose || exit 1
cat $CWD/patches/fontconfig-drop-lang-from-pkgkit-format.patch | patch -p1 --verbose || exit 1

# Prefer Liberation for sans, serif, and monospace, as we have for many years:
cat $CWD/fontconfig.liberation.diff | patch -p1 --verbose || exit 1

# Do not include the "xdg" element in fonts.conf, as it uses all of the fonts
# under /usr/share/fonts, including the 75dpi and 100dpi bitmap fonts, unlike
# earlier versions of fontconfig. However, then we need to add the directory
# ~/.local/share/fonts which was previously scanned.
cat $CWD/fonts.conf.in.no.usr.share.fonts.add.local.share.fonts.patch | patch -p1 --verbose || exit 1

# Do not emit a warning if FcInit() is not called. Not calling it can cause
# a small memory leak, but the warning causes various programs to fail.
cat $CWD/no.fcinit.warning.patch | patch -p1 --verbose || exit 1

# Configure:
mkdir meson-build
pushd meson-build
export CFLAGS="$SLKCFLAGS"
export CXXFLAGS="$SLKCFLAGS"
meson setup \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --mandir=/usr/man \
   --datadir=/usr/share \
   --sysconfdir=/etc \
   --localstatedir=/var \
   --buildtype=release \
   -D cache-build=disabled \
   -D default-hinting=medium \
   -D default-sub-pixel-rendering=none \
   -D doc-html=disabled \
   -D doc-pdf=disabled \
   -D doc-txt=disabled \
   -D bitmap-conf=noinstall \
   -D default-fonts-dirs=/usr/share/fonts/OTF,/usr/share/fonts/TTF,/usr/share/fonts/Type1,/usr/share/fonts/Speedo,/usr/share/fonts/cyrillic,/usr/share/fonts/misc \
   .. || failconfig

# Build:
"${NINJA:=ninja}" $NUMJOBS || ${NINJA} || failmake

# Install into package:
DESTDIR=$PKG $NINJA install || failinstall
popd
mkdir -p $PKG/var/log/setup
install -vpm755 $CWD/setup.05.fontconfig $PKG/var/log/setup/setup.05.fontconfig

# Don't ship static library:
rm -f $PKG/usr/lib${LIBDIRSUFFIX}/libfontconfig.a

# Not convinced.
#( cd $PKG/etc/fonts/conf.d ; ln -sf ../../../usr/share/fontconfig/conf.avail/70-no-bitmaps-except-emoji.conf . )

# We did not previously link a hinting, subpixel, or lcdfilter conf file.
# Let's not do that here, either. Let the user choose to link that themselves
# without every fontconfig update stomping on it:
rm -f $PKG/etc/fonts/conf.d/10-hinting-*
rm -f $PKG/etc/fonts/conf.d/10-sub-pixel-*
rm -f $PKG/etc/fonts/conf.d/11-lcdfilter-*

# You can shop for this kind of stuff in the source tarball.
rm -rf $PKG/usr/share/doc

# Add documentation:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav \
  AUTHORS* COPYING* ChangeLog INSTALL* NEWS* README* \
  $PKG/usr/doc/$PKGNAM-$VERSION
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

# 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           # set standard Slackware file/dir permissions and ownerships
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
