#!/usr/bin/env python

from abjad.cfg.cfg import ABJADPATH
from _create_subdirs_interactive import _create_subdirs_interactive
from _filter_files import _filter_files
from _filter_directories import  _filter_directories
from _get_module_members import _get_module_members
from _write_file_interactive import _write_file_interactive
from make_sphinx_toc import make_sphinx_toc
from make_sphinx_module_listing import make_sphinx_module_listing
import os
import types


api_doc_path = os.path.join(ABJADPATH, 'documentation', 'chapters', 'api')

def make_api_tocs( ):
   modules_visited = [ ]
   interactive = raw_input('Run script in interactive mode? [Y/n]: ')
   for current_root, dirs, files in os.walk(ABJADPATH):
      ## removed unwanted dirs and files.
      filtered_dirs = ['.svn', 'book', 'cfg', 'checks', 'debug', 'demos', 
         'documentation', 'exceptions', 'navigator', 'scm', 'scr', 'test', 
         'update',
         ]
      _filter_directories(dirs, filtered_dirs)
      _filter_files(files)
      dirs.sort( )
      files.sort( )

      ## make subdirectories in docs if needed
      #abjad_subdir = current_root[current_root.rindex('abjad'):]
      abjad_subdir = current_root.split('abjad')[-1].strip(os.path.sep)
      root_dir = os.path.join(api_doc_path, abjad_subdir)
      _create_subdirs_interactive(root_dir, dirs, interactive)

      ## make shinx listing of modules
      for file in files:
         listing = make_sphinx_module_listing('abjad/' + abjad_subdir, file)
         #print listing

         if listing is None:
            continue
         
         ## write listing to file
         rst_file = file.split('.')[0] + '.rst'
         index = os.path.join(root_dir, rst_file)
         _write_file_interactive(listing, index, interactive)

         ## keep track of modules visited
         mod = os.path.join(abjad_subdir, file.split('.')[0])
         modules_visited.append(mod)

   ## toc logic changed to divide into classes and functions
   #toc = make_sphinx_toc(modules_visited)
   toc = make_sphinx_toc( )
   #print toc

   index = os.path.join(api_doc_path, 'index.rst')
   _write_file_interactive(toc, index, interactive)


if __name__ == '__main__':
   make_api_tocs( )
   print ''
   print "Done. You are now ready to build the HTML docs with 'make html'."
   print ''
