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

VERSION=2.1.9
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}

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

PKG=$TMP/package-freetype
rm -rf $PKG
mkdir -p $PKG

cat << EOF

***************************************************
* Building freetype-$VERSION
***************************************************

EOF

# It seems prudent to place this in /usr rather than /usr/X11R6, as *many* source bits
# won't find ft2build.h in /usr/X11R6/include without some patching.
# Therefore, --prefix=/usr must be the ad-hoc standard.  Another
# rationale:  /usr is also the prefix for freetype1 (for as long as that sticks around),
# and putting them in different prefixes causes problems.  Also, we're bumping the -march
# from i386 to i486, as I can't imagine too many people are running the latest Slackware
# with X on a 386 in the year 2002(+).  If there are, maybe they can get away with
# running an earlier version of X.  :-) 
cd $TMP
rm -rf freetype-$VERSION
tar xjvf $CWD/freetype-$VERSION.tar.bz2
cd freetype-$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 {} \;

# This breaks far too many things.  Freetype2 developers will have to get their
# punishment on someone else's distribution.
zcat $CWD/freetype.illadvisederror.diff.gz | patch -p1 --verbose || exit 1

# The line below enables code patented by Apple, so don't uncomment it
# unless you have a license to use the code and take all legal responsibility
# for doing so.
# Please see this web site for more details:
#   http://www.freetype.org/patents.html
#zcat $CWD/freetype.bytecode.interpreter.diff.gz | patch -p1 --verbose || exit 1

# Another sky-is-falling bug:  ;-)
zcat $CWD/freetype-2.1.9-CVE-2006-1861-strlen.patch.gz | patch -p1 --verbose || exit 1

chown -R root:root .
CFLAGS="$SLKCFLAGS" make setup CFG="--prefix=/usr $ARCH-slackware-linux"
make -j4 || exit 1
make install DESTDIR=$PKG

# This shouldn't be needed (apps should pick up -I/usr/include/freetype2 from
# `freetype-config --cflags` while compiling), but it's so often reported as a bug that
# I'll give in to the point.  Now that Freetype1 is pretty much gone having this link
# shouldn't hurt anything.  Try not to rely on it, though.
mkdir -p $PKG/usr/include
( cd $PKG/usr/include
  rm -rf freetype
  ln -sf freetype2/freetype .
)

( 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
)

mkdir -p $PKG/usr/doc/freetype-$VERSION
cp -a \
  docs \
  README \
  $PKG/usr/doc/freetype-$VERSION
# too much
rm -rf $PKG/usr/doc/freetype-$VERSION/reference

mkdir $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# old
#mkdir -p $PKG/usr/X11R6/lib/X11/doc
#cp -a docs $PKG/usr/X11R6/lib/X11/doc/freetype-$VERSION
#rm -rf $PKG/usr/X11R6/lib/X11/doc/freetype-$VERSION/reference

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