### this file is sourced not run
PKGVER=1.13.0
PKGBUILD=1
PKGARCH=x86_64

# source: post BLFS 8.2 2018-06-08
TARBALL=mupdf-$PKGVER-source.tar.gz
MD5SUM=447bc5c3305efe9645e12fce759e0198
SRC_URL=http://www.mupdf.com/downloads/archive/$TARBALL
BUNDLE=

SLACKREQ='freetype2 lcms2 libjpeg-turbo libopenjpeg2 libX11 freeglut curl' # 
SLACKDESC="mupdf: mupdf $PKGVER (PDF and XPS Viewer)  
mupdf:  
mupdf: MuPDF is a lightweight PDF, XPS, and E-book viewer.
mupdf: It consists of a software library, command line tools, and viewers.
mupdf: 
mupdf: License: GNU Affero GPL
mupdf: http://www.mupdf.com
mupdf: http://artifex.com/
mupdf: git://git.ghostscript.com/mupdf.git
mupdf: 
mupdf: 
"

### default pkg_download
### prepare
pkg_prepare() {
	pkg_prepare_default &&

	# BLFS 8.2 - remove 3rd party libs and use shared libs
	# keep jbig2dec, mujs
	# keep freeglut for mupdf-gl, reason: unicode, copy+paste see https://github.com/ArtifexSoftware/mupdf/commit/896830e23e8f94b17bdf386c191a885c972ea8a8
	# keep lcms2, reason: API see thirdparty/lcms2/doc/WhyThisFork.txt and https://bugzilla.redhat.com/show_bug.cgi?id=1553915
	cd tmp/mupdf* &&
	rm -rf thirdparty/curl     &&
	#rm -rf thirdparty/freeglut &&
	rm -rf thirdparty/freetype && #
	rm -rf thirdparty/harfbuzz &&
	rm -rf thirdparty/jpeg     && #
	#rm -rf thirdparty/lcms2    &&
	rm -rf thirdparty/libjpeg  && #
	rm -rf thirdparty/openjpeg && #
	rm -rf thirdparty/zlib     && #
	patch -Np1 -i ../pkg/mupdf-1.13.0-shared_libs-1.patch &&
	patch -p1 -i ../../../pkg/0001-fix-build-on-big-endian.patch -d thirdparty/lcms2
}

### default pkg_package
### build
pkg_build() {
	cd /tmp/mupdf* &&
	
	# Do not add CJK fonts to libmupdf.a. This section affects the library file size.
	# See also XCFLAGS TOFU font defines in include/mupdf/fitz/config.h. Those flags affect the executable file size.
	# The default make includes the base 14 PDF fonts, DroidSansFallback from Android for CJK, and Charis SIL from SIL for epub/html.
	## Keep sil & urw fonts only.
	for p in droid han noto; do
		echo resources/fonts/$p
		rm -rf resources/fonts/$p
		[ -d resources/fonts/$p ] && return 1
		mkdir resources/fonts/$p
	done &&

	sed -i -e 's|-O2|-Os|g' Makerules && # build with -Os &&
	
	# Build mupdf-{x11,gl} and mutool.
	# XCFLAGS to omit CJK fonts and more.
	# For TOFU font defines see fitz/noto.c, include/mupdf/fitz/config.h
	make $MAKEFLAGS         \
		XCFLAGS="-DTOFU -DTOFU_CJK -DTOFU_EMOJI -DTOFU_HISTORIC -DTOFU_SYMBOL" \
		prefix=/usr         \
		build=release       \
		docdir=/usr/share/doc/mupdf-$PKGVER \
		largefile=yes       \
		libdir=/usr/$LIBDIR \
		all install  &&

	# Link as cups-filters configure expects; but the desktop app is mupdf-gl.
	ln -sfv mupdf-x11 /usr/bin/mupdf &&
	cp -a ../pkg/usr/* /usr &&
	pkg_build_slackdesc
}
