#!/usr/bin/python
# -*- mode: Python; coding: utf-8; -*-

# PO file glossary checker
# 
# Pedro Morais <morais@kde.org>
# José Nuno Pires <jncp@netcabo.pt>
# João Neves <joao@silvanaves.org>
# (c) Copyright 2006
# Distributable under the terms of the GPL - see COPYING

import sys
import getopt
if not "/usr/share/gettext-lint" in sys.path:
    sys.path.append("/usr/share/gettext-lint")
from POFile import POFile
from util import Output
from Glossary import Glossary

def usage(code = -1):
    w = sys.stderr.write
    w('Usage: POFileGlossary [OPTION] <GLOSSARY> <FILE>...\n')
    w('\n')
    w('Mandatory arguments to long options are mandatory '
      'for short options too.\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()

glossary = Glossary(args.pop(0))

def checking(args):
    out = Output("po-file-glossary")
    for filename in args:
        po = POFile(filename)
        if po.parse() == 0:
            sys.stderr.write('error parsing file %s\n' % filename)
        else:
            po.parseHeader()
            po.glossary(glossary)
            if len(po.glossaryErrors) > 0:
                out.opentag('file',
                            {'name': filename})
                for l, m, e in po.glossaryErrors:
                    out.opentag('error', {'line': str(l), 'message': str(m)},
                                body = e, close = 1)
                out.closetag()
    out.finish()

checking(args)
