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

EAPI=8

inherit cmake

DESCRIPTION="Android x86-64 Roblox compatibility runtime for Linux"
HOMEPAGE="https://github.com/komaruworld/mocktail"
SRC_URI="https://github.com/komaruworld/mocktail/archive/refs/tags/${PV}.tar.gz -> ${P}.tar.gz"

LICENSE="Apache-2.0"
SLOT="0"
KEYWORDS="~amd64"
IUSE="freebsd-helper"

# Source-verification notes (see config/upstream_dependencies.json and .gitmodules
# in the release archive):
#
# * third_party/libjnivm and third_party/Vulkan-Headers are declared as git
#   submodules, but the release archive ships them as EMPTY directories.  The
#   production build does not need libjnivm: Mocktail ships its own in-tree
#   legacy JNI layer (src/jnivm/jnivm.cc) and only the test suite references
#   the upstream copy, so it is disabled with
#   -DMOCKTAIL_ENABLE_UPSTREAM_JNIVM=OFF.  No libjnivm dependency is added.
#
# * Vulkan::Headers is required unconditionally.  It is supplied from
#   dev-util/vulkan-headers (which installs VulkanHeadersConfig.cmake) by
#   replacing the empty add_subdirectory() with a find_package() call via the
#   patch in files/.
#
# * Tests are disabled for package builds (-DBUILD_TESTING=OFF) because
#   upstream obtains GoogleTest via CMake FetchContent
#   (https://github.com/google/googletest.git) at configure time -- an
#   untracked network download that is forbidden in src_configure and is not
#   permitted for a Gentoo package build.  No test USE flag is added.
#
# * sys-devel/lld is NOT a dependency.  Upstream only requires LLD (and
#   elfedit from sys-devel/binutils) for the native FreeBSD Linuxulator socket
#   helper, which is opt-in (freebsd-helper USE flag) and disabled by default
#   on Linux.

S="${WORKDIR}/${P}"

PATCHES=(
	"${FILESDIR}/${PN}-${PV}-use-system-vulkan-headers.patch"
)

# [vulkan] on libsdl3: Mocktail's default direct-vulkan backend and the
# first-run payload graphics canary both need SDL3 built with -DSDL_VULKAN;
# the flag also pulls media-libs/vulkan-loader (dlopened by SDL).  Upstream
# assumes always-on Vulkan support as on Arch's SDL3.
# [vulkan] on mesa: a Vulkan ICD is a hard runtime requirement — the canary
# runs direct-vulkan and rejects the payload without a working driver (ANV/
# RADV/nvk for the selected video_cards_*).
# virtual/opengl: the runtime window binds EGL/GLES from the system (libglvnd).
RDEPEND="
	>=media-libs/libsdl3-3.4[vulkan]
	media-libs/sdl3-ttf
	dev-libs/glib:2
	gui-libs/gtk:4
	>=gui-libs/libadwaita-1.6:1
	net-libs/webkit-gtk:6/0
	media-libs/libplacebo
	dev-libs/elfutils:0
	dev-libs/libyaml
	virtual/minizip
	>=dev-libs/capstone-5:0/5
	dev-libs/libutf8proc
	net-misc/curl
	dev-libs/openssl:0/3
	media-libs/fontconfig
	sys-libs/zlib:0/1
	media-libs/mesa[vulkan]
	virtual/opengl"
DEPEND="${RDEPEND}
	dev-cpp/nlohmann_json
	dev-util/vulkan-headers"
# dev-build/cmake is provided automatically by cmake.eclass.
BDEPEND="
	virtual/pkgconfig"

src_configure() {
	# NOTE: -DCMAKE_BUILD_TYPE is deliberately not passed.  cmake.eclass
	# appends its own -DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}" after
	# mycmakeargs (so an ebuild-level value would be overridden), the eclass
	# default (RelWithDebInfo) is in effect, and Mocktail does not branch on
	# CMAKE_BUILD_TYPE, so the per-config optimization flags (wiped by the
	# eclass in favour of the user's CFLAGS) are all that would change.
	local mycmakeargs=(
		-DBUILD_TESTING=OFF
		-DMOCKTAIL_ENABLE_UPSTREAM_JNIVM=OFF
		-DMOCKTAIL_BUILD_FREEBSD_SOCKET_HELPER=$(usex freebsd-helper ON OFF)

		# Upstream hardcodes "lib" (not CMAKE_INSTALL_LIBDIR) when locating its
		# private runtime directory from the installed binaries:
		#   * UpdateHelper            (src/runtime/payload_update_preflight.cc)
		#   * InstalledMetadataEntry  (src/update/main.cc)
		#   * supported launch policy (src/runtime/supported_launch_policy.cc)
		# Without this the first-run payload preflight cannot find
		# mocktail_updater under Gentoo's $(get_libdir) (lib64 on amd64), so no
		# Roblox payload is ever downloaded and startup dies on a missing
		# rbx_bin/libroblox.so.  The stub libraries and helpers installed here
		# are private to ${EPREFIX}/usr/lib/mocktail (never scanned by
		# ldconfig), and upstream's own packaging/arch/PKGBUILD passes the same
		# -DCMAKE_INSTALL_LIBDIR=lib, so this matches the tested layout.
		-DCMAKE_INSTALL_LIBDIR=lib

		# Point the compile-time fallback manifests at the installed metadata
		# instead of ${CMAKE_SOURCE_DIR}/config/... (the ephemeral Portage
		# workdir).  Upstream installs these files to
		# ${CMAKE_INSTALL_DATADIR}/mocktail/metadata, and every official
		# packager (Arch, AUR, Flatpak, Nix) overrides both defines the same
		# way.
		-DMOCKTAIL_DEFAULT_COMPATIBILITY_MANIFEST="${EPREFIX}/usr/share/mocktail/metadata/roblox_compatibility.json"
		-DMOCKTAIL_DEFAULT_SIGNING_TRUST_MANIFEST="${EPREFIX}/usr/share/mocktail/metadata/roblox_signing_certificates.json"
	)

	cmake_src_configure
}

src_test() {
	# Tests are disabled at configure time (-DBUILD_TESTING=OFF) because
	# upstream fetches GoogleTest over the network via CMake FetchContent.
	# Nothing to run for a package build.
	:
}

src_install() {
	cmake_src_install
	dodoc README.md FAQ.md
}

pkg_postinst() {
	if command -v gtk-update-icon-cache >/dev/null 2>&1; then
		gtk-update-icon-cache -t -f /usr/share/icons/hicolor || die
	fi
	if command -v update-desktop-database >/dev/null 2>&1; then
		update-desktop-database /usr/share/applications || die
	fi
	if [[ ! -d "${EROOT}/usr/share/vulkan/icd.d" ]]; then
		ewarn "No Vulkan ICD found in ${EROOT}/usr/share/vulkan/icd.d."
		ewarn "Mocktail's default direct-vulkan backend (used by the first-run"
		ewarn "payload verification) needs a Vulkan driver for your GPU, e.g."
		ewarn "media-libs/mesa with USE=vulkan and the matching video_cards_*,"
		ewarn "or the graphics canary will reject the Roblox payload."
	fi
}

pkg_postrm() {
	if command -v gtk-update-icon-cache >/dev/null 2>&1; then
		gtk-update-icon-cache -t -f /usr/share/icons/hicolor || die
	fi
	if command -v update-desktop-database >/dev/null 2>&1; then
		update-desktop-database /usr/share/applications || die
	fi
}