#!/usr/bin/env python

import sys
import re
import string
import os

def doit(files):


    # regular expression for ' use modulename, only: stuff, other stuff'
    # see (txt2re.com)
    use_re = re.compile("( )(use)( )((?:[a-z_][a-z_0-9]+))", 
                        re.IGNORECASE|re.DOTALL)

    module_re = re.compile("( )(module)( )((?:[a-z][a-z_0-9]+))",
                           re.IGNORECASE|re.DOTALL)

    module_proc_re = re.compile("( )(module)( )(procedure)( )((?:[a-z][a-z_0-9]+))",
                                re.IGNORECASE|re.DOTALL)

    # first parse the files and find all the module statements.  Keep a
    # dictionary of 'module name':filename.
    modulefiles = {}

    for file in files:

        f = open(file, "r")
        
        line = f.readline()

        while (line):

            # strip off the comments
            idx = string.find(line, "!")
            line = line[:idx]

            rebreak = module_re.search(line)
            rebreak2 = module_proc_re.search(line)
            if (rebreak and not rebreak2):
                modulefiles[rebreak.group(4)] = file

            line = f.readline()


        f.close()


    # go back through the files now and look for the use statements.
    # Assume only one use statement per line.  Ignore any only clauses.
    # Build a list of dependencies for the current file and output it.
    print " "
    print "# modules dependencies:"
    for file in files:

        f = open(file, "r")

        line = f.readline()
        while (line):

            # strip off the comments
            idx = string.find(line, "!")
            line = line[:idx]

            rebreak = use_re.search(line)
            if (rebreak):
                print os.path.basename(file).replace(".f90", ".o"), ':', \
                    modulefiles[rebreak.group(4)].replace(".f90", ".o")

            line = f.readline()


        f.close()
        print " "

if __name__ == "__main__":

    files = sys.argv[1:]

    doit(files)




@@ -60,16 +60,30 @@
 mport os
+%0Aimport getopt
 %0A%0Adef do
@@ -81,24 +81,31 @@
 t%0A%0Adef doit(
+prefix,
 files):%0A%0A%0A  
@@ -101,17 +101,16 @@
 iles):%0A%0A
-%0A
     # re
@@ -1394,58 +1394,8 @@
 it.%0A
-    print %22 %22%0A    print %22# modules dependencies:%22%0A
     
@@ -1693,16 +1693,23 @@
   print 
+prefix+
 os.path.
@@ -1765,32 +1765,56 @@
                 
+prefix+os.path.basename(
 modulefiles%5Brebr
@@ -1826,16 +1826,17 @@
 roup(4)%5D
+)
 .replace
@@ -1958,39 +1958,299 @@
     
-files = sys.argv%5B1:%5D%0A%0A    doit(
+try: opts, next = getopt.getopt(sys.argv%5B1:%5D, %22%22, %5B%22prefix=%22%5D)%0A    except getopt.GetoptError:%0A        print(%22invalid options%22)%0A        sys.exit(2)%0A%0A    prefix = %22%22%0A%0A    for o, a in opts:%0A%0A        if o == %22--prefix%22:%0A            prefix = a%0A%0A            %0A    files = next%5B:%5D%0A%0A    doit(prefix, 
 file

add prefix