#!/bin/bash
#
# Build our mdbook output
#
# nailing-cargo --- maint/build-mdbook

set -e
set -o pipefail

function usage() {
    cat <<EOF
build-mdbook: Generate and check the derive-deftly mdbook documentation

Usage:
    build-mdbook [-h]

Options:
    -h: Print this message.

Notes:
   Must be run from the top level directory of derive-deftly.
EOF
}

while getopts "h" opt; do
    case $opt in
	h) usage
	   exit 0
	   ;;
	*) echo "Unknown option.  Use -h for help."
           exit 1
	   ;;
    esac
done

if test $# != 0; then
    echo "Unexpected argument.  Use -h for help."
    exit 1
fi

# mdbook-linkcheck is a non-obvious dependency, and the mdbook output when it's
# not found doesn't spell out how to install it.
if ! command -v mdbook-linkcheck; then
    echo 'ERROR: mdbook-linkcheck not found. You should probably install it with "cargo install mdbook-linkcheck"'
    exit 1
fi

if ! command -v linklint; then
    echo "ERROR: linklint not found.  You should install it according to platform instructions."
    exit 1
fi

if ! test -f ./book/book.toml; then
    echo "ERROR: ./book/book.toml not found.  You need to run this from the toplevel directory."
    exit 1
fi

: "${MDBOOK:=mdbook}"

cd ./book
$MDBOOK build
cd ./book
../../maint/check-mdbook-links

cat <<END
formatted mdbook book is here:
  $PWD/html/index.html
  file://$PWD/html/index.html

END
