# Copyright 2019-2022 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

PYTHON_COMPAT=( pypy3 python3_{10..11} )

inherit autotools python-any-r1

DESCRIPTION="Expose the main performance trends in applications computation structure"
HOMEPAGE="
	https://tools.bsc.es/cluster-analysis
	https://github.com/bsc-performance-tools/clustering-suite
"
SRC_URI="https://codeload.github.com/bsc-performance-tools/clustering-suite/tar.gz/refs/tags/${PV} -> ${P}.tar.gz"
S="${WORKDIR}/clustering-suite-${PV}"

LICENSE="LGPL-2.1+"
SLOT="0"
KEYWORDS="~amd64"
IUSE="doc mpi muster treedbscan"

PATCHES=(
	"${FILESDIR}/${P}-unbundle-libANN.patch"
	"${FILESDIR}/${P}-do-not-add-boost-thread-ldpath.patch"
	"${FILESDIR}/${P}-Build-fix-for-GCC-11-invocable-as-const.patch"
)
# attempt at unbundling libbsctools failed
# current libbsctools seems to be too old (missing headers)
#	"${FILESDIR}/${PN}-unbundle-libbsctools.patch"

RDEPEND="
	app-arch/bzip2
	dev-libs/boost:=
	sci-libs/ann
	!sys-cluster/libbsctools
	mpi? ( virtual/mpi )
	muster? ( sys-cluster/muster )
	treedbscan? (
		dev-libs/boost:=[threads(+)]
		dev-libs/gmp
		dev-libs/mpfr
		sci-mathematics/cgal
		sys-cluster/synapse
	)
"

DEPEND="
	${RDEPEND}
	${PYTHON_DEPS}
"
BDEPEND="doc? ( app-doc/doxygen )"

src_prepare() {
	use muster && PATCHES+=( "${FILESDIR}/${P}-force-muster-discovery.patch" )
	rm -r src/libANN || die
#	rm -r pcfparser_svn3942 || die
#	rm -r src/libParaverTraceParser || die
	default
	sed -e "s|iterate/lib|iterate/$(get_libdir)|g" -i config/gmp_mpfr.m4 || die
	sed -e "s|dir/lib|dir/$(get_libdir)|g" -i config/ax_muster.m4 || die

	#from bootstrap
	echo "#*********************************************************************" >> configure.ac || die
	echo "#'configure.ac.' automatically generated by 'autogen.sh' do not modify" >> configure.ac || die
	echo "#*********************************************************************" >> configure.ac || die
	echo "" >> configure.ac || die
	sed s/@@VERSION_NUMBER@@/${PV}/ < configure.ac.template >> configure.ac || die

	AT_M4DIR="config" eautoreconf
}

src_configure() {

	local myconf=(
		--disable-old-pcfparser
		--disable-static
		--disable-static-boost
		--enable-shared
		--with-boost="${EPREFIX}/usr"
		--with-pic
	)

	if use mpi; then
		myconf+=( "--with-mpi=${EPREFIX}/usr" )
	else
		myconf+=( "--without-mpi" )
	fi
	if use muster; then
		myconf+=( "--with-muster=${EPREFIX}/usr" )
	else
		myconf+=( "--without-muster" )
	fi
	if use treedbscan; then
		myconf+=( "--enable-treedbscan" )
		myconf+=( "--with-cgal=${EPREFIX}/usr" )
		myconf+=( "--with-gmp=${EPREFIX}/usr" )
		myconf+=( "--with-mpfr=${EPREFIX}/usr" )
		myconf+=( "--with-synapse=${EPREFIX}/usr" )
	else
		myconf+=( "--without-cgal" )
		myconf+=( "--without-gmp" )
		myconf+=( "--without-mpfr" )
		myconf+=( "--without-synapse" )
	fi

	econf "${myconf[@]}" || die
}

src_compile() {
	export VARTEXFONTS="${T}/fonts"
	if use doc ; then
		pushd doc || die
		emake build-documentation
		popd
	fi
	default
}

src_install() {
	MAKEOPTS="-j1" DESTDIR="${D}" emake install

	cd doc || die
	dodoc -r *.pdf

	rm "${ED}/usr/share/doc/clusteringsuite_manual.pdf" || die
	mv "${ED}/usr/share/example" "${ED}/usr/share/doc/${PF}/examples" || die
	docompress -x "/usr/share/doc/${PF}/examples"

	find "${ED}" -name '*.la' -delete || die
}
