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

inherit eutils autotools python

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

DESCRIPTION="molecular modeling and visualization application"
HOMEPAGE="http://www.ball-project.org/"
SRC_URI="http://www.ball-project.org/Downloads/V${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}/source"

src_compile() {
	ln -s source ../lib

	touch config.lic

	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"

	emake depend dirs collect
	make -j1 libBALL.so || die "make libBALL failed"
	make -j1 libVIEW.so || die "make libVIEW failed"
	make -j1 BALLView || die "make BALLView failed"

	if use python ; then
		emake -C PYTHON/EXTENSIONS || die "make python failed"
	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

	find ../include -name '*.doc' -print0 | xargs -0 rm -f
}

src_install() {
	dodir /usr/lib
	make DESTDIR=${D} install || die

	dobin APPLICATIONS/BALLVIEW/BALLView || die
	make_desktop_entry BALLView BALLView \
		/usr/share/BALL/data/graphics/ballview_32x32.xpm

	if use python ; then
		insinto $(python_get_sitedir)
		doins PYTHON/EXTENSIONS/*/*.so \
			PYTHON/EXTENSIONS/BALL.py \
			|| eerror "doins python module failed"
		insinto /usr/share/BALL
		doins -r EXAMPLES/PYTHON \
			|| eerror "doins python examples failed"
	fi

	dodoc COPYRIGHT ChangeLog

	insinto /usr/share/BALL/doc
	doins -r ../doc/BALLView || eerror "doins doc/BALLView failed"
	doins -r ../doc/internal || eerror "doins doc/internal failed"
	if use doc ; then
		doins ../doc/TUTORIAL/tutorial.pdf || eerror "doins pdf failed"
	fi
}
