#!/bin/bash
#
# To run in debug mode (serial), type
#  ./build_docs debug
#
# To not rebuild notebooks, type
#  ./build_docs never
#
# Otherwise, Jupyter notebooks will be rebuilt in parallel.
#
# To not clean up after tutorial builds, run ./build_docs noclean

set -e  # Pass errors upwards

echo 'Building docs...'
start=$SECONDS
make clean

# Handle notebook build options
if [[ "$*" == *"never"* ]]; then
    export NBSPHINX_EXECUTE=never
elif [[ "$*" == *"always"* ]]; then
    export NBSPHINX_EXECUTE=always
else
    export NBSPHINX_EXECUTE=auto
fi

# Handle notebook build options
if [[ "$*" == *"debug"* ]]; then
    export BUILD_DEBUG=1
else
    export BUILD_DEBUG=
fi

# Actually make
make html 

# Clean up tutorials
if [[ "$*" != *"noclean"* ]]; then
    cd tutorials
    ./clean_outputs
    cd ..
fi

# Tidy up
duration=$(( SECONDS - start ))
echo "Docs built after $duration seconds."
echo "Index:"
echo "`pwd`/_build/html/index.html"
