# $Id$
# Maintainer: AndyRTR <andyrtr@archlinux.org>

pkgbase=ghostscript
pkgname=(ghostscript ghostxps ghostpcl)
pkgver=9.22
pkgrel=7
pkgdesc="An interpreter for the PostScript language"
url="https://www.ghostscript.com/"
arch=('x86_64')
license=('AGPL3' 'custom')
depends=('libxt' 'libcups' 'fontconfig' 'zlib' 'libpng' 'libjpeg' 'jbig2dec'
         'libtiff' 'lcms2' 'dbus' 'libpaper' 'ijs' 'openjpeg2')
makedepends=('gtk3' 'gnutls' 'glu' 'freeglut')
# https://github.com/ArtifexSoftware/ghostpdl-downloads/releases
source=(https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs${pkgver/./}/ghostpdl-${pkgver}.tar.xz
        ghostscript-9.22-restore-flushpage.patch)
sha256sums=('f4e46bc1f8285a61ab036007705fc2b532cc40f4448fe6bc156bd130733f306b'
            '22156c892ea27572386f5211f51a10a17079a9974ded9ec9af3f37a93a1bb8ca')

prepare() {
  cd ghostpdl-${pkgver}

  # http://git.ghostscript.com/?p=ghostpdl.git;a=commitdiff;h=19ebb5f1f497b6f2d50fe13d17d3e627dfb6c868
  patch -Np1 --no-backup-if-mismatch -i ../ghostscript-9.22-restore-flushpage.patch

  # force it to use system-libs
  rm -r cups/libs expat ijs jbig2dec jpeg lcms2 libpng openjpeg tiff zlib
  # using tree freetype because of https://bugs.archlinux.org/task/56849

  autoreconf -fvi
}

build() {
  cd ghostpdl-${pkgver}

  ./configure --prefix=/usr \
              --enable-dynamic \
              --with-ijs \
              --with-jbig2dec \
              --with-x \
              --with-drivers=ALL \
              --with-fontpath=/usr/share/fonts/gsfonts \
              --enable-fontconfig \
              --enable-freetype \
              --enable-openjpeg \
              --without-luratech \
              --with-system-libtiff \
              --with-libpaper \
              --disable-compile-inits #--help # needed for linking with system-zlib

  make so-only
}

package_ghostscript() {
  optdepends=('texlive-core:      needed for dvipdf'
              'gtk3:              needed for gsx')

  cd ghostpdl-${pkgver}

  make DESTDIR="${pkgdir}" \
       CUPSSERVERROOT="${pkgdir}$(cups-config --serverroot)" \
       CUPSSERVERBIN="${pkgdir}$(cups-config --serverbin)" \
       soinstall
  ln -s gsc "${pkgdir}"/usr/bin/gs

  # install missing doc files # http://bugs.archlinux.org/task/18023
  install -Dt "${pkgdir}"/usr/share/ghostscript/${pkgver}/doc \
    -m644 doc/{VectorDevices.htm,gs-vms.hlp,gsdoc.el,pscet_status.txt}

  # remove unwanted localized manpages
  rm -r "${pkgdir}"/usr/share/man/de

  install -Dt "${pkgdir}"/usr/share/licenses/${pkgname} -m644 LICENSE
}

package_ghostxps() {
  pkgdesc="${pkgdesc/PostScript/XPS document}"
  depends=("ghostscript=${pkgver}-${pkgrel}")

  cd ghostpdl-${pkgver}

  install -Dt "${pkgdir}"/usr/bin sobin/gxpsc
  ln -s gxpsc "${pkgdir}"/usr/bin/gxps

  install -Dt "${pkgdir}"/usr/lib sobin/libgxps.so.${pkgver}
  ln -s libgxps.so.${pkgver} "${pkgdir}"/usr/lib/libgxps.so.${pkgver%.*}

  install -Dt "${pkgdir}"/usr/share/licenses/${pkgname} -m644 LICENSE
}

package_ghostpcl() {
  pkgdesc="${pkgdesc/PostScript/PCL 6}"
  depends=("ghostscript=${pkgver}-${pkgrel}")

  cd ghostpdl-${pkgver}

  install -Dt "${pkgdir}"/usr/bin sobin/gpcl6c
  ln -sf gpcl6c "${pkgdir}"/usr/bin/gpcl6

  install -Dt "${pkgdir}"/usr/lib sobin/libgpcl6.so.${pkgver}
  ln -s libgpcl6.so.${pkgver} "${pkgdir}"/usr/lib/libgpcl6.so.${pkgver%.*}

  install -Dt "${pkgdir}"/usr/share/licenses/${pkgname} -m644 LICENSE
}