#!/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 setup in $(find ../../apps/cli ../../services ../../shared -name setup.py); do
  dir=$(dirname $setup)
  sphinx-apidoc -o api/$(basename $dir) $dir $dir/setup.py $dir/test $dir/tests
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.

API Documentation
--------------------------------------------

.. toctree::
   :maxdepth: 1

EOF

find . -name \*.bak -delete
find . -name modules.rst | sed 's|\./api/|   |g; s|\.rst||g' >> api/index.rst

cat >> api/index.rst <<EOF
EOF


# generate the architecture API index
cat > architecture.rst <<'EOF'
Architecture
============

The following pages are notes from the ARCHITECTURE.md files.

Contents:

.. toctree::
   :maxdepth: 2

EOF

# go into the architecture folder
rm -rf architecture
mkdir architecture

pushd architecture
for file in $(find ../../.. -name ARCHITECTURE.md); do
  mdfile=$(basename $(dirname $file)).md
  ln -s $file $mdfile
  echo "   architecture/$mdfile" >> ../architecture.rst
done
popd

popd
