#!/usr/bin/python2.7
#
# Remove status file of Univention System Setup
# Stop firefox browser if frontend uses legacy code
#
# Copyright 2016-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/>.

from __future__ import print_function
import univention.config_registry
import psutil
import os
import os.path
import subprocess

PATH_BROWSER_PID = '/var/cache/univention-system-setup/browser.pid'


def finish_system_setup_boot():
	ucr = univention.config_registry.ConfigRegistry()
	ucr.load()

	# make sure that the status file is removed
	# (see also 05_remove_setup_status_file)
	setup_status_file = '/var/www/ucs_setup_process_status.json'
	if os.path.exists(setup_status_file):
		os.remove(setup_status_file)

	# It is possible that the installation is started with a legacy
	# umc system setup frontend (deb version < 9.0.4-33), in that case,
	# a UCRv is used as a flag to indicate this. Firefox then has to be
	# stopped manually

	if ucr.is_true('system/setup/boot/legacyfrontend', False):
		print('Appliance mode: try to shut down the browser')
		try:
			fpid = open(PATH_BROWSER_PID)
			strpid = fpid.readline().strip()
			pid = int(strpid)
			p = psutil.Process(pid)
			p.kill()
		except IOError:
			print('WARN: cannot open browser PID file: %s' % PATH_BROWSER_PID)
		except ValueError:
			print('ERROR: browser PID is not a number: "%s"' % strpid)
		except psutil.NoSuchProcess:
			print('ERROR: cannot kill process with PID: %s' % pid)

		# support psutil 0.9 and 2.0
		def pname(p):
			return p.name() if callable(p.name) else p.name

		def pcmdline(p):
			return p.cmdline() if callable(p.cmdline) else p.cmdline

		# Maybe the system-setup CMD tool was started
		for p in psutil.process_iter():
			if pname(p) == 'python2.7' and '/usr/share/univention-system-setup/univention-system-setup' in pcmdline(p):
				p.kill()

		# When the debian installer is not running, stop plymouth
		if not ucr.is_true('system/setup/boot/installer'):
			subprocess.call(["service", "plymouth", "stop"])


if __name__ == "__main__":
	finish_system_setup_boot()
