#!/user/belboz/bin/python

import os
import sys
import string
import getopt

def outputcopyright_c(file):
    file.write(" * '1997, Barry Brumitt, Carnegie Mellon. All Rights Reserved.' This software\n")
    file.write(" * is made available for academic and research purposes only. No commercial\n")
    file.write(" * license is hereby granted.  Copying and other reproduction is authorized\n")
    file.write(" * only for research, education, and other non-commercial purposes.  No\n")
    file.write(" * warranties, either expressed or implied, are made regarding the\n")
    file.write(" * operation, use, or results of the software.\n")
    file.write(" *\n")


def outputcopyright_py(file):
    file.write("# '1997, Barry Brumitt, Carnegie Mellon. All Rights Reserved.' This software\n")
    file.write("# is made available for academic and research purposes only. No commercial\n")
    file.write("# license is hereby granted.  Copying and other reproduction is authorized\n")
    file.write("# only for research, education, and other non-commercial purposes.  No\n")
    file.write("# warranties, either expressed or implied, are made regarding the\n")
    file.write("# operation, use, or results of the software.\n")
    file.write("# \n")
    

def addcopyright(name,python_flag):
    try:
	i_file = open(name,"r")
    except:
	print "Couldn't open file:",name
	return
    o_file = open(name+".2","w")
    done = 0
    for l in i_file.readlines():
	if (done == 0):
	    s = string.split(l)
	    if (len(s)>1) and (s[1] == "warranties,"):
		# This lets us avoid multiple writes.
		print "File already done: ",name
		done = 1
	    if (len(s)>1) and (s[1] == "PROJECT:"):
		print "Copyright added to ",name
		if (python_flag == 0): 
		    outputcopyright_c(o_file)
		else:
		    outputcopyright_py(o_file)
		done = 1
	o_file.write(l)

def find_cfiles(dir):
    result = []
    filelist = os.popen("ls "+dir).readlines();
    for i in filelist:
	if (len(i) > 2):
	    if (i[-3:-1] == ".c") or (i[-3:-1] == ".h"):
		result.append(i[:-1])
    return(result)


def find_pyfiles(dir):
    result = []
    filelist = os.popen("ls "+dir).readlines();
    for i in filelist:
	if (len(i) > 3):
	    if (i[-4:-1] == ".py"):
		result.append(i[:-1])
    return(result)

def find_makefiles(dir):
    result = []
    filelist = os.popen("ls "+dir).readlines();
    for i in filelist:
	if (len(i) > 5):
	    if (i[0:4] == "Make") or (i[0:4] == "make"):
		if (i[-2:-1] != "~") and (i[-2:-1] != "2") and (i[-4:-1] != "old"):
		    result.append(i[:-1])
    return(result)

def dodir(dir):
    for i in find_cfiles(dir):
	filename = dir+"/"+i
	addcopyright(filename,0)
	os.popen("mv "+filename+" "+filename+".old").readlines()
	os.popen("mv "+filename+".2 "+filename).readlines()
    for i in find_pyfiles(dir):
	filename = dir+"/"+i
	addcopyright(filename,1)
	os.popen("mv "+filename+" "+filename+".old").readlines()
	os.popen("mv "+filename+".2 "+filename).readlines()
    for i in find_makefiles(dir):
	filename = dir+"/"+i
	addcopyright(filename,1)
	os.popen("mv "+filename+" "+filename+".old").readlines()
	os.popen("mv "+filename+".2 "+filename).readlines()


# handles all files in all dirs provided on the command line.
for d in  sys.argv[1:]:
    print "Adding copyrights to dirctory: ",d
    dodir(d)


    

