# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=utop
pkgver=2.1.0
pkgrel=1
pkgdesc="Universal toplevel for OCaml"
url="https://github.com/diml/utop"
# x86, armhf, s390x: limited by ocaml aport
arch="all !x86 !armhf !s390x"
license="BSD-3-Clause"
depends="
	ocaml-$pkgname=$pkgver-r$pkgrel
	$pkgname-common=$pkgver-r$pkgrel
	"
_depends_ocaml="
	ocaml-runtime
	ocaml-camomile
	ocaml-findlib>=1.7.2
	ocaml-lambda-term
	ocaml-lwt
	ocaml-lwt-react
	ocaml-react
	"
makedepends="
	dune
	ocaml
	ocaml-compiler-libs
	ocaml-camomile-dev
	ocaml-cppo
	ocaml-findlib-dev
	ocaml-lambda-term-dev
	ocaml-lwt-dev
	ocaml-react-dev
	opam
	"
subpackages="
	$pkgname-full:_full
	$pkgname-common:_common:noarch
	$pkgname-doc
	$pkgname-emacs::noarch
	ocaml-$pkgname-dev:_libdev
	ocaml-$pkgname:_lib
	"
source="$pkgname-$pkgver.tar.bz2::https://github.com/diml/$pkgname/releases/download/$pkgver/$pkgname-$pkgver.tbz"
builddir="$srcdir/$pkgname-$pkgver"

build() {
	cd "$builddir"
	jbuilder build @install
}

check() {
	cd "$builddir"
	jbuilder runtest
}

package() {
	cd "$builddir"

	opam-installer -i \
		--prefix="$pkgdir/usr" \
		--libdir="$pkgdir/usr/lib/ocaml" \
		--docdir="$pkgdir/doc" \
		--mandir="$pkgdir/usr/share/man" \
		$pkgname.install

	# There's just a readme and changelog.
	rm -Rf "$pkgdir"/doc

	# Remove annotation files and sources.
	cd "$pkgdir"/usr/lib/ocaml/$pkgname
	rm -f *.cmt* *.ml
}

_full() {
	pkgdesc="$pkgdesc (non-expunged)"

	_submv usr/bin/utop-full
}

_common() {
	pkgdesc="Common files for utop and utop-full"
	depends=""

	_submv usr/share/$pkgname
}

emacs() {
	pkgdesc="utop plugin for Emacs"
	depends="$pkgname=$pkgver-r$pkgrel"
	install_if="$pkgname=$pkgver-r$pkgrel emacs"

	_submv usr/share/emacs
}

_libdev() {
	pkgdesc="$pkgdesc (development files)"
	depends="ocaml-$pkgname=$pkgver-r$pkgrel"
	local sitelib="usr/lib/ocaml/$pkgname"

	_submv $sitelib/*.mli
}

_lib() {
	pkgdesc="$pkgdesc (library)"
	depends="$_depends_ocaml"

	_submv usr/lib/ocaml
}

_submv() {
	local path="$1"
	mkdir -p "$subpkgdir"/${path%/*}
	mv "$pkgdir"/$path "$subpkgdir"/${path%/*}/
	rmdir -p "$pkgdir"/${path%/*} 2>/dev/null || true
}

sha512sums="5c18daad46569fccbaadebd258be8afa5c0499853b026260f12f6dec3b05c77875d555fb538a7008a994d9e4b7731128927a6cc1fece455dfe227954ee12372c  utop-2.1.0.tar.bz2"
