#!/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/$PACKAGE
export PORTCWD=$PWD

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

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar jxvvf $CWD/$PACKAGE-$VERSION.tar.bz2
cd $PACKAGE-$VERSION
slackhousekeeping

# Prefer DejaVu to Vera:
zcat $CWD/fontconfig.dejavu.diff.gz | patch -p1 --verbose || failpatch

# This should remain in /usr/X11R6/ until the X.Org --prefix
# is changed to /usr.  Fair warning.  ;-)
FCPREFIX=/usr/X11R6

# Configure:
CFLAGS=$SLKCFLAGS \
./configure \
   --prefix=$FCPREFIX \
   --sysconfdir=/etc || failconfig

# Build:
make -i || failmake

# Install into package:
make -i install DESTDIR=$PKG

# pkgconfig is stoopid, so we have to make sure this ends up
# in the right place or it won't find 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/$PACKAGE-$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

#mv $PKG/etc/fonts/local.conf $DESTDIR/etc/fonts/local.conf.new
rm -f $PKG/etc/fonts/local.conf

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

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/X11R6/bin/fc-cache ]; then
   /usr/X11R6/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

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

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