#!/usr/bin/env python

import libgmail, os, string

out_dir = "/home/truiken/wine-git/out/"
diff_dir = "/home/truiken/wine-git/diff/"
author = ""
email = ""
password = ""

ga = libgmail.GmailAccount(email, password)
ga.login()

os.popen("git-format-patch -o out origin")

listing = os.listdir(out_dir)

for file in listing:
    f = open(out_dir + file, 'r+')
    print file
    subject = raw_input("Subject: ")
    if subject == "stop":
        os.remove(out_dir + file)
        continue

    body = "Hi,\n\nChangelog\n"

    lines = f.readlines()
    j = 1 
    
    changelog = lines[0] 
    changelog = string.lstrip(changelog, "Subject: [PATCH] ")
    while len(lines[j]) > 1:
        changelog += lines[j]
        j = j+1

    body += changelog
    body += "\n"
    j = j + 3 

    while len(lines[j]) > 1:
        body += lines[j]
        j = j+1

    body += "\n"
    body += "--\n"
    body += author

    j = j + 2
    diffname = diff_dir
    diffname += lines[j]
    diffname = string.rstrip(diffname, "\n")
    diffname += ".diff"

    out = open(diffname, 'a')
    for k in range(j + 1, len(lines) - 2):
        out.write(lines[k])

    msg = libgmail.GmailComposedMessage("wine-patches@winehq.org", subject, body, filenames=[diffname])
    ga.sendMessage(msg, True)

    os.remove(out_dir + file)
    string.rstrip(out_dir, "\\")
    os.remove(out_dir)
