SUMMARY="A lightweight XPS and PDF rendering library"
DESCRIPTION="The renderer in MuPDF is tailored for high quality anti-aliased \
graphics. It renders text with metrics and spacing accurate to within \
fractions of a pixel for the highest fidelity in reproducing the look of a \
printed page on screen.

MuPDF is also small, fast, and yet complete. It supports PDF 1.7 with \
transparency, encryption, hyperlinks, annotations, searching and more. It also \
reads XPS/OpenXPS documents and CBZ (Comic Book archive) files."
HOMEPAGE="https://www.mupdf.com/"
COPYRIGHT="2006-2014 Artifex Software, Inc"
LICENSE="AGPL-3.0"
REVISION="6"
SOURCE_URI="http://www.mupdf.com/downloads/archive/mupdf-$portVersion-source.tar.gz"
CHECKSUM_SHA256="9813ee330950a641364181649ebd369c947adbcea9d41ed953ec5365640ea563"
SOURCE_DIR="mupdf-$portVersion-source"
PATCHES="
	mupdf-1.6.patchset
	mupdf-1.6-openjpeg21-1.patch
	"

ARCHITECTURES="all"
SECONDARY_ARCHITECTURES="x86"

PROVIDES="
	mupdf$secondaryArchSuffix = $portVersion compat >= 1.1
	cmd:mudraw$secondaryArchSuffix
	cmd:mujstest$secondaryArchSuffix
	cmd:mutool$secondaryArchSuffix
	lib:libmupdf$secondaryArchSuffix = $portVersion compat >= 1.1
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libjbig2dec$secondaryArchSuffix
	lib:libjpeg$secondaryArchSuffix
	lib:libopenjp2$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_devel="
	mupdf${secondaryArchSuffix}_devel = $portVersion
	devel:libmupdf$secondaryArchSuffix = $portVersion compat >= 1
	"
REQUIRES_devel="
	mupdf$secondaryArchSuffix == $portVersion
	"

BUILD_REQUIRES="
	devel:libfreetype$secondaryArchSuffix
	devel:libjbig2dec$secondaryArchSuffix
	devel:libjpeg$secondaryArchSuffix
	devel:libopenjp2$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	haiku${secondaryArchSuffix}_devel
	cmd:find
	cmd:freetype_config$secondaryArchSuffix
	cmd:gcc$secondaryArchSuffix
	cmd:make
	cmd:pkg_config$secondaryArchSuffix
	"

BUILD()
{
	# don't use builtin libs
	rm -rf thirdparty

	# static lib
	my_soname=libmupdf.so.$portVersion
	my_soname_js_none=libmupdf-js-none.so.$portVersion
	sed -e "\$a\$(MUPDF_LIB): \$(MUPDF_JS_NONE_LIB)" \
		-e "\$a\\\t\$(QUIET_LINK) \$(CC) \$(LDFLAGS) --shared -Wl,-soname -Wl,${my_soname} -Wl,--no-undefined -o \$@ \$^ \$(MUPDF_JS_NONE_LIB) \$(LIBS)" \
		-e "/^MUPDF_LIB :=/s:=.*:= \$(OUT)/${my_soname}:" \
		-e "\$a\$(MUPDF_JS_NONE_LIB):" \
		-e "\$a\\\t\$(QUIET_LINK) \$(CC) \$(LDFLAGS) --shared -Wl,-soname -Wl,${my_soname_js_none} -Wl,--no-undefined -o \$@ \$^ \$(LIBS)" \
		-e "/install/s: COPYING : :" \
		-i Makefile || die
	make XCFLAGS=-fpic build=release $jobArgs
}

INSTALL()
{
	make build=release install \
		prefix=$prefix \
		bindir=$binDir \
		docdir=$docDir \
		libdir=$libDir \
		incdir=$includeDir \
		mandir=$manDir

	ln -s libmupdf.so.$portVersion $libDir/libmupdf.so

	prepareInstalledDevelLibs libmupdf

	# devel package
	packageEntries devel \
		$developDir
}
