#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Univention Admin Diary
#  CLI Tool adding diary entry into Rsyslog to be added to the DB - eventually
#
# Like what you see? Join us!
# https://www.univention.com/about-us/careers/vacancies/
#
# Copyright 2019-2022 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/>.
#

from __future__ import print_function
from random import randint
from argparse import ArgumentParser, RawTextHelpFormatter

from univention.admindiary.events import DiaryEvent
from univention.admindiary.client import write_event, add_comment


def flip():
	return randint(0, 1)


def write_comment(context):
	add_comment('Lorem ipsum', context, username='Administrator')


def write_join(username):
	hostname = 'member-%s' % randint(1000, 9999)
	event = DiaryEvent.get('JOIN_STARTED')
	context = write_event(event, {'hostname': hostname}, username)
	if flip():
		event = DiaryEvent.get('JOIN_FINISHED_SUCCESS')
		return write_event(event, {'hostname': hostname}, username, context)
	else:
		scripts = randint(0, 2)
		while scripts:
			scripts -= 1
			event = DiaryEvent.get('JOIN_SCRIPT_FAILED')
			write_event(event, {'joinscript': '%d%s.inst' % (randint(10, 90), 'univention-some-script')}, username, context)
		event = DiaryEvent.get('JOIN_FINISHED_FAILURE')
		return write_event(event, {'hostname': hostname}, username, context)


def write_updates(username):
	hostname = 'member-%s' % randint(1000, 9999)
	event = DiaryEvent.get('UPDATE_STARTED')
	context = write_event(event, {'hostname': hostname}, username)
	if flip():
		event = DiaryEvent.get('UPDATE_FINISHED_SUCCESS')
		return write_event(event, {'hostname': hostname, 'version': 'UCS 4.4-0 errata%s' % randint(100, 500)}, username, context)
	else:
		event = DiaryEvent.get('UPDATE_FINISHED_FAILURE')
		return write_event(event, {'hostname': hostname}, username, context)


def write_password(username):
	hostname = 'member-%s' % randint(1000, 9999)
	if flip():
		event = DiaryEvent.get('SERVER_PASSWORD_CHANGED')
		return write_event(event, {'hostname': hostname}, username)
	else:
		event = DiaryEvent.get('SERVER_PASSWORD_CHANGED_FAILED')
		return write_event(event, {'hostname': hostname}, username)


def write_some_events(value, username, f):
	num_runs, num_comments = value.split('+')
	num_runs = int(num_runs)
	num_comments = int(num_comments)
	print(f.__name__, '-', num_runs, 'runs with', num_comments, 'comments each')
	while num_runs:
		num_runs -= 1
		context = f(username)
		this_num_comments = num_comments
		while this_num_comments:
			this_num_comments -= 1
			write_comment(context)


def main():
	usage = '%(prog)s [options]'
	description = 'Test the client library'
	epilog = '''Examples:
	%(prog)s --updates=10+5 --join=1+25 --join=10+0
	'''
	parser = ArgumentParser(usage=usage, description=description, epilog=epilog, formatter_class=RawTextHelpFormatter)
	parser.add_argument('-u', '--username', help='Username that issues all diary entries')
	parser.add_argument('--join', action='append', help='JOIN_STARTED + JOIN_FINISHED_SUCCESS/JOIN_FINISHED_FAILURE')
	parser.add_argument('--updates', action='append', help='UPDATE_STARTED + UPDATE_FINISHED_SUCCESS/UPDATE_FINISHED_FAILURE')
	parser.add_argument('--password', action='append', help='SERVER_PASSWORD_CHANGED/SERVER_PASSWORD_CHANGED_FAILED')
	args = parser.parse_args()
	if args.join:
		for value in args.join:
			write_some_events(value, args.username, write_join)
	if args.updates:
		for value in args.updates:
			write_some_events(value, args.username, write_updates)
	if args.password:
		for value in args.password:
			write_some_events(value, args.username, write_password)


if __name__ == '__main__':
	main()
