# Maintainer: Christian Hesse <mail@eworm.de>
# Maintainer: Robin Candau <antiz@archlinux.org>
# Contributor: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Brad Fanella <bradfanella@archlinux.us>
# Contributor: Stefan Husmann <stefan-husmann@t-online.de>
# Contributor: Pierre-Paul Paquin <pierrepaulpaquin@gmail.com>
# Contributor: xduugu

pkgbase=mupdf
pkgname=(libmupdf mupdf mupdf-gl mupdf-tools python-mupdf)
pkgver=1.24.1
pkgrel=2
pkgdesc='Lightweight PDF and XPS viewer'
arch=('x86_64' 'aarch64')
url='https://mupdf.com/'
license=('AGPL-3.0-or-later')
makedepends=(
  'clang'
  'curl'
  'desktop-file-utils'
  'freeglut'
  'freetype2'
  'git'
  'glu'
  'gumbo-parser'
  'harfbuzz'
  'jbig2dec'
  # 'lcms2'  # requires fork (not the version we are packaging)
  'leptonica'
  'libjpeg'
  'libx11'
  'libxext'
  'mesa-libgl'
  # 'mujs'  # is hardcoded and needs a patch to debundle
  'openjpeg2'
  'openssl'
  'python'
  'swig'
  'tesseract'
  'zlib'
  'unzip'
)
options=('staticlibs')
source=(
  git+https://github.com/ArtifexSoftware/mupdf.git#tag=$pkgver
  git+https://github.com/ArtifexSoftware/mujs.git
  git+https://github.com/ArtifexSoftware/thirdparty-lcms2.git
  git+https://github.com/ArtifexSoftware/extract.git
  $pkgbase.desktop
  $pkgbase.xpm
  $pkgbase-1.23.9-cpp_ldflags.patch
  $pkgbase-1.23.9-install_targets.patch
)
sha256sums=('bba6c0b3f808a5233807f3777eb98f03d7740aa2125c1dd6c412180f4cd8a0b2'
            'SKIP'
            'SKIP'
            'SKIP'
            'ccff66979249bd4ab4ba8918660f194eb90eb0ae231b16e36a6cecdcf471883f'
            'a435f44425f5432c074dee745d8fbaeb879038ec1f1ec64f037c74662f09aca8'
            '152222c0bc066404d9e38be7abbba20748b0a4bbc9cb30403d19e3b176ab257f'
            'ceb2b16a12bf193d514969f995dfca4c2d12d489c2690928db2db4afe09f6c8c')

prepare() {
  cd $pkgbase

  # use our LDFLAGS when building the C++ bindings to have full RELRO
  patch -Np1 -i ../$pkgbase-1.23.9-cpp_ldflags.patch
  # alter install-shared-* targets to not call one another (which fails on installing headers twice) and install libmupdfcpp.so with soname postfix
  patch -Np1 -i ../$pkgbase-1.23.9-install_targets.patch

  # init submodules and disable the ones we don't need, we want our system libraries instead
  git submodule init
  rm -rvf thirdparty/{curl,freeglut,freetype,gumbo-parser,harfbuzz,jbig2dec,leptonica,libjpeg,openjpeg,tesseract,zlib}
  git submodule deinit thirdparty/curl
  git submodule deinit thirdparty/freeglut
  git submodule deinit thirdparty/freetype
  git submodule deinit thirdparty/gumbo-parser
  git submodule deinit thirdparty/harfbuzz
  git submodule deinit thirdparty/jbig2dec
  git submodule deinit thirdparty/leptonica
  git submodule deinit thirdparty/libjpeg
  git submodule deinit thirdparty/openjpeg
  git submodule deinit thirdparty/tesseract
  git submodule deinit thirdparty/zlib
  # update required third party libraries
  git config submodule.thirdparty/mujs.url "$srcdir/mujs" # is hardcoded and needs a patch to debundle
  git config submodule.thirdparty/lcms2.url "$srcdir/thirdparty-lcms2" # requires fork (not the version we are packaging)
  git config submodule.thirdparty/extract.url "$srcdir/extract" # we do not package this one
  git -c protocol.file.allow=always submodule update

  {
    printf "LINUX_OR_OPENBSD := yes\n"  # required so that Makefile sets soname symlink
    printf "USE_SYSTEM_CURL := yes\n"
    printf "USE_SYSTEM_FREETYPE := yes\n"
    printf "USE_SYSTEM_GLUT := yes\n"
    printf "USE_SYSTEM_GUMBO := yes\n"
    printf "USE_SYSTEM_HARFBUZZ := yes\n"
    printf "USE_SYSTEM_JBIG2DEC := yes\n"
    printf "USE_SYSTEM_JPEGXR := yes\n"  # not used without HAVE_JPEGXR
    printf "USE_SYSTEM_LCMS2 := no\n"  # need lcms2-art fork
    printf "USE_SYSTEM_LEPTONICA := yes\n"
    printf "USE_SYSTEM_LIBJPEG := yes\n"
    printf "USE_SYSTEM_LIBS := yes\n"
    printf "USE_SYSTEM_MUJS := no\n"  # needs patch to debundle
    printf "USE_SYSTEM_OPENJPEG := yes\n"
    printf "USE_SYSTEM_TESSERACT := yes\n"
    printf "USE_SYSTEM_ZLIB := yes\n"
    printf "USE_TESSERACT := yes\n"
  } > user.make
}

