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

EAPI=8

inherit cmake-multilib

DESCRIPTION="OpenVR over OpenXR compatibility layer"
HOMEPAGE="https://gitlab.com/znixian/OpenOVR"

if [[ ${PV} == 9999 ]]; then
	inherit git-r3
	EGIT_REPO_URI="https://gitlab.com/znixian/OpenOVR.git"
	EGIT_BRANCH="openxr"
else
	KEYWORDS="~amd64"
	OPENXR_COMMIT=91a8a8d9d70f4b469bca0726122c3b5a6096010e
	SRC_URI="
		https://gitlab.com/znixian/OpenOVR/-/archive/${PV}/OpenOVR-${PV}.tar.bz2 -> ${P}.tar.bz2
		https://github.com/KhronosGroup/OpenXR-SDK/archive/${OPENXR_COMMIT}.tar.gz -> OpenXR-SDK-${OPENXR_COMMIT}.tar.gz
	"
	S="${WORKDIR}/OpenOVR-${PV}"
fi

LICENSE="GPL-3 Apache-2.0 MIT"
SLOT="0"

DEPEND="
	dev-lang/python
	dev-util/vulkan-headers
	media-libs/glm
	media-libs/libglvnd[${MULTILIB_USEDEP},X]
	media-libs/openxr-loader[${MULTILIB_USEDEP}]
	media-libs/vulkan-loader[${MULTILIB_USEDEP}]
	virtual/glu[${MULTILIB_USEDEP}]
	x11-libs/libX11[${MULTILIB_USEDEP}]
"
RDEPEND="${DEPEND}"

if [[ ${PV} != 9999 ]] ; then
src_unpack()
{
	default_src_unpack
	mv --no-target-directory "${WORKDIR}/OpenXR-SDK-${OPENXR_COMMIT}" "${S}/libs/openxr-sdk"
}
fi

multilib_src_configure()
{
	# Installing to /usr would not work with Steam bacause pressure vessel
	# sandbox mounts /usr as /run/host/usr and openvr path would point to a
	# non-existing location.
	#
	# Pressure vessel would need to be fixed and patch the location
	# as it does for vulkan and other similar configuration files.
	local mycmakeargs=(
		"-DCMAKE_INSTALL_PREFIX=/opt"
		# Required for Proton: https://gitlab.com/znixian/OpenOVR/-/issues/416
		"-DUSE_SYSTEM_OPENXR=OFF"
		"-DUSE_SYSTEM_GLM=ON"
	)
	cmake_src_configure
}

multilib_src_install()
{
	cmake_src_install

	insinto /opt/OpenComposite
	doins "${FILESDIR}/openvrpaths.vrpath"
}

pkg_postinst()
{
	elog "For applications to use OpenComposite, symlink"
	elog "~/.config/openvr/openvrpaths.vrpath to /opt/OpenComposite/openvrpaths.vrpath."
}
