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

# source: BLFS 11.3 updated with our own recipe to make it smaller
TARBALL=mupdf-$PKGVER-source.tar.gz
MD5SUM=62690ebb86e3683d331554433e58c10a
SRC_URL=https://www.mupdf.com/downloads/archive/$TARBALL
BUNDLE=

SLACKREQ='freetype2 harfbuzz libjpeg-turbo libopenjpeg2 libX11 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
### default pkg_prepare
### default pkg_package

### build
pkg_build() {
	cd /tmp/mupdf* &&

	# BLFS 11.3: Fix shared library linkage
	sed -i '/MU.*_EXE. :/{
			s/\(.(MUPDF_LIB)\)\(.*\)$/\2 | \1/
			N
			s/$/ -lmupdf -L$(OUT)/
			}' Makefile &&

	# build options
	cat > user.make << EOF &&
USE_SYSTEM_FREETYPE := yes
USE_SYSTEM_HARFBUZZ := yes
USE_SYSTEM_JBIG2DEC := no
USE_SYSTEM_JPEGXR := no # not used without HAVE_JPEGXR
USE_SYSTEM_LCMS2 := no # need lcms2-art fork
USE_SYSTEM_LIBJPEG := yes
USE_SYSTEM_MUJS := no # build needs source anyway
USE_SYSTEM_OPENJPEG := yes
USE_SYSTEM_ZLIB := yes
USE_SYSTEM_GLUT := no # need freeglut2-art fork
USE_SYSTEM_CURL := yes
USE_SYSTEM_GUMBO := no
EOF

	# 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="-fPIC -DTOFU -DTOFU_CJK -DTOFU_EMOJI -DTOFU_HISTORIC -DTOFU_SYMBOL" \
		prefix=/usr         \
		build=release       \
		docdir=/usr/share/doc/mupdf-$PKGVER \
		largefile=yes       \
		libdir=/usr/$LIBDIR \
		shared=yes          \
		all install  &&

	# BLFS: Link as cups-filters configure expects; but the desktop app is mupdf-gl.
	ln -sfv mupdf-x11 /usr/bin/mupdf &&

	# copy over our stuff
	cp -a ../pkg/usr/* /usr &&
	
	pkg_build_slackdesc
}
