SUMMARY="The XML C parser and toolkit of Gnome"
DESCRIPTION="Libxml2 is the XML C parser and toolkit developed for the Gnome \
project, but is usable outside of the Gnome platform as well.
XML itself is a metalanguage to design markup languages, i.e. text language \
where semantic and structure are added to the content using extra \"markup\" \
information enclosed between angle brackets. HTML is the most well-known \
markup language.
Though the library is written in C a variety of language bindings make it \
available in other environments."
HOMEPAGE="http://www.xmlsoft.org/"
COPYRIGHT="1998-2013 Daniel Veillard.  All Rights Reserved."
LICENSE="MIT"
REVISION="1"
SOURCE_URI="https://download.gnome.org/sources/libxml2/2.9/libxml2-$portVersion.tar.xz"
CHECKSUM_SHA256="276130602d12fe484ecc03447ee5e759d0465558fbc9d6bd144e3745306ebf0e"
PATCHES="libxml2-$portVersion.patchset"

ARCHITECTURES="all"
SECONDARY_ARCHITECTURES="?x86_gcc2 x86"


# build the python module only for the primary architecture
pythonModuleEnabled=false
if [ -z "$secondaryArchSuffix" ]; then
	pythonModuleEnabled=true
fi

PROVIDES="
	libxml2$secondaryArchSuffix = $portVersion compat >= 2
	lib:libxml2$secondaryArchSuffix = $portVersion compat >= 2
	"
if [ -z "$secondaryArchSuffix" ]; then
	PROVIDES="$PROVIDES
		cmd:xmlcatalog = $portVersion compat >= 2
		cmd:xmllint = $portVersion compat >= 2
		"
fi
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libicuuc$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_devel="
	libxml2${secondaryArchSuffix}_devel = $portVersion
	cmd:xml2_config$secondaryArchSuffix = $portVersion compat >= 2
	devel:libxml2$secondaryArchSuffix = $portVersion compat >= 2
	"
REQUIRES_devel="
	libxml2$secondaryArchSuffix == $portVersion base
	devel:libicuuc$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"

if $pythonModuleEnabled; then
	SUMMARY_python="The python module for libxml2"
	PROVIDES_python="
		libxml2_python = $portVersion
		"
	REQUIRES_python="
		libxml2 == $portVersion base
		haiku$secondaryArchSuffix
		lib:libz$secondaryArchSuffix
		cmd:python
		"
fi

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libicuuc$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
if $pythonModuleEnabled; then
	BUILD_REQUIRES="$BUILD_REQUIRES
		python >= 2.7
		"
		# Note: We don't use "cmd:python" here to avoid issues with the
		# python search path.
fi
BUILD_PREREQUIRES="
	lib:libicudata$secondaryArchSuffix
	cmd:aclocal
	cmd:autoconf
	cmd:automake
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:libtoolize$secondaryArchSuffix
	cmd:make
	cmd:sed
	cmd:pkg_config$secondaryArchSuffix
	"

PATCH()
{
	# replace hard-coded references to the development documentation dir
	sed -i "s,^DEVHELP_DIR=\(.*\),DEVHELP_DIR=${developDocDir}," \
		doc/devhelp/Makefile.am
	sed -i "s,%{_datadir}/gtk-doc/html/libxml2,${developDocDir},g" \
		libxml.spec.in

	# treat __GNUC__ as true only if >= 4 in some files
	sed -i "s/def __GNUC__/ defined\\(__GNUC__\\) \\&\\& __GNUC__ > 3/g" \
		timsort.h
}

BUILD()
{
	libtoolize --force --copy --install
	aclocal
	autoconf
	automake --add-missing

	withPython=
	if $pythonModuleEnabled; then
		# We symlink python's directories into our packaging directory and tell
		# configure that it can find python there. This way the correct
		# installation directory for the python module is determined.
		pythonDir="$portPackageLinksDir/python"
		ln -s $pythonDir/bin $pythonDir/develop $pythonDir/lib $prefix
		withPython="--with-python=$prefix"
	fi

	runConfigure ./configure LDFLAGS="-lnetwork"  \
		--with-html-dir=$docDir/html \
		--with-html-subdir="" \
		--enable-icu \
		$withPython
	make $jobArgs
}

INSTALL()
{
	if $pythonModuleEnabled; then
		# Unfortunately we can't just remove the in symlinks we created in
		# BUILD() and install normally, since then make detects that the
		# Python.h is missing and tries to rebuild the module. So we leave
		# things as is and use DESTDIR to install the everything in a subdir.
		# Then we remove the symlinks and move everything where it belongs.
		make DESTDIR=$prefix install
		rm -f $prefix/bin $prefix/develop $prefix/lib
		mv $prefix/$prefix/* $prefix
		rm -r $prefix/packages
	else
		make install
	fi

	rm $libDir/libxml2.la
	# prepare develop/lib
	prepareInstalledDevelLibs libxml2
	fixPkgconfig
	mv $libDir/xml2Conf.sh $developLibDir/
	fixDevelopLibDirReferences $developLibDir/xml2Conf.sh $binDir/xml2-config

	# python package
	if $pythonModuleEnabled; then
		packageEntries python \
			$libDir/python* \
			${docDir}_python*
	fi

	# devel package
	packageEntries devel \
		$binDir/xml2-config \
		$dataDir \
		$developDir \
		$docDir \
		$manDir/man1/xml2-config.1 \
		$manDir/man3 \
		$libDir/cmake

	# Remove stuff we don't need in the secondary architecture base package.
	if [ -n "$secondaryArchSuffix" ]; then
		rm -rf $binDir
		rm -rf $documentationDir
	else
		# left-over since all contents has been moved to other packages
		rmdir $(dirname $docDir)
	fi
}

TEST()
{
	make tests
}
