#!/usr/bin/env bash
# Build the odoc HTML documentation and open it in a browser.
#
# scripts/docs.sh build, then open
# scripts/docs.sh --print build, then print the entry point path
#
# Equivalent to `dune build @doc`; this wrapper exists only to locate the entry
# point, which is buried in the build directory.
set -euo pipefail
root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
cd "$root"
entry="_build/default/_doc/_html/index.html"
opam exec -- dune build @doc
if [[ ! -f $entry ]]; then
printf 'error: expected documentation at %s\n' "$entry" >&2
exit 1
fi
if [[ ${1-} == --print ]]; then
printf '%s/%s\n' "$root" "$entry"
exit 0
fi
url="file://$root/$entry"
# xdg-open detaches, so suppress the browser's own chatter on stderr.
if command -v xdg-open > /dev/null; then
xdg-open "$url" > /dev/null 2>&1 &
printf 'opened %s\n' "$url"
else
printf 'no xdg-open found; open this manually:\n%s\n' "$url"
fi