#!/bin/bash

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

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.?z*
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

# The wonderful extended version of the font so generously
# opened up for free modification and distribution by one
# for the previously proprietary font founderies, and that
# Stepan Roh did such a marvelous job on getting the ball
# rolling with should clearly (IMHO) be the default font:
zcat $CWD/fontconfig.dejavu.diff.gz | patch -p1 --verbose || failpatch

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

# Configure:
CFLAGS=$SLKCFLAGS \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --mandir=/usr/man \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --build=$ARCH-slackware-linux-gnueabi || 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 || failmake

# Install into package:
make install DESTDIR=$PKG

# 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-urw-aliases.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
     ln -sf ../conf.avail/$fontconf . || exit 1
   done
   if [ ! $? = 0 ]; then
     exit 1
   fi
)
if [ ! $? = 0 ]; then
  echo "Missing /etc/fonts/$fontconf default.  Exiting"
  exit 1
fi

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# 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
