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-2016 Artifex Software, Inc"
LICENSE="AGPL-3.0"
REVISION="2"
SOURCE_URI="http://mupdf.com/downloads/archive/mupdf-${portVersion/\~/}-source.tar.gz"
CHECKSUM_SHA256="aacc1f36b9180f562022ef1ab3439b009369d944364f3cff8a2a898834e3a836"
SOURCE_DIR="mupdf-${portVersion/\~/}-source"
PATCHES="
	mupdf-$portVersion.patchset
	mupdf-$portVersion-openjpeg21.patchset
	mupdf-$portVersion-harfbuzz.patchset
	"

ARCHITECTURES="!all"
SECONDARY_ARCHITECTURES="!x86"

commandSuffix=$secondaryArchSuffix
commandBinDir=$binDir
if [ "$targetArchitecture" = x86_gcc2 ]; then
	commandSuffix=
	commandBinDir=$prefix/bin
fi

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

PROVIDES_devel="
	mupdf${secondaryArchSuffix}_devel = $portVersion
	devel:libmupdf$secondaryArchSuffix
	devel:libmupdfthird$secondaryArchSuffix
	"
REQUIRES_devel="
	mupdf$secondaryArchSuffix == $portVersion base
	"

BUILD_REQUIRES="
	devel:libfreetype$secondaryArchSuffix
	devel:libglib_2.0$secondaryArchSuffix
	devel:libharfbuzz$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 \
		libmupdfthird
	fixPkgconfig

	# devel package
	packageEntries devel \
		$developDir
}
