#!/bin/bash

# xpdf.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
# 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/$PKGNAM
export PORTCWD=$PWD

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

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac 

# Extract source:
tar xvvf $CWD/$PKGNAM-*$VERSION*.tar.?z
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

# Apply patches:
zcat $CWD/patches/xpdfrc.diff.gz | patch -p1 --verbose || exit 1
zcat $CWD/patches/xpdf.XPDFViewer.diff.gz | patch -p1 --verbose || exit 1
zcat $CWD/patches/xpdf-3.03-CVE-2012-2142.diff.gz | patch -p1 --verbose || exit 1

# Configure:
#autoconf
#CFLAGS="-I/usr/X11R6/include/Xm $SLKCFLAGS" \
#CXXFLAGS="-I/usr/X11R6/include/Xm $SLKCFLAGS" \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --mandir=/usr/man \
   --sysconfdir=/etc \
   --with-freetype2-includes=/usr/include/freetype2 \
   --build=$ARCH-slackware-linux-gnueabi || failconfig

# Build:
make $NUMJOBS || failmake

# Install into package:
make install DESTDIR=$PKG || exit 1

chmod 0755 $PKG/usr/bin/*
gzip -9 $PKG/usr/man/man?/*

# We'll use the versions of these from the poppler derived work (if they exist here):
( cd $PKG/usr/bin
  rm -f pdfinfo pdftops pdftoppm pdftohtml pdftotext pdfimages pdffonts
  cd $PKG/usr/man/man1
  for file in pdfinfo pdftops pdftoppm pdftohtml pdftotext pdfimages pdffonts ; do
    rm -f ${file}.1.gz
  done
)

# Don't clobber /etc/xpdfrc
mkdir -p $PKG/etc
cat doc/sample-xpdfrc > $PKG/etc/xpdfrc
mv $PKG/etc/xpdfrc $PKG/etc/xpdfrc.new

# Add desktop menu entry and icon
mkdir -p $PKG/usr/share/{applications,pixmaps}
cat $CWD/xpdf.desktop > $PKG/usr/share/applications/xpdf.desktop
cat xpdf/xpdfIcon.xpm > $PKG/usr/share/pixmaps/xpdfIcon.xpm

# Add additional language support:
for language in xpdf-arabic xpdf-chinese-simplified xpdf-chinese-traditional xpdf-cyrillic xpdf-greek xpdf-hebrew xpdf-japanese xpdf-korean xpdf-latin2 xpdf-thai xpdf-turkish ; do
  rm -rf ${language}
  tar xf $CWD/lang/${language}*.tar.gz
  ( cd ${language}
    echo "Patching for ${language}:"
    zcat $CWD/lang/${language}.diff.gz | patch -p1 --verbose --backup --suffix=.orig || exit 1
    chown -R root:root .
    find . -type f -exec chmod 644 {} \;
    cat add-to-xpdfrc >> $PKG/etc/xpdfrc.new
    mkdir -p $PKG/usr/share/xpdf/$(echo ${language} | cut -f2- -d -)
    cp -a * $PKG/usr/share/xpdf/$(echo ${language} | cut -f2- -d -)
  ) || exit 1
done
mkdir -p $PKG/usr/doc/xpdf-$VERSION
cp -a \
  ANNOUNCE CHANGES COPYING INSTALL README \
  $PKG/usr/doc/xpdf-$VERSION
changelogliposuction ChangeLog $PKGNAM $VERSION

# 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
slackmp         # run makepkg -l y -c n

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