#!/usr/bin/env python
# $Id: doc,v 1.2 2003/12/18 21:25:49 pletzer Exp $

import sys, getopt, glob, re
import docModule, docRoutine, docF90

def usage():
    print """
Usage:
%s {-a|--all}                           Generate complete doc
%s {-f|--file} <file.f90>               Generate doc for <file.f90>
%s {-m|--module} <module> <file.f90>    Generate doc for module named <module> in file <file.f90>
%s {-r|--routine} <routine> <file.f90>  Generate doc for function/subroutine named <routine> in file <file.f90>
""" % tuple([sys.argv[0] for i in range(4)])

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "af:m:r:", ["all", "file=", "module=", "routine="])
    except getopt.GetoptError:
        # print help information and exit:
        usage()
        sys.exit(2)
    for o, a in opts:
        if o in ("-a", "--all"):
            all_f90_files = glob.glob('./*.f90')
            for f in all_f90_files:
                lines = file(f, 'r').read()
                d = docF90.docTree(f)
                #d.out()
                flag = 0
                for k in d.data:
                    for k2 in d.data[k]:
                        if d.data[k][k2]:
                            flag = 1
                            break
                if  not flag:
                    # nothing interesting in this file
                    continue
                    
                print """\n\n
FILE %s

""" % f
                #print d.data
                for m in d.data:
                    dm = docModule.docModule(m, lines)
                    dm.out()
                    print '\n == in module ' + m + ' == '
                    for s in dm.data['contains']:
                        s = re.sub(r'\([^\)]+\)', '', s)
                        s = re.sub(r'FUNCTION\s+', '', s)
                        s = re.sub(r'^\s*SUBROUTINE\s+', '', s)
                        #print s
                        dr = docRoutine.docRoutine(s, lines)
                        #print dr.data
                        dr.out()
                    
        if o in ("-f", "--file"):
            d = docF90.docTree(a)
            d.out()
        if o in ("-m", "--module"):
            lines = file(sys.argv[-1], 'r').read()
            d = docModule.docModule(a, lines)
            d.out()
        if o in ("-r", "--routine"):
            lines = file(sys.argv[-1], 'r').read()
            d = docRoutine.docRoutine(a, lines)
            d.out()
    # ...

if __name__ == "__main__":
    main()

        
