#!/usr/bin/env python

from subprocess import Popen, PIPE, call
import os

def pout(p):
	c = p.communicate()
	l = []
	for x in c:
		for y in x.split('\n'):
			l.append(y)
	return l

def printl(l):
	print lstr(l)

def lstr(l):
	tl = []
	for x in l:
		if x != '':
			tl.append(x)
	return '\n'.join(tl)	

def mkp(c):
	return mkpl(c.split(' '))

def mkpl(c):
	return Popen(c, stdout=PIPE,stderr=PIPE)

def ocall(c):
	return pout(mkp(c))

def lcall(c):
	return pout(mkpl(c))

def tail1(c):
	o = ""
	for x in c:
		if x != '':
			o = x
	return o

def fstrspl(fullst, searchst, splitc):
	l = fullst.split(splitc)
	for x in l:
		if searchst in x:
			return x
	return ""

qtx11d = os.getcwd()+'/qt4-x11'
call([qtx11d+'/bin/lupdate', 'unetbootin.pro'])
call([qtx11d+'/bin/lrelease','unetbootin.pro'])
potgendate = lstr(lcall(['date','+%Y-%m-%d %R%z']))
potext = ocall(qtx11d+'/bin/lconvert -i unetbootin.ts -if ts -of po')
qtversion = fstrspl(tail1(ocall(qtx11d+'/bin/qmake --version')), '.', ' ')
lineno = 0
for x in potext:
	if 'Header' in x:
		break
	lineno += 1
ntext = [ \
'#, fuzzy', \
'msgid ""', \
'msgstr ""', \
'"Project-Id-Version: PACKAGE VERSION\\n"', \
'"Report-Msgid-Bugs-To: \\n"', \
'"POT-Creation-Date: '+potgendate+'\\n"', \
'"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"', \
'"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"', \
'"Language-Team: LANGUAGE <LL@li.org>\\n"', \
'"MIME-Version: 1.0\\n"', \
'"Content-Type: text/plain; charset=UTF-8\\n"', \
'"Content-Transfer-Encoding: 8bit\\n"', \
'"X-Generator: lconvert '+qtversion+'\\n"', \
]
i = 0
for x in potext:
	if i <= lineno:
		i += 1
		continue
	ntext.append(x)
f = open('../../po/unetbootin.pot', 'w')
for x in ntext:
	if x != 'QFileInfo::absolutePath: Constructed with empty filename':
		f.write(x+'\n')

