#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
#
# Univention Updater
#  read the repository server
#
# Copyright 2004-2014 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 os
import sys
import subprocess
from univention.config_registry import ConfigRegistry, handler_set

def exit(result, message = None):
	"""
	Exit with error message.
	"""
	script = os.path.basename(sys.argv[0])
	if message:
		print '%s: %s' % (script, message)
	sys.exit(result)


def query_policy(update, ldap_hostdn):
	"""
	Retrieve updateServer and version from policy.
	"""
	cmd = (
			'univention_policy_result',
			'-D', ldap_hostdn,
			'-y', '/etc/machine.secret',
			'-s', ldap_hostdn,
			)
	p1 = subprocess.Popen(cmd, stdout=subprocess.PIPE)
	result = p1.communicate()[0]

	if p1.returncode != 0:
		exit(result, "FAIL: failed to execute `univention_policy_result'")

	server = None
	for line in result.split('\n'):
		line = line.strip()
		if line.startswith('univentionRepositoryServer='):
			server = line.split('=', 1)[1].split('"',2)[1]
		elif line.startswith('univentionUpdateVersion='):
			update = line.split('=', 1)[1].split('"',2)[1]
	if server and server.startswith('http://'):
		server = server.replace('http://', '', 1)
	return (server, update)


def main():
	"""
	Set repository server.
	"""
	ucr = ConfigRegistry()
	ucr.load()

	hostdn = ucr.get('ldap/hostdn')
	if not hostdn:
		# can't query policy without host-dn
		exit(0)

	online_server = ucr.get('repository/online/server')
	mirror_server = ucr.get('repository/mirror/server')
	fqdn  = '%(hostname)s.%(domainname)s' % ucr
	update = '%(version/version)s-%(version/patchlevel)s' % ucr

	ucr_variables = []

	new_server, update = query_policy(update, hostdn)
	if ucr.is_true('local/repository'):
		# on a repository server
		if not new_server:
			ucr_variables.append('repository/online/server?%s' % fqdn)
		elif new_server != mirror_server and new_server != fqdn:
			ucr_variables.append('repository/mirror/server=%s' % new_server)
	else:
		# without a local repository
		if new_server and new_server != online_server:
			ucr_variables.append('repository/online/server=%s' % new_server)

	if ucr_variables:
		handler_set(ucr_variables)

if __name__ == '__main__':
	main()
