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

# 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_32 lcms2_32 libjpeg-turbo32 libopenjpeg2_32 libX11_32 freeglut32 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 &&
	cd tmp/mupdf* &&

	# 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* &&

	# 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
}
