#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Univention App Center
#  univention-app
#
# Copyright 2015-2021 Univention GmbH
#
# https://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
# <https://www.gnu.org/licenses/>.
#

import sys
from argparse import ArgumentParser
import locale

# do it immediately before univention.appcenter.utils loads Translation
try:
	locale.setlocale(locale.LC_ALL, "")
except locale.Error:
	pass

from univention.appcenter.actions import all_actions
from univention.appcenter.exceptions import Abort
from univention.appcenter.log import _reverse_umc_module_logger, log_to_logfile, log_to_stream


def add_action(subparsers, action):
	description = action.__doc__ or action.help
	help = action.help or action.__doc__
	subparser = subparsers.add_parser(action.get_action_name(), description=description, help=help)
	action.setup_parser(subparser)
	subparser.set_defaults(func=action.call_with_namespace)
	return subparser


def main():
	usage = '%(prog)s'
	description = '%(prog)s is a program to manage Apps from the Univention App Center'
	parser = ArgumentParser(usage=usage, description=description)
	subparsers = parser.add_subparsers(description='type %(prog)s <action> --help for further help and possible arguments', metavar='action')

	try:
		log_to_logfile()
	except IOError:
		pass
	log_to_stream()
	_reverse_umc_module_logger()

	for action_name, action in all_actions():
		add_action(subparsers, action())

	args = parser.parse_args()

	try:
		try:
			action = args.func
		except AttributeError:
			parser.print_help()
			ret = 0
		else:
			ret = action(args)
	except Abort:
		ret = 10
	if ret is True:
		ret = 0
	elif ret is False:
		ret = 1
	elif not isinstance(ret, int):
		ret = 0
	sys.exit(ret)


if __name__ == '__main__':
	main()
