abinfo "Building Dune's core executable ..."
"$SRCDIR"/configure --libdir="$(ocamlc -where)"
ocaml "$SRCDIR"/boot/bootstrap.ml --verbose

abinfo "Building Dune packages ..."
# Note: Ensure `dune` is last, otherwise it will install error:
# * Error: Invalid dune file
DUNE_RELEASE_PKGS=(
    'dune'
    'dune-action-plugin'
    'dune-build-info'
    'dune-configurator'
    'dune-glob'
    'dune-private-libs'
    'dune-rpc'
    'dune-site'
    'chrome-trace'
    'dyn'
    'fs-io'
    'ordering'
    'stdune'
    'xdg'
    'top-closure'
    'ocamlc-loc'
)

_DUNE_RELEASE_PKGS=$(IFS=','; echo "${DUNE_RELEASE_PKGS[*]}")

"$SRCDIR"/dune.exe build \
    -p "${_DUNE_RELEASE_PKGS}" \
    --profile dune-bootstrap \
    --verbose

abinfo "Installing Dune and related files ..."
"$SRCDIR"/dune.exe install \
    -p "${_DUNE_RELEASE_PKGS}" \
    --prefix "$PKGDIR"/usr \
    --libdir "$PKGDIR"/usr/lib/ocaml

abinfo "Removing unnecessary files ..."
rm -rv "$PKGDIR"/usr/doc

abinfo "Correcting directories ..."
mv -v "$PKGDIR"/usr/{,share/}man
