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

# PO file status
#
# 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
from util import Output

def usage(code = -1):
    w = sys.stderr.write
    w('Usage: POFileStatus [OPTION] <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')
    w('  -a, --show-all             show all files, even if 100% translated\n')
    w('  -b, --break-path           add file path components to XML output\n')
    w('  -p, --pot-dir=<dir>        pot file dir, checks obsolete/untranslated files\n')
    w('  -x, --pot-exclude=<pat>    exclude pot files that match this pattern\n')
    sys.exit(code)

try:
    opts, args = getopt.getopt(sys.argv[1:], "habd:x:",
                               ["help", "show-all", "break-path",
                                "pot-dir=", "pot-exclude="])
except getopt.GetoptError:
    usage()
showall = 0
breakpath = 0
potexclude = []
potdir = None
for o, a in opts:
    if o in ("-h", "--help"):
        usage(0)
    if o in ("-a", "--show-all"):
        showall = 1
    if o in ("-b", "--break-path"):
        breakpath = 1
    if o in ("-d", "--pot-dir"):
        import os
        potdir = os.path.expandvars(os.path.expanduser(a))
    if o in ("-x", "--pot-exclude"):
        potexclude.append(a)
if len(args) < 1: usage()

def findfiles(dir, ext, exclude):
    def walkfunc(arg, dirname, fnames):
        dirname = dirname[len(os.path.commonprefix((dirname, arg[2]))):]
        for i in fnames:
            if i.endswith(arg[1]):
                name = dirname + '/' + i
                for j in exclude:
                    if name.find(j) >= 0:
                        name = None
                        continue
                if name != None: arg[0][name] = name
    import os
    map = {}
    if os.path.isdir(dir): os.path.walk(dir, walkfunc, (map, ext, dir))
    return map

def attrname(filename, breakpath, attr):
    attr['name'] = filename
    if breakpath:
        cindex = 1
        for component in filename.split('/'):
            attr['name-%d' % cindex] = component
            cindex = cindex + 1

potfiles = None
if potdir != None: potfiles = findfiles(potdir, '.pot', potexclude)

out = Output("po-file-status")
for filename in args:
    po = POFile(filename)
    if po.validate():
        po.obsolete = 0
        if potfiles:
            if not potfiles.has_key(filename + 't'):
                po.obsolete = 1
            else:
                del potfiles[filename + 't']
        if showall or po.fuzzy or po.untranslated or po.obsolete:
            attr = {}
            attrname(filename, breakpath, attr)
            if po.translated: attr['translated'] = str(po.translated)
            if po.fuzzy: attr['fuzzy'] = str(po.fuzzy)
            if po.untranslated: attr['untranslated'] = str(po.untranslated)
            if po.obsolete: attr['obsolete'] = 'true'
            out.opentag('file', attr, close = 1)
    else:
        out.opentag('file', { 'name': filename })
        out.opentag('error', body = po.validateError, close = 1)
        out.closetag()
if potfiles:
    for filename in potfiles.keys():
        attr = {}
        attrname(filename, breakpath, attr)
        attr['not-found'] = 'true'
        out.opentag('file', attr, close = 1)
out.finish()
