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

EAPI=8

inherit cmake fortran-2

DESCRIPTION="Quantum chemistry and solid state physics software package"
HOMEPAGE="https://www.cp2k.org"
SRC_URI="https://github.com/${PN}/${PN}/releases/download/v${PV}/${P}.tar.bz2"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~amd64"
IUSE="fftw mpi plumed"

# PLUMED support requires MPI on the CP2K side (CP2K_USE_PLUMED is
# gated by CP2K_USE_MPI in the build system).
REQUIRED_USE="plumed? ( mpi )"

# fypp preprocesses the Fortran sources during the build
BDEPEND="dev-python/fypp"
DEPEND="
	virtual/blas
	virtual/lapack
	fftw? ( sci-libs/fftw:3.0= )
	mpi? (
		>=sci-libs/dbcsr-2.8.0[mpi]
		virtual/mpi
		plumed? ( sci-chemistry/plumed[mpi] )
	)
	!mpi? ( >=sci-libs/dbcsr-2.8.0 )
"
RDEPEND="${DEPEND}"

# cp2k normally suffixes its binaries (cp2k.ssmp, cp2k.psmp, ...);
# drop the suffix so the main binary is simply 'cp2k'.
PATCHES=( "${FILESDIR}/${P}-remove-ext.patch" )

src_configure() {
	local mycmakeargs=(
		-DCP2K_USE_FFTW3=$(usex fftw)
		-DCP2K_USE_MPI=$(usex mpi)
		-DCP2K_USE_PLUMED=$(usex plumed)
	)
	cmake_src_configure
}

src_install() {
	cmake_src_install

	# remove unittests
	rm -f "${ED}"/usr/bin/*_unittest || die
}