#!/usr/bin/env python
# Dots - A braille translation program.
#
# Copyright (C) 2010 Consorcio Fernando de los Rios
#               Author: Fernando Herrera <fherrera@onirica.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 3 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, see <http://www.gnu.org/licenses/>.

import pygtk
pygtk.require('2.0')

import os
from sys import exit
from dots import host_settings

import gettext
gettext.bindtextdomain(host_settings.DOMAIN,host_settings.LOCALE_DIR)
gettext.textdomain(host_settings.DOMAIN)
_ = gettext.gettext


from optparse import OptionParser
from dots.config_builder import ConfigBuilder
from dots.document_builder import document_new 


usage = "usage: %prog [options] <input file>"
parser = OptionParser(usage=usage)

parser.add_option("-o", "--output", dest="output",
                  help=_("write translation to OUTFILE"), metavar="OUTFILE")
parser.add_option("-d", "--use-default-semantics", dest="use_default_semantics",
                  action="store_true", default=True,
                  help=_("include default semantics for the input file type"))
parser.add_option("-a", "--add-semantics", dest="add_semantics", metavar="SEMANTICS", action="store",
                  help=_("add extra semantics files separated by comas"))
parser.add_option("-n", "--network", dest="network",
                  action="store_true", default=True,
                  help=_("access the network to download schemas"))
parser.add_option("-t", "--table", dest="table", metavar="TABLE", action="store",
                  help=_("use TABLE as translation table for literary text"))
parser.add_option("-c", "--cells", type="int", dest="cells",
                  help=_("cells per line"), default=40)
parser.add_option("-l", "--lines", type="int", dest="lines",
                  help=_("lines per page"), default=25)
parser.add_option("-p", "--page-numbers", dest="page_numbers",
                  action="store_true", default=True,
                  help=_("include page number on every page"))
parser.add_option("-x", "--page-position", dest="page_position", metavar="POS", action="store", default="bottom",
                  help=_("set position for page numbers, where POS is top or bottom"))

(options, args) = parser.parse_args()
if len(args) != 1:
	parser.error(_("incorrect number of arguments"))
	exit(1)

if not os.access(args[0], os.R_OK) or not os.path.isfile (args[0]):
	print (_("Cannot open input file ") + args[0])
	exit(1)
	

if options.table is None:
	parser.error(_("a translation table must be specified"))
	exit(1)

if options.page_position is not "top" and not "bottom":
	parser.error(_("page postion can only be top or bottom"))
	exit(1)

if not os.access(options.table, os.R_OK) and not os.access(os.path.join (host_settings.tablesdir, options.table), os.R_OK):
	print (_("Cannot open table ") + options.table)
	print (_("Installed tables are:"))
	for file in os.listdir(host_settings.tablesdir):
		print "\t" + file
	exit(1)

d = document_new (args[0])
config_builder = ConfigBuilder()

if options.use_default_semantics:
	config_builder['xml']['semanticFiles'] = '*,nemeth.sem'

if options.add_semantics is not None:
	config_builder['xml']['semanticFiles'] += options.add_semantics

if options.network:
        config_builder['xml']['internetAccess'] = 'yes'
else:
        config_builder['xml']['internetAccess'] = 'no'

config_builder['translation']['literaryTextTable'] = options.table

config_builder['outputFormat']['cellsPerLine'] = options.cells

if options.page_numbers:
        config_builder['outputFormat']['braillePages'] = 'yes'
else:
        config_builder['outputFormat']['braillePages'] = 'no'

config_builder['outputFormat']['LinesPerPage'] = options.lines
config_builder['outputFormat']['braillePageNumberAt'] =  options.page_position

d.translate (config_builder)

if options.output is not None:
	output_file = options.output
else:
	output_file = os.path.basename (args[0]) + ".brl"

try:
		f = open (output_file, 'w')
except IOError, (errno, strerror):
        print _("Cannot write to %s: %s" % (output_file, strerror))
	exit(1)

f.write(d.get_braille_text ())
f.close()



