# Maintainer: Konstantin Gizdov <arch at kge dot pw>
# Maintainer: Bruno Pagani <archange@archlinux.org>
# Contributor: Jakob Gahde <j5lx@fmail.co.uk>

pkgname=dune
pkgver=3.15.0
pkgrel=1.1
pkgdesc="A composable build system for OCaml (formerly jbuilder)"
arch=(x86_64 aarch64)
url="https://github.com/ocaml/dune"
license=('Apache')
depends=('glibc')
makedepends=('ocaml' 'ocaml-compiler-libs' 'ocaml-findlib')
optdepends=('ocaml: Dune standard library')
source=("${url}/archive/${pkgver}/${pkgname}-${pkgver}.tar.gz")
b2sums=('6e3b1beff10eb45540783be4d16408c640e6efc17116a549488e737a0ef1abeac48966e7e9e05145b4d0b12c696f8a02b0aa9e2677a3ba390349a3e9fa4b352b')


_dune_release_pkgs=('dune' 'dune-action-plugin' 'dune-build-info' 'dune-configurator' 'dune-glob' 'dune-private-libs'
                    'dune-site' 'dune-rpc' 'dyn' 'stdune' 'ordering' 'xdg' 'chrome-trace' 'ocamlc-loc')

build() {
    cd "${srcdir}/${pkgname}-${pkgver}"

    # FS#74061
    ./configure --libdir /usr/lib/ocaml

    make _boot/dune.exe  # this runs `ocaml bootstrap.ml`, but keeps upstream's choice

    # dune build needs a comma separated list of packages
    local dune_release_pkgs='dummy'
    for _pkg in "${_dune_release_pkgs[@]}"; do
      dune_release_pkgs+=",${_pkg}"
    done
    dune_release_pkgs="${dune_release_pkgs#dummy,}"
    echo "Building packages: ${dune_release_pkgs}"

    ./dune.exe build -p "${dune_release_pkgs}" --profile dune-bootstrap
}

# Tests require a bunch of (currently) unpackaged dependencies
# check() {
#     cd "${srcdir}/${pkgname}-${pkgver}"
#     make test
# }

package() {
    cd "${srcdir}/${pkgname}-${pkgver}"

    # we need to install packages one by one
	for _pkg in "${_dune_release_pkgs[@]}"; do
      ./dune.exe install "${_pkg}" --destdir="${pkgdir}" --prefix="/usr" --libdir="$(ocamlfind printconf destdir)"
    done

    # Fix doc and man install
    install -d "${pkgdir}"/usr/share
    mv "${pkgdir}"/usr/{doc,share/}
    mv "${pkgdir}"/usr/{man,share/}
    install -dm755 "${pkgdir}/usr/share/licenses/${pkgname}"
    ln -s /usr/share/doc/pp/LICENSE.md "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
}
