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

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-0.90 |"
echo "+===========+"
cd $TMP
tar xzvf $CWD/xpdf-0.90.tar.gz
cd xpdf-0.90
CXXFLAGS=-O2 ./configure --prefix=/usr/X11R6 --with-gzip
make
mkdir -p $PKG/usr/X11R6/bin
chown root.bin $PKG/usr/X11R6/bin
mkdir -p $PKG/usr/X11R6/man/man1
mkdir -p $PKG/usr/doc/xpdf
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
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
chown root.bin $PKG/usr/X11R6/bin/*
chmod 755 $PKG/usr/X11R6/bin/*
strip $PKG/usr/X11R6/bin/*
cp -a ANNOUNCE CHANGES README $PKG/usr/doc/xpdf
chown root.root $PKG/usr/doc/xpdf/*
chmod 644 $PKG/usr/doc/xpdf/*

# Build the package:
cd $PKG
tar czvf $TMP/xpdf.tgz .

# Warn of zero-length files:
for file in `find . -type f -print` ; do
 if [ "`filesize $file`" = "0" ]; then
  echo "WARNING: zero length file $file"
 fi
 if [ "`filesize $file`" = "20" ]; then
  echo "WARNING: possible empty gzipped file $file"
 fi
done

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