#! /usr/bin/env python

import sys, string, codecs, os

max_length = 78

wrap_before = ['<h1>', '<h2>', '<h3>', '<h4>', '<h5>', '<h6>', '<p>', '<br>', '<br/>', 
	       '<ol>', '<ul>', '<li>', '<table>', '<th>', '<tr>', '<td>', '<center>', 
	       '<blockquote>', '<pre>', '<hr>', '<hr/>']

def splitit( start, message, outfile ):
	if len(start):
		if len(message) + len(start) < max_length and \
			   string.find(message, '\\n') == -1:
			outfile.write('%s"%s"\n' % (start, message.encode('utf-8')))
			return
		outfile.write(start)
		outfile.write(u'""\n')
		index = 0
		mlen = len(message)
		last_brace = 0
		last_space = 0
		last_comma = 0
		while index < mlen:
			if message[index] == r'n' and (index > 0 and message[index-1] == '\\') \
			   and (index < 2 or message[index-2] != '\\'):
				outfile.write('"%s"\n' % message[:index+1].encode('utf-8'))
				message = message[index+1:]
				mlen -= index + 1
				index = 0
				last_brace = 0
				last_space = 0
				last_comma = 0
				continue
			elif message[index] == u'>':
				last_brace = index
			elif message[index] == u' ':
				last_space = index
			elif message[index] == u',':
				last_comma = index
			elif message[index] == u'<':
				for s in wrap_before:
					if index > 0 and message[index:].startswith(s):
						outfile.write('"%s"\n' % message[:index].encode('utf-8'))
						message = message[index:]
						mlen -= index
						index = 0
						last_brace = 0
						last_space = 0
						last_comma = 0
						continue
			if index > max_length:
				if last_brace > 50:
					index = last_brace
					while index < mlen - 1 and message[index+1] == ' ':
						index += 1
				elif last_space != 0:
					index = last_space
				elif last_comma != 0:
					index = last_comma
				else:
					while index > 0 and message[index] == u'\\':
						index = index - 1
				outfile.write('"%s"\n' % message[:index+1].encode('utf-8'))
				message = message[index+1:]
				mlen -= index + 1
				index = 0
				last_brace = 0
				last_space = 0
				last_comma = 0
				continue
			index += 1
		if len(message):
			outfile.write('"%s"\n' % message.encode('utf-8'))
 
for file in sys.argv[1:]:
	# ### TODO KDE4: orig_file should be opened in "rU" mode, so that any end of line becomes \n
	orig_file = open(file, 'r')
	new_file = open(file + ".new", 'w')
	
	last=''
	start=''
	index=0
	line=' '
	while index >= 0: # python 2.1 has no True ;)
		line = orig_file.readline()
		index += 1
		if not line:
			break
		if line == '\n' or line[0] == '#':
			splitit(start, last, new_file)
			start = ''
			last = ''
			new_file.write(line)
			continue
		try:
			line = string.strip(unicode(line, 'utf-8'))
		except UnicodeError:
			print file
		if line[0] == '"' and line[-1:] == '"':
			last += line[1:-1]
			continue
		# new message
		# ### TODO KDE4: support Gettext plurals and Gettext context
		splitit(start, last, new_file)
		if line.startswith("msgid "):
			start = "msgid "
			last = string.lstrip(line[6:-1])[1:]
		elif line.startswith("msgstr "):
			start = "msgstr "
			last = string.lstrip(line[7:-1])[1:]
		else:
			print file, "parsing error in line", index
			new_file.close()
			sys.exit(1)

	splitit(start, last, new_file)
	new_file.close()
	os.rename(file + ".new", file)

# kate:  space-indent off; indent-width 8; replace-tabs off;
