#!/usr/bin/env python
# Copyright 2013 - Eric "Sparks" Christensen <eric@christensenplace.us> 0x024BB3D1
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Reads pot files to create .tx/config file for Transifex.
#

import os

# Get answers to variables.
default = "https://www.transifex.com"
host = raw_input("What Transifex URL do you use? (https://www.transifex.com) : ")
if not host:
	host = default
print ("Okay, I'll use " + host + " for the host.")

default = "en"
lang = raw_input("In which language is the source written? (en) : ")
if not lang:
	lang = default
print ("Using " + lang + " for the source language.")

guide = raw_input("What guide are you working with here? (Example: fedora-release-notes) : ")
if not guide:
	print ("Sorry, I really need to know what guide this is for.")
	exit()
print ("Okay, using " + guide + " for this instance.")

#getpath = "What path shall I look for your pot files?"
#print (getpath)
#path = raw_input()
#print "Okay, using " + path + " today."

# Search in path for files with a .pot suffix and provide a relative path and file name.
txconfig = ".tx/config"
if os.path.isfile(txconfig):
	print ("I've found an existing Transifex config file")
else:
	print ("I've not found an existing Transifex config file.  Be a doll and do a tx init, first, please.")
	exit()


potpath = "pot/"
import string
f = open(".tx/config",'w')
f.writelines("[main]\nhost = " + host +"\nlang_map = aln:aln-AL, ar:ar-SA, ast:ast-ES, as:as-IN, bal:bal-PK, bg:bg-BG, bn:bn-BD, bn_IN:bn-IN, bs:bs-BA, ca:ca-ES, cs:cs-CZ, da:da-DK, de_CH:de-CH, de:de-DE, el:el-GR, en_GB:en-GB, es:es-ES, et:et-EE, eu:eu-ES, fa:fa-IR, fi:fi-FI, fr:fr-FR, gl:gl-ES, gu:gu-IN, he:he-IL, hi:hi-IN, hr:hr-HR, hu:hu-HU, id:id-ID, is:is-IS, it:it-IT, ja:ja-JP, kn:kn-IN, ko:ko-KR, lt:lt-LT, lv:lv-LV, mai:mai-IN, ml:ml-IN, mr:mr-IN, ms:ms-MY, nb:nb-NO, nds:nds-DE, nl:nl-NL, nn:nn-NO, or:or-IN, pa:pa-IN, pl:pl-PL, pt_BR:pt-BR, pt:pt-PT, ro:ro-RO, ru:ru-RU, si:si-LK, sk:sk-SK, sl:sl-SI, sq:sq-AL, sr:sr-RS, sr@latin:sr-Latn-RS, sv:sv-SE, ta:ta-IN, te:te-IN, tg:tg-TJ, tr:tr-TR, uk:uk-UA, ur:ur-PK, vi:vi-VN, zh_CN:zh-CN, zh_HK:zh-HK, zh_TW:zh-TW\n")

if os.path.isdir(potpath):
	for dirpath, dirname, filenames in os.walk(potpath):
		for potfile in filenames:
			if potfile.endswith(".pot"):
				pot = os.path.join(dirpath,potfile)
				filename = potfile
				popath = dirpath.replace("pot","<lang>")
				po = potfile.replace(".pot", ".po")
				po1 = os.path.join(popath,po)
				name = potfile.replace(".pot", "")
			
			form = """\n[%s.%s]\nfile_filter = %s\nsource_file = %s\nsource_lang = %s\ntype = PO\n""" % (guide, name, po1, pot, lang)

			f.writelines(form)
	
else:
	print ("Cannot locate pot directory.  Please make sure there is a pot/ directory.")
	exit()
print ("Transifex config file written.  Please verify output in .tx/config.")
exit()
