#!/bin/bash
#
# Copyright 2013-2017 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/>.

. /usr/share/univention-lib/all.sh
. /usr/share/ucs-school-lib/base.sh

if [ -z "$1" -o "$1" = "-h" -o "$1" = "--help" ] ; then
   echo "syntax: $(basename "$0") [--help] [--dn <oudn>|--name <ou>|--all] [--binddn <dn> --bindpwd <password>]"
   exit 0
fi

eval "$(ucr shell)"

if [ "$1" = "--dn" ] ; then
	oudnlist="$2"
	shift 2 || exit 2
elif [ "$1" = "--name" ] ; then
	oudnlist="$(univention-ldapsearch "(&(objectClass=ucsschoolOrganizationalUnit)(ou=$2))" | ldapsearch-wrapper | sed -nre 's/^dn: //p' | head -n1)"
	shift 2 || exit 2
elif [ "$1" = "--all" ] ; then
	oudnlist=$(servers_school_ous)
	shift 1 || exit 2
else
	echo 'Please specifiy argument "--dn <ou_dn>" or "--name <ou_name>" or "--all"'
	exit 1
fi

if ! is_ucr_true "ucsschool/import/generate/policy/ucc/settings" ; then
	echo "$(basename $0): creation of an UCR policy for UCC settings has been disabled"
	echo "via UCR variable ucsschool/import/generate/policy/ucc/settings."
	exit 0
fi

# set default squid port if unset
[ -z "$squid_httpport" ] && squid_httpport=3128

# Update all OUs this host is responsible for

IFS="
"
for oudn in $oudnlist ; do
	echo "Updating $oudn ..."

    # create policy if missing
    univention-directory-manager policies/registry create "$@" \
     	--ignore_exists \
     	--position "cn=policies,${oudn}" \
     	--set "name=ou-default-ucr-policy"

    # add UCC settings to policy
    univention-directory-manager policies/registry modify "$@" \
     	--dn "cn=ou-default-ucr-policy,cn=policies,${oudn}" \
     	--append "registry=ucc/mount/cifshome/server ${hostname}.${domainname}" \
     	--append "registry=ucc/proxy/http http://${hostname}.${domainname}:${squid_httpport}" \
     	--append "registry=ucc/cups/server ${hostname}.${domainname}" \
     	--append "registry=ucc/italc/key/sambasource \\\\${hostname}\\netlogon" \
     	--append "registry=ucc/italc/key/filename italc-key_${hostname}.pub"

	# Bug #40705 - Set timeserver for UCC clients to school DC slave (in multiserver env)
    univention-directory-manager policies/registry modify "$@" \
     	--dn "cn=ou-default-ucr-policy,cn=policies,${oudn}" \
     	--append "registry=ucc/timeserver ${hostname}.${domainname}" | grep -v "Duplicated variables not allowed"

    # link to OU
    univention-directory-manager container/ou modify "$@" \
     	--dn "$oudn" \
     	--policy-reference "cn=ou-default-ucr-policy,cn=policies,${oudn}"
done

exit 0
