#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
#
# Univention Configuration Registry
#  build UMC module
#
# Copyright 2011-2014 Univention GmbH
#
# http://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.

import os
import sys

from optparse import OptionParser

import univention.dh_umc as dh_umc
import univention.debhelper as dh_ucs

"""Tool creates .json files for translation using gettext."""


def main():
	# parse all options
	parser = OptionParser(usage='usage: %prog --package <packageName> --outdir <outDir> [options] <jsFile> ...')
	parser.add_option('-p', '--package', action='store',
		dest='package',
		help='Specifies the package name which is needed for the creation of .po files. (Mandatory)'
	)
	parser.add_option('-t', '--type', action='store', type='choice',
		choices=['json', 'mo', 'po', 'core'],
		dest='type',
		default='json',
		help='Type of the final output file; note that "json" and "mo" will both also create .po files [%default]'
	)
	parser.add_option('-o', '--outdir', action='store',
		dest='outdir',
		help='Specifies the output directory where translations from all js files are saved to. (Mandatory)')
	parser.add_option('-l', '--lang', action='append',
		dest='lang',
		help='Specifies the languages that are processed (default: de)')

	(options, args) = parser.parse_args()

# make sure we have javascript files
#	if not len(args):
#		print '\nPlease specify a set of JavaScript files containing translations!\n'
#		sys.exit(1)

	# update the list of languages
	if options.lang and len(options.lang):
		dh_umc.LANGUAGES = options.lang

	# make sure we have enough parameters
	if not options.package:
		print '\nYou need to specify a package-name (--package) as well as a list of JavaScript files to process!\n'
		sys.exit(1)

	# make sure that we have an output file specified
	if not options.outdir:
		print '\nYou need to specify an output directory (--outdir)!\n'
		sys.exit(1)

	# set the po/mo/json file names and the correct function for generating the
	# final output
	po_file = '%s/%%s.po' % options.outdir
	create_final_output = lambda x: None
	if 'json' == options.type:
		# output is json
		create_final_output = dh_umc.create_json_file
	elif 'mo' == options.type:
		# output is mo
		create_final_output = dh_umc.create_mo_file
	elif 'po' != options.type:
		# invalid output type
		print '\nThe output type needs to be one of the following: ".json", ".mo", ".po"!\n'
		sys.exit(1)

	# build translation files
	for lang in dh_umc.LANGUAGES:
		ipo_file = po_file % lang
		if len(args):
			# only re-create po files if javascript files are given
			dh_umc.create_po_file(ipo_file, options.package, args)
		create_final_output(ipo_file)

if __name__ == '__main__':
	main()
