# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Michael Zhou <zhoumichaely@gmail.com>
# Maintainer: Daniel Sabogal <dsabogalcc@gmail.com>
pkgname=mupdf
pkgver=1.12.0
pkgrel=0
pkgdesc="A lightweight PDF and XPS viewer"
url="http://mupdf.com"
arch="all"
license="AGPL-3.0-or-later"
depends=""
makedepends="freetype-dev jpeg-dev jbig2dec-dev libx11-dev libxext-dev
	openjpeg-dev harfbuzz-dev freeglut-dev"
subpackages="$pkgname-doc $pkgname-dev $pkgname-x11:_x11
	$pkgname-gl:_gl $pkgname-tools:_tools"
options="!check"
source="http://mupdf.com/downloads/archive/$pkgname-$pkgver-source.tar.xz
	shared-lib.patch
	openjpeg-2.1.patch
	"

# secfixes:
#   1.11-r1:
#   - CVE-2017-6060
#   1.10a-r2:
#   - CVE-2017-5991
#   1.10a-r1:
#   - CVE-2017-5896

builddir="$srcdir/$pkgname-$pkgver-source"
prepare() {
	default_prepare

	cd "$builddir"
	for file in thirdparty/*; do
		[ "${file##*/}" != "mujs" ] && rm -rf "$file"
	done
}

build() {
	make prefix=/usr -C "$builddir"
}

package() {
	make prefix=/usr DESTDIR="$pkgdir" \
		-C "$builddir" install

	ln -s libmupdf.so.0 "$pkgdir"/usr/lib/libmupdf.so
	ln -s libmupdfthird.so.0 "$pkgdir"/usr/lib/libmupdfthird.so
}

_x11() {
	pkgdesc="A lightweight PDF and XPS viewer with X11 backend"
	depends=""

	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/mupdf-x11 \
		"$subpkgdir"/usr/bin/
	ln -s /usr/bin/mupdf-x11 "$subpkgdir"/usr/bin/mupdf
}

_gl() {
	pkgdesc="A lightweight PDF and XPS viewer with OpenGL backend"
	depends=""

	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/mupdf-gl \
		"$subpkgdir"/usr/bin/
}

_tools() {
	pkgdesc="Tools for a lightweight PDF and XPS viewer"
	depends=""

	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/mutool \
		"$pkgdir"/usr/bin/mjsgen \
		"$pkgdir"/usr/bin/mujstest \
		"$pkgdir"/usr/bin/muraster \
		"$subpkgdir"/usr/bin/
}

sha512sums="4c9ce81e7b0e2d77e017776fd9f700bc2f695f34b7fd97be7ba113ec1b340e7046c9db7d396abf19a98b1d0c7d72f01ecc1b44fadd250d2d6c6ffdcd9799bc16  mupdf-1.12.0-source.tar.xz
8d3f22908060351e8bb171c7c8248662dd6ac3fb5c765ad0ce33f33c418f6bcfb1982ea8e97b4fefee067c675cac03f76e8b715640dfd3689e8d7738384b6536  shared-lib.patch
3034e7d3248d904cc23e033a6331fc7a48d8f14e85deaa7b7b7bc37d8a3bc6c788e2fd0a866873a69c5f416d8de5b3a5b9efde4747fa50c2ea3c4b8d170aa549  openjpeg-2.1.patch"
