#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Univention Setup
#  role change script
#
# Copyright 2011-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

import glob
import os
import shutil
import sys

from univention.management.console.modules.setup.setup_script import AptScript, _, main


class RoleScript(AptScript):
	name = _('Configuring server role')
	brutal_apt_options = True

	def inner_run(self):
		# preparation install autoremove
		self.steps(3 * 100)
		self.message(_('Preparing package lists...'))

		if not self.wanted_server_role or self.wanted_server_role == self.current_server_role:
			# nothing to do
			return True

		# Copy local files into the local cache
		#  https://forge.univention.org/bugzilla/show_bug.cgi?id=27935
		#  https://forge.univention.org/bugzilla/show_bug.cgi?id=38393
		for filename in glob.glob('/var/cache/univention-system-setup/packages/*.deb'):
			try:
				basename = os.path.basename(filename)
				os.link(filename, os.path.join('/var/cache/apt/archives/', basename))
			except OSError as e:
				try:
					print('Error creating hardlink to %s: %s' % (filename, e))
					shutil.copy(filename, '/var/cache/apt/archives/')
				except (OSError, IOError, shutil.Error) as e:
					print('Error copying %s: %s' % (filename, e))
		self.reopen_cache()

		current_server_role_package = self.get_package_for_role(self.current_server_role) if self.current_server_role else None
		wanted_server_role_package = self.get_package_for_role(self.wanted_server_role)
		if wanted_server_role_package is None:
			return False
		self.finish_task()

		if not self.update():
			return False

		self.ucr.set('server/role', self.wanted_server_role)
		self.ucr.commit()

		if current_server_role_package:
			self.uninstall(current_server_role_package)
		success = self.install(wanted_server_role_package)

		self.finish_task()

		# mark these packages as manually installed
		self.mark_auto(False, 'univention-pam', 'univention-management-console')
		self.reopen_cache()

		# apt-get autoremove
		self.autoremove()

		if self.wanted_server_role == 'memberserver':
			# reinstall listener on Managed Node because the ldap-server package
			# will remove too much:
			# https://forge.univention.org/bugzilla/show_bug.cgi?id=26269
			self.install('univention-directory-listener')

		self.finish_task()

		# Cleanup
		# rm -f /etc/apt/sources.list/05univention-system-setup.list
		# rm -Rf /var/cache/univention-system-setup/packages

		return success


if __name__ == '__main__':
	script = RoleScript()
	sys.exit(int(not main(script)))
