# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
# TODO: Verify declared dependencies.
pkgname=js_of_ocaml
pkgver=3.1.0
pkgrel=2
pkgdesc="Compiler from OCaml to Javascript"
url="http://ocsigen.org/js_of_ocaml/"
arch="all !x86 !armhf !s390x"  # limited by ocaml aport
license="LGPL-2.0-or-later"
depends="
	$pkgname-compiler=$pkgver-r$pkgrel
	ocaml-migrate-parsetree
	ocaml-ppx_tools_versioned
	ocaml-uchar
	"
checkdepends="nodejs ocaml-deriving"
makedepends="
	bash
	dune
	ocaml
	ocaml-camlp4-dev
	ocaml-cmdliner-dev
	ocaml-compiler-libs
	ocaml-cppo-dev
	ocaml-findlib-dev
	ocaml-lwt-dev
	ocaml-migrate-parsetree-dev
	ocaml-ocamlbuild-dev
	ocaml-ppx_deriving-dev
	ocaml-ppx_tools-dev
	ocaml-ppx_tools_versioned-dev
	ocaml-react-dev
	ocaml-reactivedata-dev
	ocaml-result-dev
	ocaml-tyxml-dev
	ocaml-uchar-dev
	ocaml-yojson-dev
	ocamlbuild
	opam
	"
options="!check"  # FIXME: needs additional dependencies
subpackages="$pkgname-dev $pkgname-doc"
source="$pkgname-$pkgver.tar.gz::https://github.com/ocsigen/$pkgname/archive/$pkgver.tar.gz"
builddir="$srcdir/$pkgname-$pkgver"

for _i in camlp4 compiler lwt ocamlbuild ppx ppx_deriving_json toplevel tyxml; do
	subpackages="$subpackages $pkgname-$_i"
done

build() {
	cd "$builddir"
	jbuilder build @install @DEFAULT -j ${JOBS:-2}
}

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

package() {
	cd "$builddir"

	mkdir -p "$pkgdir"/usr/lib/ocaml
	jbuilder install \
		--destdir="$pkgdir"/usr \
		--libdir="$pkgdir"/usr/lib/ocaml

	cd "$pkgdir"

	_mv usr/man usr/share/

	# There's just a readme and changelog.
	rm -Rf usr/doc

	# These are just duplicates.
	rm -f usr/bin/*.exe

	# Remove annotation files and sources.
	find usr/lib/ocaml \
		\( -name '*.cmt' -o -name '*.cmti' -o -name '*.ml' \) \
		-a -delete
}

dev() {
	default_dev

	cd "$pkgdir"

	local path
	for path in $(find usr/lib/ocaml/ -name '*.cmx' -o -name '*.cmxa' -o -name '*.mli'); do
		_mv "$path" "$subpkgdir"/${path%/*}/
	done
}

camlp4() {
	pkgdesc="$pkgdesc (Camlp4 syntax extension)"
	depends="ocaml-camlp4"
	_subpackage
}

compiler() {
	pkgdesc="$pkgdesc (compiler)"
	depends="ocaml ocaml-cmdliner ocaml-cppo ocaml-findlib ocaml-yojson"

	_subpackage

	cd "$pkgdir"/usr/bin
	_mv js_of_ocaml jsoo_minify jsoo_link "$subpkgdir"/usr/bin/
}

lwt() {
	pkgdesc="$pkgdesc (lwt)"
	depends="$pkgname=$pkgver-r$pkgrel
		$pkgname-ppx=$pkgver-r$pkgrel
		ocaml-lwt"
	_subpackage
}

ocamlbuild() {
	pkgdesc="$pkgdesc (OCamlbuild plugin)"
	depends="ocamlbuild"
	_subpackage
}

ppx() {
	pkgdesc="$pkgdesc (ppx syntax extensions)"
	depends="$pkgname=$pkgver-r$pkgrel
		ocaml-migrate-parsetree
		ocaml-ppx_tools_versioned"
	_subpackage
}

ppx_deriving_json() {
	pkgdesc="$pkgdesc ([@@deriving json])"
	depends="$pkgname ocaml-ppx_tools ocaml-ppx_deriving"
	_subpackage
}

toplevel() {
	pkgdesc="Web-based OCaml toplevel"
	depends="$pkgname=$pkgver-r$pkgrel
		$pkgname-compiler=$pkgver-r$pkgrel
		$pkgname-ppx=$pkgver-r$pkgrel
		ocaml-compiler-libs
		ocaml-findlib"

	_subpackage

	cd "$pkgdir"/usr/bin
	_mv jsoo_mkcmis jsoo_mktop jsoo_listunits "$subpkgdir"/usr/bin/
}

tyxml() {
	pkgdesc="$pkgname (Tyxml)"
	depends="$pkgname=$pkgver-r$pkgrel
		$pkgname-ppx=$pkgver-r$pkgrel
		ocaml-reactivedata
		ocaml-tyxml"
	_subpackage
}

_subpackage() {
	depends="ocaml-runtime $depends"
	_mv "$pkgdir"/usr/lib/ocaml/$subpkgname "$subpkgdir"/usr/lib/ocaml/
}

_mv() {
	local dest; for dest; do true; done  # get last argument
	mkdir -p "$dest"
	mv "$@"
}

sha512sums="63bd4961fbdc677cc992d253a7254a159d86c444b6aa5a8b2fac446925f35fbc8454865db167364cb2c2b7f02b113ebb0c0ac1cb5d2f7d7ae06519c68cdbc58a  js_of_ocaml-3.1.0.tar.gz"
