#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import httplib
import urllib
import urllib2
import traceback
import re
import random
import time


proxies = [
    "72.247.52.10:80",
    "72.246.132.10:80",
    "72.247.76.10:80",
    "72.247.20.10:80",
    "72.246.152.10:80",
    ]


def translate(text):
    addr = "http://www.google.com/translate_t?sl=en&tl=zh-CN"

    proxies.insert(0, proxies.pop())
    
    for proxy in proxies[:]:
        try:
            req = urllib2.Request(addr)
            req.add_header("User-Agent", "Mozilla/5.0")
            req.add_data(urllib.urlencode({"text": text}))
            if proxy:
                req.set_proxy(proxy, "http")
            trans = urllib2.urlopen(req).read()
            trans = re.search("<div id=result_box dir=\"ltr\">(.*?)</div>", 
                              trans).group(1)
            trans = re.sub("&#(\d+);", 
                           lambda s: chr(int(s.group(1))), 
                           trans)
            trans = re.sub("&([a-z]+);", 
                           lambda s: {"lt":"<", "gt":">", "amp":"&"}[s.group(1)],
                           trans)
            return trans
        except:
            proxies.remove(proxy)
            print >>sys.stderr, "Proxy: %s" % proxy
            traceback.print_exc()

    return None
            

def translate_fixed(text):
#    signature = random.randrange(1E20, 1E21)
    notrans = []
    def replace(match):
        notrans.append(match.group(0))
        return " 0.%d68065175210" % (len(notrans) - 1)
    text = re.sub("\${[\w_]+}|\$[\w_]+", replace, text)
    text = re.sub("\\\\\"|\\\\$|\\\\\\\\n|\\\\t", replace, text)
    text = re.sub("\\\\", replace, text)
    text = re.sub("[A-Z]{2,100}", replace, text)
    text = re.sub("<\w+>|</\w+>", replace, text)
    text = re.sub("puppy(?i)", replace, text)

#     print text
#    print "TEXT=" + text
    trans = translate(text)
#    print "NOTRANS=",
#    print notrans
#    print "TRANS=" + trans

    for i in range(len(notrans)):
#        print "re.sub",
#        print "%dx" % (signature + i), notrans[i], trans
        trans = re.sub("0 ?.%d68065175210" % i, lambda x: notrans[i], trans)

    return trans


random.seed()

if len(sys.argv) != 2:
    print "Usage: " + sys.argv[0] + " filename.pot"
    sys.exit(1)

if sys.argv[1] == "-":
    f = sys.stdin
else:
    f = open(sys.argv[1])


print """# 中文Puppy Linux开发者之家.
# This file is distributed under GPL.
#"""

print """#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: %(program)s\\n"
"Report-Msgid-Bugs-To: %(reportbug)s\\n"
"POT-Creation-Date: %(date)s\\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
"Last-Translator: Google Translate\\n"
"Language-Team: Chinese\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=utf-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
""" % {"program": sys.argv[1],
       "reportbug": "laborer@126.com",
       "date": time.strftime("%Y-%m-%d %H:%M%z")}


for line in f:
    if not line.rstrip():
        break;

trans = []
buf = ""
skip = False
for line in f:
    line = line.rstrip()
    if skip:
        skip = line;
        continue

    if line.startswith("#,"):
        print line + ", fuzzy"
    elif line.startswith("#"):
        print line
    elif line.startswith("msgid "):
        print "msgid",
        line = line[6:]
    elif line.startswith("msgstr "):
        print "msgstr",
        if buf:
            trans.append(translate_fixed(buf))
            buf = ""
        for s in trans:
            print "\"%s\"" % s
        print
        trans = []
        skip = True

    if line.startswith("\""):
        print line
        buf += line[1:-1]
        if buf.endswith("\\n"):
            trans.append(translate_fixed(buf[:-2])+"\\n")
            buf = ""
