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

VERSION=2.4.2
export ARCH=${ARCH:-x86_64}
BUILD=${BUILD:-1}
DISTRO=${DISTRO:-slamd64}
NUMJOBS=${NUMJOBS:-10}

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 -fPIC"
fi

if [ $DISTRO = slackware ]; then
	PKGARCH=$ARCH
else
	PKGARCH=${ARCH}_${DISTRO}
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

LDFLAGS="-L/usr/lib$LIBSUFFIX" \
CFLAGS="$SLKCFLAGS -m32" \
./configure \
  --prefix=$FCPREFIX \
	--libdir=$FCPREFIX/lib$LIBSUFFIX \
	--disable-docs \
  --mandir=/usr/man \
  --sysconfdir=/etc \
  --localstatedir=/var

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

# These files are already provided by the 64 bit package
rm -rf $PKG/etc

echo "Installed"

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