'''
Created on Jun 20, 2012

@author: Sandro La Bruzzo
'''
import fnmatch
import os
import sys
import shutil

hascomment=0
notComment=0
def getAllJavaSources(basePath):
    matches=[]
    for root, dirnames, filenames in os.walk(basePath):
        for filename in fnmatch.filter(filenames, '*.java'):
            matches.append(os.path.join(root, filename))
    return matches


def getModuleName(path, baseModuleName):
    start=path.find(baseModuleName)
    if start>=0:
        p=path.find('/',start+len(baseModuleName)+1)
        return path[start++len(baseModuleName)+1:p]
    
def process(content, filename, position, name):
    global hascomment
    global notComment
    startComment= content.find("/**", 0, position+1)
    startPackage= content.find("package", 0, position+1)    
    if startComment>=0:
        if(startComment<startPackage):
            startComment=content.find("/**", startPackage, position+1)
        i=startComment
        while i<len(content):
            if content[i]=='*' and content[i+1]=='/':
                nline=i-1
                break
            i+=1
        if i>=len(content):
            print "ERROR I must not more the content"
               
        f1= open(filename+".new", "w")
        f1.write(content[0:nline]+" *\n * @dnetModule "+getModuleName(filename, name) +"\n"+content[nline:])
        f1.close()
        shutil.move(filename+".new",filename)
        return       
    else:
        i=position
        while i>0:
            if content[i]=='\n':
                nline=i                
                f1= open(filename+".new", "w")
                f1.write(content[0:nline]+"\n\n /**\n * @dnetModule "+getModuleName(filename, name) +" \n */ \n"+content[nline:])
                f1.close()
                shutil.move(filename+".new",filename)
                return               
            i-=1
        print "ERROR I cannot be zero"
    

    
if (len(sys.argv) > 1):
    if( sys.argv[1] == '-path' ):
        if len(sys.argv)>=2:
            path=sys.argv[2]
    if( sys.argv[3] == '-name' ):        
        if len(sys.argv)>=2:
            name=sys.argv[4]
if os.path.exists(path)==False:
    print "the path does not exist"    
    sys.exit()




files=getAllJavaSources(path)


for pfile in files:
    file= open(pfile,"r")
    content= file.read()
    if content.find("dnetModule")>0:
        continue
    startClass= content.find("public class")
    if startClass==-1:
        startInterface=content.find("public interface")
        if startInterface>=0:
            process(content, pfile, startInterface, name)
        else:            
            startabstract=content.find("abstract class")
            if startabstract>=0:
                process(content, pfile, startabstract, name)
            else :
                if content.find("class")>=0:
                    process(content, pfile, content.find("class"), name)
                elif content.find("interface")>=0:
                    process(content, pfile, content.find("interface"), name)
                elif content.find("public enum")>=0:
                    process(content, pfile, content.find("public enum"), name)        
    else:        
        process(content, pfile, content.find("public class"), name)

print "DONE! :)"

