SUMMARY="A library to work with the PDF file format"
DESCRIPTION="PoDoFo is a library to work with the PDF file format and \
includes also a few tools. The name comes from the first two letters of \
PDF (Portable Document Format."
HOMEPAGE="https://sourceforge.net/projects/podofo"
COPYRIGHT="2005-2014 Dominik Seichter
	2008 Craig Ringer"
LICENSE="GNU GPL v2
	GNU LGPL v2.1"
REVISION="1"
SOURCE_URI="https://sourceforge.net/projects/podofo/files/podofo/$portVersion/podofo-$portVersion.tar.gz"
CHECKSUM_SHA256="7cf2e716daaef89647c54ffcd08940492fd40c385ef040ce7529396bfadc1eb8"
PATCHES="podofo-$portVersion.patchset"

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

libVersion="$portVersion"
libVersionCompat="$libVersion compat >= ${libVersion%%.*}"

if [ $targetArchitecture != x86_gcc2 ]; then
	commandSuffix=
	commandBinDir=$prefix/bin
fi

PROVIDES="
	podofo$secondaryArchSuffix = $portVersion
	lib:libpodofo$secondaryArchSuffix = $libVersionCompat
	cmd:podofobox$commandSuffix
	cmd:podofocolor$commandSuffix
	cmd:podofocountpages$commandSuffix
	cmd:podofocrop$commandSuffix
	cmd:podofoencrypt$commandSuffix
	cmd:podofogc$commandSuffix
	cmd:podofoimg2pdf$commandSuffix
	cmd:podofoimgextract$commandSuffix
	cmd:podofoimpose$commandSuffix
	cmd:podofoincrementalupdates$commandSuffix
	cmd:podofomerge$commandSuffix
	cmd:podofopages$commandSuffix
	cmd:podofopdfinfo$commandSuffix
	cmd:podofosign$commandSuffix
	cmd:podofotxt2pdf$commandSuffix
	cmd:podofotxtextract$commandSuffix
	cmd:podofouncompress$commandSuffix
	cmd:podofoxmp$commandSuffix
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libfontconfig$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libjpeg$secondaryArchSuffix
	lib:libpng16$secondaryArchSuffix
	lib:libssl$secondaryArchSuffix
	lib:libtiff$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_devel="
	podofo${secondaryArchSuffix}_devel = $portVersion
	devel:libpodofo$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES_devel="
	podofo$secondaryArchSuffix == $portVersion base
	devel:libssl$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libcppunit$secondaryArchSuffix
	devel:libfontconfig$secondaryArchSuffix
	devel:libfreetype$secondaryArchSuffix
	devel:libjpeg$secondaryArchSuffix
	devel:libpng16$secondaryArchSuffix
	devel:libssl$secondaryArchSuffix
	devel:libtiff$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:cmake
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:make
	cmd:pkg_config$secondaryArchSuffix
	cmd:sed
	"

defineDebugInfoPackage podofo$secondaryArchSuffix \
	$libDir/libpodofo.so.$libVersion

PATCH()
{
	headersDir=
	if [ -n "$secondaryArchSuffix" ]; then
		headersDir=/$effectiveTargetArchitecture
	fi
	freetypeDir=$portPackageLinksDir/devel~libfreetype$secondaryArchSuffix/develop/headers$headersDir/freetype2
	sed -i -e s:/usr/include/freetype2:$freetypeDir: cmake/modules/FindFREETYPE.cmake

	# Taken from Gentoo
	# Test name: EncodingTest::testDifferencesEncoding
	# equality assertion failed
	# - Expected: 1
	# - Actual  : 0
	sed -e 's:CPPUNIT_TEST( testDifferencesEncoding ://\0:' \
		-i test/unit/EncodingTest.h
	sed -e '/TokenizerTest/ s/^#*/#/' -i test/CMakeLists.txt
}

BUILD()
{
	cmake -Bbuild -S. -DCMAKE_INSTALL_PREFIX:PATH=$prefix \
		-DPODOFO_BUILD_SHARED=1 \
		-DPODOFO_HAVE_CPPUNIT=1 \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo
	make -C build $jobArgs
}

INSTALL()
{
	make -C build install

	mkdir -p $(dirname $includeDir) $manDir
	mv $prefix/include $includeDir
	mv $prefix/share/man/man1 $manDir
	rmdir $prefix/share/man $prefix/share

	if [ -n "$secondaryArchSuffix" ]; then
		mv $prefix/lib $prefix/lib2
		mkdir -p $libDir
		mv $prefix/lib2/* $libDir
		rmdir $prefix/lib2
	fi

	prepareInstalledDevelLibs libpodofo
	fixPkgconfig

	# devel package
	packageEntries devel \
		$developDir
}

TEST()
{
	cd build/test/unit
	./podofo-test --selftest
}
