#!/bin/bash

# fontconfig.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>

# 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=armv4 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.bz2
cd $PKGNAM-$VERSION
slackhousekeeping

FCPREFIX=/usr/

# 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=$FCPREFIX \
   --mandir=/usr/man \
   --sysconfdir=/etc \
   --localstatedir=/var || failconfig

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

# This ought to be easily found here, but it's still safer to move it:
if [ ! -d $PKG/usr/lib/pkgconfig ]; then
   mkdir -p $PKG/usr/lib/pkgconfig
   mv ${PKG}${FCPREFIX}/lib/pkgconfig/* $PKG/usr/lib/pkgconfig
   rmdir ${PKG}${FCPREFIX}/lib/pkgconfig
fi

# 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}${FCPREFIX}/share/doc
rmdir ${PKG}${FCPREFIX}/share

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

mkdir $PKG/install
install -m644 $CWD/slack-desc $PKG/install/
cat << EOF >> $PKG/install/doinst.sh
#!/bin/sh
# Update the X font indexes:
if [ -x /usr/bin/fc-cache ]; then
  /usr/bin/fc-cache -f
fi
# else we'll catch it later with setup.fontconfig :-)
# make links:
EOF

# 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
slackmp         # run makepkg -l y -c n

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