# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

MY_P=${P/.08/-august}

DESCRIPTION="An XML parser toolkit written in C++"
HOMEPAGE="http://www.jezuk.co.uk/cgi-bin/view/arabica"
SRC_URI="mirror://sourceforge/${MY_P}.tar.bz2"
LICENSE="as-is"
SLOT="0"
KEYWORDS="~x86"
IUSE="ucs2 expat xerces-c xml2"
S=${WORKDIR}/${MY_P}

DEPEND="expat? ( >=dev-libs/expat-1.95.8 )
	xerces-c? ( >=dev-libs/xerces-c-2.7.0 )
	xml2? ( >=dev-libs/libxml2-2.6.20-r2 )"

src_compile() {

	PARSER_CONFIG=""
	DYNAMIC_LIBS="-lstdc++"
	INCS_DIRS=""

	useq expat \
		&& PARSER_CONFIG="${PARSER_CONFIG} -DUSE_EXPAT" \
		&& DYNAMIC_LIBS="-lexpat ${DYNAMIC_LIBS}"
	useq xerces-c \
		&& PARSER_CONFIG="${PARSER_CONFIG} -DUSE_XERCES" \
		&& DYNAMIC_LIBS="-lxerces-c ${DYNAMIC_LIBS}"
	useq xml2 \
		&& PARSER_CONFIG="${PARSER_CONFIG} -DUSE_LIBXML2" \
		&& DYNAMIC_LIBS="-lxml2 ${DYNAMIC_LIBS}" \
		&& INCS_DIRS="${INCS_DIRS} -I\/usr\/include\/libxml2"

	if [ -z "${PARSER_CONFIG}" ]; then
		eerror "at least one of expat, xerces-c or xml2 must be in USE!"
		die
	fi

	useq ucs2 || PARSER_CONFIG="${PARSER_CONFIG} -DARABICA_NO_WCHAR_T"

	# replace PARSER_CONFIG and libs.
	# append CXXFLAGS and extra include dirs.
	sed	-e "s/\r//g" \
		-e "s/^PARSER_CONFIG =.*/PARSER_CONFIG = ${PARSER_CONFIG}/" \
		-e "s/^DYNAMIC_LIBS =.*/DYNAMIC_LIBS = ${DYNAMIC_LIBS}/" \
		-e "s/^INCS_DIRS =.*/\0 ${INCS_DIRS}/" \
		-e "s/^CXXFLAGS =.*/\0 -fPIC ${CXXFLAGS}/" \
		<Makefile.header.linux >Makefile.header

	# don't build tests and examples
	cd SAX

	emake || die "emake failed"

}

src_install() {

	dolib.so bin/libArabica.so

	HEADERS=$(find XML SAX DOM XPath \( -name "*.h" -o -name "*.hpp" \) -printf "%p ")
	for i in ${HEADERS}; do
		insinto /usr/include/${i%\/*}
		doins ${i}
	done

}