#!/bin/bash

# xpdf.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 20-Dec-2004

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar zxvvf $CWD/$PACKAGE-$VERSION.tar.gz
cd $PACKAGE-$VERSION
slackhousekeeping

# Apply patches:
patch -p1 --verbose < $CWD/xpdf-3.01pl2.patch || failpatch
zcat $CWD/xpdf-3.01-resizefix.patch.gz | patch -p1 --verbose || failpatch

# Configure:
autoconf
CFLAGS="-I/usr/X11R6/include/Xm $SLKCFLAGS" \
CXXFLAGS="-I/usr/X11R6/include/Xm $SLKCFLAGS" \
./configure \
   --prefix=/usr/X11R6 \
   --with-gzip \
   --with-Xext-includes=/usr/X11R6/include/X11/extensions \
   --with-freetype2-includes=/usr/include/freetype2 || failconfig

# Build:
make || failmake
make xpdf || failmake

# Install into package:
make install DESTDIR=$PKG || failinstall
chmod 755 $PKG/usr/X11R6/bin/*
install -m644 doc/sample-xpdfrc $PKG/usr/X11R6/etc/xpdfrc

# Install the docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -a ANNOUNCE CHANGES COPYING INSTALL README \
      $PKG/usr/doc/$PACKAGE-$VERSION

# Add cyrillic support.  Can't leave that out.
echo "[***] Adding cyrillic support [***]"
tar zxvvf $CWD/xpdf-cyrillic-2003-jun-28.tar.gz
cd xpdf-cyrillic
zcat $CWD/xpdf-cyrillic.diff.gz | patch -p1 --verbose || failpatch
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:
echo "[***] Adding latin2 support [***]"
tar zxvvf $CWD/xpdf-latin2-2002-oct-22.tar.gz
cd xpdf-latin2
zcat $CWD/xpdf-latin2.diff.gz | patch -p1 --verbose || failpatch
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:
echo "[***] Adding greek support [***]"
tar xzvf $CWD/xpdf-greek-2003-jun-28.tar.gz
cd xpdf-greek
zcat $CWD/xpdf-greek.diff.gz | patch -p1 --verbose || failpatch
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

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
