#!@PYTHON@
# -*- mode: Python; coding: utf-8; -*-

# PO file fill with existing translations
# 
# Pedro Morais <morais@kde.org>
# José Nuno Pires <jncp@netcabo.pt>
# (c) Copyright 2003, 2004
# Distributable under the terms of the GPL - see COPYING

import sys
import getopt
if not "@prefix@/share/@PACKAGE@" in sys.path:
    sys.path.append("@prefix@/share/@PACKAGE@")
from POFile import POFile

def usage(code = -1):
    w = sys.stderr.write
    w('Usage: POFileFill <OPTION> <TARGET FILE> <FILE>...\n')
    w('\n')
    w('Options:\n')
    w('  -h, --help                 show this help\n')
    sys.exit(code)

try:
    opts, args = getopt.getopt(sys.argv[1:], "h", ["help"])
except getopt.GetoptError:
    usage()
for o, a in opts:
    if o in ("-h", "--help"):
        usage(0)
if len(args) < 2: usage()

map = None

target = POFile(args[0])
if target.parse() == 0:
    sys.stderr.write('error parsing target file %s\n' % target.filename)
    sys.exit(-2)

map = {}
for filename in args[1:]:
    if filename != target.filename:
        po = POFile(filename)
        if po.consistency(map, '') == 0:
            sys.stderr.write('error parsing file %s\n' % filename)

for i in target.data:
    line, message, msgid, msgstr, fuzzy = i
    if (len(msgstr) == 0 or fuzzy) and map.has_key(msgid):
        results = map[msgid]
        if len(results) == 1:
            newmsgstr = results.keys()[0]
            po = POFile(target.filename)
            lines = po.prepare_replace(message)
            if lines != None:
                file = open(target.filename, 'w')
                po.execute_replace(lines, newmsgstr, 1, file)
                file.close()