build() {
  cd $pkgbase
  # Enforce -j1 to avoid concurrency issue during build
  make -j1 VENV_FLAG= shared=yes build=release libs apps c++ python
}

package_libmupdf() {
  pkgdesc='Library for Lightweight PDF and XPS viewer'
  depends=(
    'freetype2'
    'gcc-libs'
    'glibc'
    'gumbo-parser'
    'harfbuzz'
    'jbig2dec'
    'leptonica'
    'libjpeg'
    'openjpeg2'
    'tesseract'
    'zlib'
  )

  cd $pkgbase
  make prefix=/usr DESTDIR="$pkgdir" SO_INSTALL_MODE=755 install-shared-c install-shared-c++
}

package_python-mupdf() {
  pkgdesc='Library for Lightweight PDF and XPS viewer - Python bindings'
  depends=(
    'gcc-libs'
    'glibc'
    'libmupdf'
    'python'
  )

  cd $pkgbase
  make prefix=/usr DESTDIR="$pkgdir" SO_INSTALL_MODE=755 install-shared-python
}

package_mupdf() {
  pkgdesc='Lightweight PDF and XPS viewer'
  depends=(
    'desktop-file-utils'
    'glibc'
    'libmupdf'
    'libx11'
    'libxext'
  )

  cd $pkgbase

  install -vDm 0755 build/shared-release/$pkgbase-x11 "$pkgdir"/usr/bin/$pkgbase

  install -vDm 0644 docs/man/$pkgbase.1 -t "$pkgdir"/usr/share/man/man1/

  install -vDm 0644 README COPYING CHANGES -t "$pkgdir"/usr/share/doc/$pkgbase/

  install -vDm 0644 ../$pkgbase.desktop -t "$pkgdir"/usr/share/applications/
  install -vDm 0644 ../$pkgbase.xpm -t "$pkgdir"/usr/share/pixmaps/
}

package_mupdf-gl() {
  pkgdesc='Lightweight PDF and XPS viewer with OpenGL backend'
  conflicts=('mupdf')
  provides=("mupdf=${pkgver}")
  depends=(
    'desktop-file-utils'
    'glibc'
    'glut'
    'libgl'
    'libmupdf'
    'openssl'
  )

  cd $pkgbase

  install -vDm 0755 build/shared-release/$pkgbase-gl "$pkgdir"/usr/bin/$pkgbase

  install -vDm 0644 docs/man/$pkgbase.1 -t "$pkgdir"/usr/share/man/man1/

  install -vDm 0644 README COPYING CHANGES -t "$pkgdir"/usr/share/doc/$pkgbase/

  install -vDm 0644 ../$pkgbase.desktop -t "$pkgdir"/usr/share/applications/
  install -vDm 0644 ../$pkgbase.xpm -t "$pkgdir"/usr/share/pixmaps/
}

package_mupdf-tools() {
  pkgdesc='Tools for Lightweight PDF and XPS viewer'
  depends=(
    'glibc'
    'libmupdf'
    'openssl'
  )

  cd $pkgbase

  install -vDm 0755 build/shared-release/{mutool,muraster} -t "$pkgdir"/usr/bin/

  install -vDm 0644 docs/man/mutool.1 -t "$pkgdir"/usr/share/man/man1/

  install -vDm 0644 README COPYING CHANGES -t "$pkgdir"/usr/share/doc/$pkgname/
}
