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

VERSION=2.4.2
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 xjvf $CWD/fontconfig-$VERSION.tar.bz2
cd fontconfig-$VERSION
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 {} \;

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 || exit 1

# 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 || exit 1

CFLAGS=$SLKCFLAGS \
./configure \
  --prefix=$FCPREFIX \
  --mandir=/usr/man \
  --sysconfdir=/etc \
  --localstatedir=/var

# Uses a currently non-functional sgml tool, thus '-i':
make -i
make -i install DESTDIR=$PKG

# 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

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

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
)

# Set up the default options in /etc/fonts/conf.d:
(  cd $PKG/etc/fonts/conf.d
   for fontconf in 20-fix-globaladvance.conf 20-lohit-gujarati.conf 20-unhint-small-vera.conf 30-amt-aliases.conf 30-urw-aliases.conf 40-generic.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

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

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