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

# Autogenerated by pycargoebuild 0.16.0

EAPI=8

RUST_MIN_VER="1.88.0"
CRATES="
"
LUA_COMPAT=( lua5-1 luajit )

inherit cargo desktop lua-single toolchain-funcs xdg-utils

DESCRIPTION="Cross-platform runtime for Path of Building"
HOMEPAGE="https://github.com/meehl/rusty-path-of-building"
SRC_URI="
	https://github.com/meehl/${PN}/archive/refs/tags/v${PV}.tar.gz -> "${P}".tar.gz
	${CARGO_CRATE_URIS}
"
if [[ ${PKGBUMPING} != ${PVR} ]]; then
	SRC_URI+="
		https://github.com/fasys-crate-dist/${PN}/releases/download/v${PV}/${P}-crates.tar.xz
	"
fi

LICENSE="MIT"
# Dependent crate licenses
LICENSE+="
	Apache-2.0 BSD-2 BSD Boost-1.0 CC0-1.0 CDLA-Permissive-2.0 ISC MIT
	MPL-2.0 UoI-NCSA Unicode-3.0 ZLIB
"
SLOT="0"
KEYWORDS="~amd64"

REQUIRED_USE="${LUA_REQUIRED_USE}"
RESTRICT="test"

# lzip requires luajit to build
DEPEND="
	${LUA_DEPS}
	dev-lang/luajit:=
	virtual/zlib:=
"
RDEPEND="
	$(lua_gen_cond_dep '
		dev-lua/lua-curlv3[${LUA_USEDEP}]
		dev-lua/lua-utf8[${LUA_USEDEP}]
		dev-lua/luasocket[${LUA_USEDEP}]
	')
	virtual/zlib:=
"
BDEPEND="virtual/pkgconfig"

pkg_setup() {
	lua-single_pkg_setup
	rust_pkg_setup
}

src_prepare() {
	default

	sed 's/COMMONFLAGS\s*=.*/COMMONFLAGS = -fPIC $(OS_FLAGS) $(DBG)/' \
		-i lua/libs/lzip/Makefile || die
}

src_compile() {
	cargo_src_compile

	pushd lua/libs/lzip >/dev/null || die

	emake \
		CC="$(tc-getCXX)" \
		CFLAGS="${CXXFLAGS}" \
		LDFLAGS="${LDFLAGS}" \
		LUA_IMPL=luajit

	popd >/dev/null || die
}

src_install() {
	cargo_src_install

	pushd lua/libs/lzip >/dev/null || die

	emake \
		DESTDIR="${ED}" \
		LUA_IMPL=luajit \
		install

	popd >/dev/null || die

	newicon assets/icon.png ${PN}.png

	for v in 1 2; do
		make_desktop_entry --eapi9 \
			${PN} -a "poe${v} %U" \
			-n "Path of Building PoE ${v}" \
			-i ${PN} \
			-c "Game;Utility;" \
			-d "${PN}-${v}" \
			-e "Keywords=poe;pob;pob${v};pobc;path;exile;" \
			-e "MimeType=x-scheme-handler/pob${v/1/};" \
			-e "StartupWMClass=${PN}-${v}"
	done
}

pkg_postinst() {
	xdg_desktop_database_update

	einfo "If automatic updates fail, navigate to ~/.local/share/RustyPathOfBuilding{1,2}/"
	einfo "and delete both the rpob.version file and the Update directory. This will force"
	einfo "a complete re-download of PoB's latest assets and Lua code on the next startup."
}

pkg_postrm() {
	xdg_desktop_database_update
}