#!/bin/bash
#http://redsymbol.net/articles/unofficial-bash-strict-mode/

set -x
set -euo pipefail
IFS=$'\n\t'


# change to project root
pushd "$( dirname "${BASH_SOURCE[0]}" )"/..

echo --- Clean apidoc
# Clean docs/(build|apidoc)
rm -rf docs/apidoc

echo --- Build html docs
filter_warnings() {
  # exit 1 on non-match, override with return 0
  grep -v -f docs/.expected_warnings || true
}

strip_color() {
  # exit 1 on non-match, override with return 0
  sed 's/\x1b\[[0-9;]*m//g' || true
}

# Add cwd to pythonpath to support apidoc/autodoc of tmol module
# will need to `pip install` if any built components are added
# in the future.
(
  trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM

  mkdir -p docs/_build
  export PYTHONPATH=.
  better-apidoc -T -M -e -t docs/_templates -o docs/apidoc tmol tmol/tests
  sphinx-build -M html docs docs/_build -w docs/_build/warnings.raw | filter_warnings

  # Strip expected warnings and colorized output
  cat docs/_build/warnings.raw | filter_warnings | strip_color | tee docs/_build/warnings
)

popd
