# Contributor: rubicon <rubicon@mailo.com>
# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=ocaml-omod
_pkgname=omod
pkgver=0.0.3
pkgrel=3
pkgdesc="Lookup and load installed OCaml modules"
url="https://erratique.ch/software/omod"
arch="all !riscv64 !loongarch64" # ocaml
license="ISC"
depends="$pkgname-bin"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="
	ocaml
	ocamlbuild
	ocaml-compiler-libs
	ocaml-findlib
	ocaml-topkg
	ocaml-cmdliner-dev
	cmd:opam-installer
	"
options="!check"  # no tests provided
subpackages="$pkgname-bin $pkgname-dev"
source="$pkgname-$pkgver.tar.bz2::https://erratique.ch/software/omod/releases/omod-$pkgver.tbz
	fix-cache-dir.patch
	"
builddir="$srcdir/$_pkgname-$pkgver"
_ocamldir=usr/lib/ocaml

# 32-bit archs
case "$CARCH" in
	arm*|x86) options="$options textrels" ;;
esac

build() {
	ocaml pkg/pkg.ml build \
		--lib-dir "$(ocamlc -where)"
}

package() {
	opam-installer -i \
		--prefix="$pkgdir"/usr \
		--libdir="$pkgdir/$_ocamldir" \
		--docdir="$builddir"/.omit \
		$_pkgname.install
}

bin() {
	pkgdesc="$pkgdesc (standalone binary)"
	depends="cmd:ocamlc"
	amove usr/bin/omod
}

dev() {
	default_dev

	cd "$pkgdir"

	local path; find $_ocamldir \( \
			-name '*.cmt' -o \
			-name '*.cmti' -o \
			-name '*.ml' -o \
			-name '*.mli' \
		\) | while read -r path
	do
		amove "$path"
	done
}

prepare_py_provides() {
	local datadir="${subpkgdir:-$pkgdir}"
	local pkgbasedir=${pkgbasedir:-"$startdir/pkg"}
	local controldir="$pkgbasedir"/.control.${subpkgname:-$pkgname}

	options_has "!tracedeps" && return 0

	ocaml4-abuild-find provides \
		"$datadir"/usr/lib/ocaml \
		"$controldir" \
		"$pkgver-r$pkgrel"
}

scan_python3_dependency() {
	local controldir="$2" datadir="$3"

	ocaml4-abuild-find requires \
		"$datadir"/usr/lib/ocaml \
		"$controldir"
}

sha512sums="
4f53b8cdd054dc1a6813427452a91294e0bbcfefe948fc1caec47be136dbcecf13112bf2b620fa2f667592b04b28df74e3bf012ea0fb0038c1da4217155ca626  ocaml-omod-0.0.3.tar.bz2
a53e77bc22f6b8a45b664973c648234155c5b3e1cd3526ac0713e91cf6d40bede7e8f084684203c027495c70d7ccf8ebf678a4cbe13e61f22eaa5444a2a1987d  fix-cache-dir.patch
"
