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

EAPI=8

inherit git-r3

DESCRIPTION="a small helper program for Whisker written in Haxe"
HOMEPAGE="https://github.com/corecathx/whisker-cli"
EGIT_REPO_URI="https://github.com/corecathx/whisker-cli.git"

HXCPP_VERSION="4.3.2"
SRC_URI="https://lib.haxe.org/p/hxcpp/${HXCPP_VERSION}/download/ -> hxcpp-${HXCPP_VERSION}.zip"

LICENSE="MIT"
SLOT=0
KEYWORDS=""

BDEPEND="
    app-arch/unzip
    dev-vcs/git
    ~dev-lang/haxe-4.3.7
"

src_unpack() {
	default
	git-r3_src_unpack

	einfo "Extracting Hxcpp.."
    	mkdir -p "${WORKDIR}/hxcpp-src" || die
    	unzip -q "${DISTDIR}/hxcpp-${HXCPP_VERSION}.zip" -d "${WORKDIR}/hxcpp-src" || die
}

src_compile() {
	export HAXELIB_PATH="${WORKDIR}/haxelib"    	
	export WHISKER_VERSION="${PV}"
	export WHISKER_COMMIT="${EGIT_VERSION:0:7}"

	mkdir -p "${HAXELIB_PATH}"
   	haxelib setup "${HAXELIB_PATH}"

    	local hxcpp_path=$(find "${WORKDIR}/hxcpp-src" -name "haxelib.json" -exec dirname {} \;)
    	if [[ -z "${hxcpp_path}" ]]; then
        	die "Hxcpp directory was not found in ${WORKDIR}. Aborting!"
    	fi
    	einfo "Register HXCPP from ${hxcpp_path}.."
    	haxelib dev hxcpp "${hxcpp_path}"

	haxe release.hxml
}

src_install() {
	dobin target/cpp/whisker
	einstalldocs
}