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

inherit eutils flag-o-matic autotools

MY_PV=${PV/_/-}
MY_P=${P/_/-}

DESCRIPTION="molecular modeling and visualization application"
HOMEPAGE="http://www.ball-project.org/"
SRC_URI="http://www.ball-project.org/Downloads/V${MY_PV}/${MY_P}.tar.gz"
RESTRICT="mirror"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~amd64 x86"
IUSE="debug doc fftw python gsl asio"

RDEPEND="virtual/glut
	>=x11-libs/qt-sql-4.1
	>=x11-libs/qt-opengl-4.1
	>=x11-libs/qt-test-4.1
	fftw? ( >=sci-libs/fftw-3.0.1 )
	python? ( >=dev-python/sip-4.4.5 )
	gsl? ( sci-libs/gsl )"
DEPEND="sys-devel/bison
	sys-devel/flex
	doc? ( dev-texlive/texlive-latexextra )
	asio? ( dev-cpp/asio )
	${RDEPEND}"

S="${WORKDIR}/${MY_P}/source"

src_compile() {
	append-ldflags -L${S}

	touch config.lic
	eautoconf

	econf \
		--with-ball-path=/usr/share/BALL \
		$(use_enable debug debuginfo) \
		$(use_enable !debug optimization) \
		$(use_enable python) \
		$(use_enable fftw) \
		$(use_enable gsl) \
		--disable-fftw-longdbl \
		--with-qt=/usr \
		--with-qt-incl=/usr/include/qt4 \
		--with-qt-libs=/usr/lib/qt4 \
		|| die "econf failed"

	make -j1 depend
	emake lib || die "make lib failed"
	rm -f libBALL.so libVIEW.so
	make -j1 libBALL.so || die "make libBALL failed"
	make -j1 libVIEW.so || die "make libVIEW failed"
	emake BALLView || die "make BALLView failed"

	if use python ; then
		emake -C PYTHON/EXTENSIONS
	fi

	# fix executable bits
	chmod -x ../data/QSAR/*
	chmod -x ../data/CHARMM/charmm*

	if use doc ; then
		make -C ../doc/TUTORIAL tutorial.pdf \
			|| eerror "make doc failed"
	fi
}

src_install() {
	insinto /usr/lib/BALL

	doins libBALL.so libVIEW.so || die "doins libs failed"
	doins APPLICATIONS/BALLVIEW/BALLView || die "doins BALLView failed"

	if use python ; then
		doins PYTHON/EXTENSIONS/BALLmodule.so \
			|| eerror "doins python module failed"
		insinto /usr/share/BALL
		doins -r EXAMPLES/PYTHON \
			|| eerror "doins python examples failed"
	fi

	fperms +x /usr/lib/BALL/*

	dodoc COPYRIGHT ChangeLog

	cd ../debian-upstream
	dobin BALLView || eerror "dobin failed"
	doman ballview.1 || eerror "doman failed"
	domenu ballview.desktop

	cd ..

	insinto /usr/share/BALL
	doins -r data || eerror "doins data failed"

	insinto /usr/share/BALL/doc
	doins -r doc/BALLView || eerror "doins doc/BALLView failed"
	doins -r doc/internal || eerror "doins doc/internal failed"

	find include -name '*.doc' -print0 | xargs -0 rm -f
	insinto /usr/include
	doins -r include/BALL

	if use doc ; then
		insinto /usr/share/doc/${PF}
		doins doc/TUTORIAL/tutorial.pdf || eerror "doins pdf failed"
	fi
}
