#!/usr/bin/python

import commands
import os.path
import string
import sys

def dirstat (params, dirname, names):
	basedir=params[0]
	domain=params[1]
	langcode=dirname[len(basedir)+1:]
	if string.find(langcode,"/")==-1 and os.path.exists(dirname+os.sep+domain+".po") and langcode!="templates":
		sys.stdout.write (dirname+os.sep+domain+".po ")
		cmdline="msgmerge -q -o \""+dirname+os.sep+"temp28347.po\" \""+dirname+os.sep+domain+".po\" \""+dirname+os.sep+".."+os.sep+"templates"+os.sep+domain+".po\""
		mergeoutput=commands.getoutput(cmdline)
		if mergeoutput=="":
			os.rename (dirname+os.sep+"temp28347.po", dirname+os.sep+domain+".po")
		sys.stdout.write (mergeoutput+"\n")

if len(sys.argv)<=1:
	sys.stdout.write ("ERROR: You must specify a domain name as parameter.\n")
else:
	domain=sys.argv[1]
	print "Mergemodule started for module "+domain
	sys.stdout.write ("Updating all translations for domain "+domain+"\n")
	os.path.walk (os.getcwd(), dirstat, [os.getcwd(), domain])

