#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# xpdf.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
# 20-Dec-2004
#
# Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

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

# Extract source:
tar xf $CWD/$PKGNAM-*$VERSION*.tar.!(*sign|*asc|*sig)
cd $PKGNAM-$VERSION || failextract

# Apply patches:
zcat $CWD/patches/xpdfrc.diff.gz | patch -p1 --verbose || exit 1

# Source clean up:
slackhousekeeping
# Remove .orig files from the package:
find $PKG -name "*.orig" -exec rm "{}" \;

# Allow build to use CMake 4.x:
export CMAKE_POLICY_VERSION_MINIMUM=3.5

# Configure:
mkdir build
pushd build
  cmake \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DLIB_SUFFIX=${LIBDIRSUFFIX} \
    -DCMAKE_INSTALL_MANDIR=/usr/man \
    -DCMAKE_BUILD_TYPE=Release \
    -DXPDFWIDGET_PRINTING=1 \
    -DSYSTEM_XPDFRC="/etc/xpdfrc" \
    .. || failconfig

# Build:
make $NUMJOBS || make || failmake

# Install into package:
make install DESTDIR=$PKG || failinstall
popd

chmod 0755 $PKG/usr/bin/*

# We'll use the versions of these from the poppler derived work (if they exist here):
for file in pdfinfo pdftops pdftoppm pdftohtml pdftotext pdfimages pdffonts ; do
  rm -fv $PKG/usr/bin/${file}
  rm -fv $PKG/usr/man/man1/${file}.1*
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 $CWD/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.!(*sign|*asc|*sig)
  ( 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 -fav \
   ANNOUNCE CHANGES COPYING* INSTALL README \
   $PKG/usr/doc/xpdf-$VERSION
changelogliposuction ChangeLog $PKGNAM $VERSION

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slack_delete_lafiles # delete usr/lib{,64}/*.la
#slackstriprpaths     # strip rpaths
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
