#!/bin/bash

# Regenerate the API documentation for the project.
#
# A handy trick in case we build new modules and need to bring them into the documentation system.

# remove the existing source/api folder
rm -rf source/api

# go into the source folder
pushd source

# for each application we have, run sphinx-apidocs
for dir in ../../apps/cli/*/* ../../shared/* ../../services/*; do
  [[ ! $dir =~ .*__pycache__.* ]] && sphinx-apidoc -o api/$(basename $dir) $dir
done

# generate the header for the API index
cat > api/index.rst <<'EOF'
.. Workspaces documentation master file, created by
   sphinx-quickstart on Thu Oct 15 14:12:32 2020.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

Welcome to the Workspaces API documentation!
--------------------------------------------
Hello!

.. toctree::
   :maxdepth: 2

EOF

find . -name modules.rst -exec sed -i.bak '/^ *setup *$/d' {} \;
find . -name \*.bak -o -name setup.rst -delete
find . -name modules.rst | sed 's|\./api/|   |g; s|\.rst||g' >> api/index.rst

cat >> api/index.rst <<EOF

.. note::
   If you have any feedback or requests for the documentation, let Nathan know.
EOF

popd