# Contributor: Anjandev Momi <anjan@momi.ca>
# Maintainer: Anjandev Momi <anjan@momi.ca>
pkgname=haxe
_haxelib_commit=f17fffa97554b1bdba37750e3418051f017a5bc2
pkgver=4.3.3 # limited to 4.3.3 by testing/snikket-sdk
_luv_version=0.5.14
_extlib_version=1.7.8
_hx3compat_commit=f1f18201e5c0479cb5adf5f6028788b37f37b730
pkgrel=1
pkgdesc="The Cross-Platform Toolkit"
url="https://haxe.org"
arch="all !riscv64 !loongarch64" # limited by community/ocaml
license="GPL-2.0-or-later"
depends="musl mbedtls pcre2 zlib"
makedepends="
	aspcud
	bash
	m4
	make
	mbedtls-dev
	musl-dev
	neko
	neko-dev
	ocaml
	ocaml-compiler-libs
	ocaml-ocamldoc
	opam
	patch
	pcre2-dev
	perl-ipc-system-simple
	perl-string-shellquote
	pkgconf
	rsync
	unzip
	zlib-dev
	"
subpackages="$pkgname-doc"
source="$pkgname-$pkgver.tar.gz::https://github.com/HaxeFoundation/haxe/archive/refs/tags/$pkgver.tar.gz
	haxelib-$_haxelib_commit.tar.gz::https://github.com/HaxeFoundation/haxelib/archive/$_haxelib_commit.tar.gz
	hx3compat-$_hx3compat_commit.tar.gz::https://github.com/HaxeFoundation/hx3compat/archive/$_hx3compat_commit.tar.gz
	haxe.opam.locked
	luv0.5.13.patch
	mbedtls3.patch
	"
options="!strip" # enabling stripping removes core libraries needed to build snikket-sdk

export OPAMROOT="${OPAMROOT:-"$srcdir/opam-root"}"

prepare() {
	rm -rf extra/haxelib_src
	cp -rv "$srcdir/haxelib-$_haxelib_commit" extra/haxelib_src

	rm -rf extra/haxelib_src/hx3compat
	cp -rv "$srcdir/hx3compat-$_hx3compat_commit" extra/haxelib_src/hx3compat

	default_prepare

	opam init -a --disable-sandboxing --compiler=ocaml-system
	eval "$(opam env)"
	opam pin add luv $_luv_version --no-action
	opam pin add extlib $_extlib_version --no-action
	opam install -y --deps-only "$srcdir"/haxe.opam.locked
}

build() {
	# Prepare opam and ocamlfind before building
	eval "$(opam env --switch=ocaml-system)"
	export OCAMLPARAM="_,w=-a"
	make
}

check() {
	./haxe -version
	./haxelib version
}

package() {
	install -d "$pkgdir/usr/bin/"

	install -d "$pkgdir/usr/share/$pkgname/"
	cp -rf std "$pkgdir/usr/share/$pkgname/"

	install -Dm755 haxe -t "$pkgdir"/usr/bin/
	install -Dm755 haxelib -t "$pkgdir"/usr/bin/
	install -Dm644 extra/LICENSE.txt "$pkgdir"/usr/share/licenses/$pkgname/COPYING
}

sha512sums="
72fd7370f4a42d3524bad78e4ab2b4e839e4d9db6f4e8a942bdddfbe93f97c11ee8e7e96c524b5f8ab0a37a0e151cdeb4dd1d085f6093b70adb555cab6113b0b  haxe-4.3.3.tar.gz
adc80c20833062a6639f352861a950a4289a06ba97dcc57e327186cec8aa7988e17b5f4df518ced9a371ae4f0fd7d065bd79f400aa48045081066fa784c6b837  haxelib-f17fffa97554b1bdba37750e3418051f017a5bc2.tar.gz
d3df767744c073715a5deb9e1b602006aded8335d7afe28686e9440c4d9fabe6eb29314beb7b929bb7a76de68a815a9a7c7a49d2a92183e9a5331923f70249ff  hx3compat-f1f18201e5c0479cb5adf5f6028788b37f37b730.tar.gz
f519c93926b0a5e5bfb83a11dfd3c77f18b4c6bcbc47db48e00e454fcd40862b547327fc3c093ba9d9cd48fcc849f3b9f9b6fb23ec86de208aa64792fb81e538  haxe.opam.locked
779cd88a3ba859287c4db1d640ea32b58649154fe14f3e72f15193b655fee1aacce897813d677c871cebb6600d3a368841c89ffc34926639630849b96d1dce35  luv0.5.13.patch
b8691ebf06c1a37f3a9b89fd5b6b73fa59a93577ba46208bc9a87ca1f5a02a6dfed072773aaebe2d7961e4c054cddcdce0441ce17c14b61e4025d2d56754017c  mbedtls3.patch
"
