#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Copyright 2018-2020 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 sys
import logging
from logging.handlers import TimedRotatingFileHandler
from paramiko.ssh_exception import SSHException, NoValidConnectionsError
from univention.asm.asm_upload import ASMUpload
from univention.asm.utils import get_ucr


LOGFILE_PATH = '/var/log/univention/asm.log'
PASSWORD_FILE_PATH = '/etc/asm.secret'
UCR_USERNAME_KEY = 'asm/username'
UCR_OU_WHITELIST_KEY = 'asm/school_whitelist'
UCR_STORE_ZIP_KEY = 'asm/store_zip'


class ASMCredentialError(Exception):
	pass


def get_config():
	ucr = get_ucr()
	with open(PASSWORD_FILE_PATH, 'r') as passwordFile:
		password = passwordFile.read().strip("\n")
	username = ucr[UCR_USERNAME_KEY]
	whitelist = ucr[UCR_OU_WHITELIST_KEY]
	delete_zip = not ucr.is_true(UCR_STORE_ZIP_KEY)
	if whitelist is not None:
		whitelist = [school.strip(" ") for school in whitelist.split(",") if school]
	if not username:
		raise ASMCredentialError("Please set a username in the app settings")
	if not password:
		raise ASMCredentialError("Please set a password in the app settings")
	return username, password, whitelist, delete_zip


def setup_logging(level_console=logging.INFO, level_file=logging.DEBUG):
	handler_console = logging.StreamHandler()
	handler_console.setLevel(level_console)
	handler_file = TimedRotatingFileHandler(LOGFILE_PATH, when="D", interval=7, backupCount=4)
	handler_file.setLevel(level_file)
	formatter_file = logging.Formatter('%(asctime)s %(levelname)-5s %(module)s.%(funcName)s:%(lineno)d - %(message)s')
	handler_file.setFormatter(formatter_file)
	logger = logging.getLogger('univention.asm')
	logger.setLevel(logging.DEBUG)
	logger.addHandler(handler_console)
	logger.addHandler(handler_file)


def main():
	logger = logging.getLogger('univention.asm.upload_script')
	setup_logging()
	try:
		config = get_config()
	except ASMCredentialError as exc:
		logger.error(exc)
		sys.exit(1)
	asmUpload = ASMUpload(*config)
	try:
		zip_path = asmUpload.upload()
	except (SSHException, NoValidConnectionsError) as exc:
		logger.error("SFTP upload failed: {}".format(exc))
		sys.exit(1)
	if not config[3] and zip_path is not None:
		logger.info("The uploaded zip is stored in: {}".format(zip_path))


if __name__ == '__main__':
	main()
