### this file is sourced not run
PKGVER=1.16.1
PKGBUILD=1
PKGARCH=i686

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

SLACKREQ='freetype2_32 lcms2_32 libjpeg-turbo32 libopenjpeg2_32 libX11_32 freeglut_32 curl32'
SLACKDESC="mupdf32: mupdf32 $PKGVER (PDF and XPS Viewer)  
mupdf32:  
mupdf32: MuPDF is a lightweight PDF, XPS, and E-book viewer.
mupdf32: It consists of a software library, command line tools, and viewers.
mupdf32: 
mupdf32: 
mupdf32: 
mupdf32: This contains 32-bit version of the library.
mupdf32: 
mupdf32: 
mupdf32:
"

### 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.16.1-shared_libs-1.patch &&

	# output correct 32-bit format for conversion 
	sed -i -e 's|-b binary|& --oformat=elf32-i386|' Makefile 
}

### package
pkg_package() {
	rm -rf ./usr/bin ./usr/include ./usr/share
}

### build
pkg_build() {
	cd /tmp/mupdf* &&
	export USE_SYSTEM_LIBS=yes 
	
	# 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 &&
	pkg_build_slackdesc
}
