#!/usr/bin/python2
# -*- coding: ISO-8859-1 -*-
# Copyright (C) 2003-2006 J. David Ibez <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 sys

# Import from itools
import itools
from itools.resources import get_resource
from itools.handlers import get_handler
from itools.xhtml import XHTML
import itools.stl
import itools.gettext



if __name__ == '__main__':
    version = 'itools %s' % itools.__version__
    description = ('Builds a new file from the given source file, but'
                   ' replacing the translatable messages by the'
                   ' translations found in the PO file.')
    parser = OptionParser('%prog <source file> <PO file>',
                          version=version, description=description)

    parser.add_option('-o', '--output',
                      help="The output will be written to the given file,"
                           " instead of printed to the standard output.")

    options, args = parser.parse_args()

    if len(args) != 2:
        parser.error('incorrect number of arguments')

    if options.output is None:
        output = sys.stdout
    else:
        output = open(options.output, 'w')

    try:
        source, po = args
        resource = get_resource(source)
        xhtml = XHTML.Document(resource)
        po = get_handler(po)
        output.write(xhtml.translate(po))
    finally:
        if options.output is not None:
            output.close()
