#!/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/$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=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/$PKGNAM-*$VERSION*.tar.gz
cd $PKGNAM-$VERSION
slackhousekeeping

# Apply patches:
zcat $CWD/xpdfrc.diff.gz | patch -p1 --verbose --backup --suffix=.orig || failpatch
cat $CWD/xpdf-3.02pl1.patch | patch -p1 --verbose || exit 1
cat $CWD/xpdf-3.02pl2.patch | patch -p1 --verbose || exit 1
zcat $CWD/xpdf_3.02-1.3.diff.gz | patch -p1 --verbose || exit 1
cat $CWD/xpdf-3.02pl3.patch | 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-gzip \
   --with-freetype2-includes=/usr/include/freetype2 || failconfig

# Build:
make $NUMJOBS || failmake

# Install into package:
make install DESTDIR=$PKG || failmake
mkdir -p $PKG/etc
cat doc/sample-xpdfrc > $PKG/etc/xpdfrc
mkdir -p $PKG/usr/share/applications
cat $CWD/xpdf.desktop > $PKG/usr/share/applications/xpdf.desktop
mkdir -p $PKG/usr/share/icons
cat xpdf/xpdfIcon.xpm > $PKG/usr/share/icons/xpdfIcon.xpm
chmod 755 $PKG/usr/bin/*
gzip -9 $PKG/usr/man/man?/*
mkdir -p $PKG/usr/doc/xpdf-$VERSION
cp -a \
  ANNOUNCE CHANGES COPYING INSTALL README \
  $PKG/usr/doc/xpdf-$VERSION

# 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/${language}.tar.gz
  ( cd ${language}
    zcat $CWD/${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
    mkdir -p $PKG/usr/share/xpdf/$(echo ${language} | cut -f2- -d -)
    cp -a * $PKG/usr/share/xpdf/$(echo ${language} | cut -f2- -d -)
  )  
done

mkdir -p $PKG/install
mv $PKG/etc/xpdfrc $PKG/etc/xpdfrc.new
cat << EOF > $PKG/install/doinst.sh
#!/bin/sh
config() {
  NEW="\$1"
  OLD="\$(dirname \$NEW)/\$(basename \$NEW .new)"
  # If there's no config file by that name, mv it over:
  if [ ! -r \$OLD ]; then
    mv \$NEW \$OLD
  elif [ "\$(cat \$OLD | md5sum)" = "\$(cat \$NEW | md5sum)" ]; then # toss the redundant copy
    rm \$NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}
config etc/xpdfrc.new
EOF
cat $CWD/slack-desc > $PKG/install/slack-desc

# 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
)
# 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
