# Copyright 1999-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

# Amber 26's Fortran module (dyndep) dependencies are not ordered correctly
# by the Ninja generator (amber_rism_interface.F90 misses rism3d_c.mod);
# upstream builds and docs assume Unix Makefiles.
CMAKE_MAKEFILE_GENERATOR=emake

inherit cmake fortran-2

DESCRIPTION="Collection of programs for molecular dynamics simulations of biomolecules"
HOMEPAGE="https://ambermd.org/AmberTools.php"
# Upstream only hands out the tarball after accepting the license form at
# https://ambermd.org/GetAmber.php , hence RESTRICT="fetch".  The archive
# unpacks to ambertools${PV}_src/ and already carries the int64 RISM
# treecode fix (rism3d_tree_cp.F90).
SRC_URI="https://ambermd.org/downloads/AmberTools/AmberTools${PV}.tar.bz2 -> ambertools${PV}.tar.bz2"

S="${WORKDIR}/ambertools${PV}_src"

LICENSE="GPL-3 LGPL-3 BSD MIT"
SLOT="0"
KEYWORDS="~amd64"
IUSE="mpi"

RESTRICT="fetch"

# PLUMED is a hard dependency of sander in AmberTools 26 (the C API
# changed in 2.5); its MPI flavour must match the one used here.
# dev-libs/boost must provide zlib/bzip2-enabled iostreams, which the
# Amber build system verifies at configure time.
RDEPEND="
	app-arch/bzip2
	dev-libs/boost[zlib,bzip2]
	sci-chemistry/plumed[mpi?]
	sci-libs/arpack
	sci-libs/fftw:3.0=
	sci-libs/netcdf[hdf5]
	sci-libs/netcdf-fortran
	sci-libs/nlopt
	sys-libs/readline:0
	virtual/zlib
	mpi? ( virtual/mpi )
	virtual/blas
	virtual/lapack
"
DEPEND="${RDEPEND}"

PATCHES=(
	"${FILESDIR}/${P}-fhs.patch"
	# Boost.System is header-only since Boost 1.69 and Gentoo's dev-libs/boost
	# no longer ships a compiled libboost_system, but Amber still requests it
	# as a component and links it into metatwist (moft).
	"${FILESDIR}/${P}-boost-system-header-only.patch"
	# Put the libraries into $(get_libdir): upstream forces CMAKE_INSTALL_LIBDIR
	# to "lib" and hardcodes its own LIBDIR="lib".
	"${FILESDIR}/${P}-multilib-libdir.patch"
	# teLeap wrote leap.log into the data search path (= $AMBERHOME/dat/leap/*)
	# when the cwd was not writable, which is fatal under read-only /usr;
	# create log files relative to the cwd only and warn instead of dying.
	"${FILESDIR}/${P}-leap-log-file.patch"
)

pkg_nofetch() {
	einfo "Please download AmberTools ${PV} from"
	einfo "  https://ambermd.org/downloads/AmberTools/ (via https://ambermd.org/GetAmber.php)"
	einfo "after accepting the license terms, and place the archive into"
	einfo "  DISTDIR as ambertools${PV}.tar.bz2"
}

src_configure() {
	local mycmakeargs=(
		# Amber insists on an explicit compiler family
		-DCOMPILER=GNU
		# never run update_amber during configure (it fetches patches
		# from ambermd.org; the tarball must build as shipped)
		-DCHECK_UPDATES=FALSE
		-DAPPLY_UPDATES=FALSE
		-DMPI=$(usex mpi)
		-DCUDA=FALSE
		-DBUILD_QUICK=FALSE
		-DBUILD_GUI=FALSE
		-DBUILD_PYTHON=OFF
		-DDOWNLOAD_MINICONDA=FALSE
		# the bundled PerlMol installs its modules into /usr/lib/perl (not in
		# @INC, FHS violation) and nothing in our tool set needs it
		-DBUILD_PERL=FALSE
		-DBUILD_DEPRECATED=OFF
		-DFORCE_EXTERNAL_LIBS="boost;netcdf;netcdf-fortran;fftw;arpack;zlib;libbz2;blas;lapack"
		# Amber's FindKMMD.cmake runs a bare find_library(libkmmd.so): once a
		# previous AmberTools is merged, the stale /usr/lib64/libkmmd.so is
		# picked up as an "external" interface target which then clashes with
		# the bundled kmmd target (CMP0002) and links an outdated version.
		# kmmd is meant to be built from the bundled sources.
		-DCMAKE_DISABLE_FIND_PACKAGE_KMMD=TRUE
		# the FHS patch puts the tools directly into ${BINDIR} instead of
		# wrapping them into bin/wrapped_progs plus amber.sh sourcing
		-DINSTALL_WRAPPED=FALSE
	)
	cmake_src_configure
}

src_install() {
	cmake_src_install

	# The FHS patch moves the data tree to ${EPREFIX}/usr/share/ambertools/dat.
	# Drop the prefix-style leftovers that make no sense under /usr: shell
	# setup scripts, the autotools compatibility config.h (only used by the
	# python build), the copy of the cpptraj data fallback tree and the
	# test helper scripts.
	rm -rf "${ED}/usr/AmberTools" "${ED}/usr/test" || die
	rm "${ED}"/usr/{amber.sh,amber.csh,amber-interactive.sh,config.h} || die
	# dangling symlink to the (not built) bundled miniconda python
	rm -f "${ED}/usr/bin/amber.python" || die

	# the build installs unversioned static archives that nothing links against
	find "${ED}" -name '*.a' -delete || die
	find "${ED}" -name '*.la' -delete || die

	# collect license files (LICENSE and GNU_LGPL_v3 land in the prefix root,
	# the bundled third-party notices in licenses/)
	dodir /usr/share/licenses/${PN}
	mv "${ED}/usr/licenses/"* "${ED}/usr/share/licenses/${PN}/" || die
	rmdir "${ED}/usr/licenses" || die
	mv "${ED}"/usr/{LICENSE,GNU_LGPL_v3} "${ED}/usr/share/licenses/${PN}/" || die

	# third-party scripts and tleap expect $AMBERHOME/bin to exist
	dosym -r /usr/bin /usr/share/ambertools/bin

	local envd="${T}/99${PN}"
	cat > "${envd}" <<- EOF || die
		AMBERHOME="${EPREFIX}/usr/share/ambertools"
	EOF
	doenvd "${envd}"
}

pkg_postinst() {
	einfo "AmberTools is installed in the standard ${EPREFIX}/usr hierarchy."
	einfo "AMBERHOME is set to ${EPREFIX}/usr/share/ambertools via env.d"
	einfo "(99${PN}); run 'env-update && source ${EPREFIX}/etc/profile'"
	einfo "(or re-login) to pick it up."
}