#!/usr/bin/python2
# -*- coding: ISO-8859-1 -*-
# Copyright (C) 2006 Juan David Ibez Palomar <jdavid@itaapy.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

# Import from the Standard Library
from optparse import OptionParser
import os
import sys

# Import from itools
import itools
from itools.resources import get_resource
from itools.handlers.python import Python
from itools.gettext import PO
from itools.xhtml import XHTML
import itools.stl



if __name__ == '__main__':
    # The command line parser
    version = 'itools %s' % itools.__version__
    description = ('Updates the message catalogs (POT and PO files) in the'
                   ' "locale" directory, with the messages found in the'
                   ' source.')
    parser = OptionParser('%prog',
                          version=version, description=description)

    options, args = parser.parse_args()
    if len(args) != 0:
        parser.error('incorrect number of arguments')

    # Initialize message catalog
    po = PO.PO()

    # Process Python files
    print '(1) Processing Python files',
    sys.stdout.flush()
    cmd = 'find -name "*.py" | grep -v "^./build" | grep -v "^./dist" | grep -v "/test_" | grep -v "cms/utils.py"'
    for path in os.popen(cmd).readlines():
        sys.stdout.write('.')
        sys.stdout.flush()
        path = path.strip()
        resource = get_resource(path)
        handler = Python(resource)
        for msgid, line_number in handler.get_messages():
            if len(msgid) > 2:
                po.set_message(msgid, references={path: [line_number]})
    print ' OK'

    # Process XHTML files
    print '(2) Processing XHTML files',
    sys.stdout.flush()
    cmd = 'find -name "*.x*ml.en" | grep -v "^./build" | grep -v "^./dist"'
    for path in os.popen(cmd).readlines():
        sys.stdout.write('.')
        sys.stdout.flush()
        path = path.strip()
        resource = get_resource(path)
        handler = XHTML.Document(resource)
        for msgid, line_number in handler.get_messages():
            if len(msgid) > 1:
                po.set_message(msgid, references={path: [line_number]})
    print ' OK'

    # Update locale.pot
    print '(3) Updating "locale/locale.pot"',
    sys.stdout.flush()
    open('locale/locale.pot', 'w').write(po.to_str())
    print 'OK'

    # Update language files
    print '(4) Updating language files:'
    print '',
    sys.stdout.flush()
    cmd = 'find locale -name "*.po"'
    for path in os.popen(cmd).readlines():
        path = path.strip()
        print path,
        sys.stdout.flush()
        os.system('msgmerge -U -s %s locale/locale.pot' % path)
