import os
import os.path

TO_INSERT = "./general/footer.code"
LOCATION = "</body>"

istream = file(TO_INSERT,"r")
content = istream.read()
istream.close()

htmls = []
files = os.walk('.')
for x in files:
    dirpath = x[0]
    files = x[2]
    
    for y in files:
        if y.endswith('html'):
            htmls.append(os.path.join(dirpath,y))

for x in htmls:
    print "Processing: ", x
    
    try:
        os.rename(x,x+".old")
    except:
        os.remove(x+".old")
        os.rename(x,x+".old")
        
    instream = file(x+".old","r")
    outstream = file(x,"w")
    for line in instream:
        if line.lower().find(LOCATION) >= 0:
            outstream.write(content)
        outstream.write(line)
    instream.close()
    outstream.close()
    os.remove(x+".old")

    
