#!/bin/sh
CWD=`pwd`
TMP=${TMP:-/tmp}
PKG=$TMP/package-fontconfig

VERSION=2.2.3
export ARCH=${ARCH:-i486}
BUILD=${BUILD:-2}

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

rm -rf $PKG
mkdir -p $PKG

cd $TMP
rm -rf fontconfig-$VERSION
tar xjf $CWD/fontconfig-$VERSION.tar.bz2
cd fontconfig-$VERSION

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

chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;

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

CFLAGS=$SLKCFLAGS \
./configure \
  --prefix=$FCPREFIX \
  --sysconfdir=/etc
# Uses some obscure sgml tool...  thanks a bunch.
make -i
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

mkdir -p $PKG/usr/doc/fontconfig-$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

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# Fix manpages:
if [ -d ${PKG}${FCPREFIX}/man ]; then
  ( cd ${PKG}${FCPREFIX}/man
    for manpagedir in $(find . -type d -name "man*") ; do
      ( cd $manpagedir
        for eachpage in $( find . -type l -maxdepth 1) ; do
          ln -s $( readlink $eachpage ).gz $eachpage.gz
          rm $eachpage
        done
        gzip -9 *.?
      )
    done
  )
fi

mkdir $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
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

cd $PKG
makepkg -l y -c n $TMP/fontconfig-$VERSION-$ARCH-$BUILD.tgz

