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

VERSION=1.01
ARCH=i386
BUILD=1

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
fi

echo "+===========+"
echo "| xpdf-$VERSION |"
echo "+===========+"
cd $TMP
tar xzvf $CWD/xpdf-$VERSION.tar.gz
cd xpdf-$VERSION
zcat $CWD/xpdfrc.diff.gz | patch -p1 --verbose --backup --suffix=.orig
CXXFLAGS="-O2 -march=i386 -mcpu=i686" \
CFLAGS="-O2 -march=i386 -mcpu=i686" \
./configure \
  --prefix=/usr/X11R6 \
  --with-gzip \
  --with-freetype2-includes=/usr/X11R6/include/freetype2
make
mkdir -p $PKG/usr/X11R6/bin
chown root.bin $PKG/usr/X11R6/bin
cat xpdf/pdffonts > $PKG/usr/X11R6/bin/pdffonts
cat xpdf/pdfimages > $PKG/usr/X11R6/bin/pdfimages
cat xpdf/pdfinfo > $PKG/usr/X11R6/bin/pdfinfo
cat xpdf/pdftopbm > $PKG/usr/X11R6/bin/pdftopbm
cat xpdf/pdftops > $PKG/usr/X11R6/bin/pdftops
cat xpdf/pdftotext > $PKG/usr/X11R6/bin/pdftotext
cat xpdf/xpdf > $PKG/usr/X11R6/bin/xpdf
chmod 755 $PKG/usr/X11R6/bin/*
chown -R root.bin $PKG/usr/X11R6/bin
strip $PKG/usr/X11R6/bin/*
mkdir -p $PKG/usr/X11R6/man/man1
cat doc/pdffonts.1 | gzip -9c > $PKG/usr/X11R6/man/man1/pdffonts.1.gz
cat doc/pdfimages.1 | gzip -9c > $PKG/usr/X11R6/man/man1/pdfimages.1.gz
cat doc/pdfinfo.1 | gzip -9c > $PKG/usr/X11R6/man/man1/pdfinfo.1.gz
cat doc/pdftopbm.1 | gzip -9c > $PKG/usr/X11R6/man/man1/pdftopbm.1.gz
cat doc/pdftops.1 | gzip -9c > $PKG/usr/X11R6/man/man1/pdftops.1.gz
cat doc/pdftotext.1 | gzip -9c > $PKG/usr/X11R6/man/man1/pdftotext.1.gz
cat doc/xpdf.1 | gzip -9c > $PKG/usr/X11R6/man/man1/xpdf.1.gz
mkdir -p $PKG/usr/X11R6/man/man5
cat doc/xpdfrc.5 | gzip -9c > $PKG/usr/X11R6/man/man5/xpdfrc.5.gz
mkdir -p $PKG/usr/X11R6/etc
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
chown -R root.root $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.tar.gz
cd xpdf-cyrillic
zcat $CWD/xpdf-cyrillic.diff.gz | patch -p1 --verbose --backup --suffix=.orig
mkdir -p $PKG/usr/X11R6/share/xpdf/cyrillic
cat KOI8-R.unicodeMap > $PKG/usr/X11R6/share/xpdf/cyrillic/KOI8-R.unicodeMap
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