#!/bin/bash

# freetype.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>

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

# Paths to skeleton port's source & real Slackware source tree:
#export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export CWD=$SLACKSOURCE/$PKGNAM
export PORTCWD=$PWD

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

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.xz
#tar xvvf $PORTCWD/sources/$PKGNAM-$VERSION.tar.xz
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

# This breaks far too many things.  Freetype2 developers will have to get their
# punishment on someone else's distribution.
zcat $CWD/freetype.illadvisederror.diff.gz | patch -p1 --verbose || exit 1

# Security fixes:
zcat $CWD/freetype.CVE-2017-8287.diff.gz | patch -p1 --verbose || exit 1
zcat $CWD/freetype.CVE-2020-15999.diff.gz | patch -p1 --verbose || exit 1

# Nasty little hack to test something out:
#( cd /usr/include
#  ln -vfs harfbuzz/hb-ft.h . )

# Configure:
# Added            --with-harfbuzz=no  for ARM to test.
CFLAGS="$SLKCFLAGS" \
make \
  setup CFG="--prefix=/usr --libdir=/usr/lib${LIBDIRSUFFIX} --build=$ARCH-slackware-linux-gnueabi" ## --with-harfbuzz=no" || exit 1

# Build:
make $NUMJOBS || make || failmake

# Install into package:
make install DESTDIR=$PKG

# Can't imagine a lot of use for this:
rm -fv $PKG/usr/lib/*.a

# This shouldn't be needed (apps should pick up -I/usr/include/freetype2 from
# `freetype-config --cflags` while compiling), but it's so often reported as
# a bug that I'll give in to the point.  Now that Freetype1 is pretty much gone
# having this link shouldn't hurt anything.  Try not to rely on it, though.
mkdir -vpm755 $PKG/usr/include
( cd $PKG/usr/include
  rm -rf freetype
  ln -vsf freetype2 freetype )

# Install docs:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -favv docs README \
         $PKG/usr/doc/$PKGNAM-$VERSION
rm -rf $PKG/usr/doc/freetype-$VERSION/docs/reference

# Move man pages in to Slackware location:
mv -fv $PKG/usr/share/man $PKG/usr/

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
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
