#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-xpdf
rm -rf $PKG
mkdir -p $PKG

VERSION=3.01
ARCH=${ARCH:-i486}
BUILD=2

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

cd $TMP
rm -rf xpdf-$VERSION
tar xzvf $CWD/xpdf-$VERSION.tar.gz
cd xpdf-$VERSION

#( cd xpdf
#  cat $CWD/xpdf-3.00pl1.patch | patch -p0 --verbose || exit
#  cat $CWD/xpdf-3.00pl2.patch | patch -p0 --verbose || exit
#  cat $CWD/xpdf-3.00pl3.patch | patch -p0 --verbose || exit
#)

chown -R root.root .
zcat $CWD/xpdfrc.diff.gz | patch -p1 --verbose --backup --suffix=.orig || exit
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr/X11R6 \
  --with-gzip \
  --with-freetype2-includes=/usr/include/freetype2
make -j3
make install DESTDIR=$PKG
( 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
)
chmod 755 $PKG/usr/X11R6/bin/*
chown -R root.bin $PKG/usr/X11R6/bin
gzip -9 $PKG/usr/X11R6/man/man?/*
cat doc/sample-xpdfrc > $PKG/usr/X11R6/etc/xpdfrc
mkdir -p $PKG/usr/doc/xpdf-$VERSION
cp -a \
  ANNOUNCE CHANGES COPYING INSTALL README \
  $PKG/usr/doc/xpdf-$VERSION
chmod 644 $PKG/usr/doc/xpdf-$VERSION/*
# Add cyrillic support.  Can't leave that out.
tar xzvf $CWD/xpdf-cyrillic-2003-jun-28.tar.gz
cd xpdf-cyrillic
zcat $CWD/xpdf-cyrillic.diff.gz | patch -p1 --verbose --backup --suffix=.orig || exit
mkdir -p $PKG/usr/X11R6/share/xpdf/cyrillic
cat Bulgarian.nameToUnicode > $PKG/usr/X11R6/share/xpdf/cyrillic/Bulgarian.nameToUnicode
cat KOI8-R.unicodeMap > $PKG/usr/X11R6/share/xpdf/cyrillic/KOI8-R.unicodeMap
cat add-to-xpdfrc >> $PKG/usr/X11R6/etc/xpdfrc
# Add latin2 support:
tar xzvf $CWD/xpdf-latin2-2002-oct-22.tar.gz
cd xpdf-latin2
zcat $CWD/xpdf-latin2.diff.gz | patch -p1 --verbose --backup --suffix=.orig || exit
mkdir -p $PKG/usr/X11R6/share/xpdf/latin2
cat Latin2.unicodeMap > $PKG/usr/X11R6/share/xpdf/latin2/Latin2.unicodeMap
cat add-to-xpdfrc >> $PKG/usr/X11R6/etc/xpdfrc
# Add greek support:
tar xzvf $CWD/xpdf-greek-2003-jun-28.tar.gz
cd xpdf-greek
zcat $CWD/xpdf-greek.diff.gz | patch -p1 --verbose --backup --suffix=.orig || exit
mkdir -p $PKG/usr/X11R6/share/xpdf/greek
cat ISO-8859-7.unicodeMap > $PKG/usr/X11R6/share/xpdf/greek/ISO-8859-7.unicodeMap
cat Greek.nameToUnicode > $PKG/usr/X11R6/share/xpdf/greek/Greek.nameToUnicode
cat add-to-xpdfrc >> $PKG/usr/X11R6/etc/xpdfrc
# Package description:
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/xpdf-$VERSION-$ARCH-$BUILD.tgz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/xpdf-$VERSION
  rm -rf $PKG
fi

