#!/bin/bash
#
# Univention Setup
#  change imapserver for all users
#
# Copyright (C) 2012-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-ox/ox-lib.sh || exit 1
OXADMINMASTERPW="$(cat "$OXMASTERSECRETFILE")"

eval "$(univention-config-registry shell)"

usage () {
   echo "syntax: $(basename "$0") [--help] [--set-domains] <FQDN_of_IMAP_server>"
   exit $1
}

if [ -z "$1" -o "$1" = "-h" -o "$1" = "--help" ] ; then
	usage 0
fi

SET_DOMAINS=""
if [ "$1" = "--set-domains" ] ; then
	SET_DOMAINS="1"
	shift
fi

IMAPSERVER="$1"
if [ -z "$IMAPSERVER" ] ; then
	usage 1
fi

IFS=$'\n'

if [ -n "$SET_DOMAINS" ] ; then
	if [ "$server_role" = "domaincontroller_master" ] || [ "$server_role" = "domaincontroller_backup" ]; then
		binddn="cn=admin,$ldap_base"
		bindpwd="$(< /etc/ldap.secret)"
	else
		binddn=""
		bindpwd=""
		while [ -z "$binddn" ] ; do
			echo -n "Please enter username with administrative rights: "
			read binduser
			binddn="$(univention-ldapsearch "(&(objectClass=person)(uid=$binduser))" dn | ldapsearch-wrapper | ldapsearch-decode64 | sed -ne 's/^dn: //p;T;q')"
		done
		while [ -z "$bindpwd" ] ; do
			echo -n "Please enter password: "
			read -s bindpwd
			echo
		done
		echo
	fi

	for dn in $(univention-directory-manager oxmail/oxdomain list | sed -ne 's/DN: //p') ; do
		echo "$(date): replacing oxImapServer of object $dn with $IMAPSERVER"
		echo "dn: $dn
changetype: modify
replace: oxImapServer
oxImapServer: $IMAPSERVER
" | ldapmodify -x -D "$binddn" -w "$bindpwd"
	done
fi

eval "$(ucr shell ox/context/id)"
CONTEXTNUM="${ox_context_id:=10}"
if [ "$CONTEXTNUM" = "10" ]; then
	CONTEXTADMIN="oxadmin"
else
	CONTEXTADMIN="oxadmin-context${CONTEXTNUM}"
fi

echo "$(date): changing IMAP server of all users to '$IMAPSERVER'"
FILTER='(&(objectClass=inetOrgPerson)(objectClass=oxUserObject)(isOxUser=OK)(!(uid=Administrator))(!(uid=join-backup))(!(uid=join-slave))(!(uid=spam)))'
for uid in $(univention-ldapsearch -LLL "$FILTER" uid | ldapsearch-wrapper | ldapsearch-decode64 | sed -ne 's/^uid: //p') ; do
	ox_retry \
	"$OXADMINPATH/changeuser" -A "$CONTEXTADMIN" -P "$(< "$OXSECRETPATH/context${CONTEXTNUM}.secret")" -c "${CONTEXTNUM}" -u "$uid" --imapserver "$IMAPSERVER"
done

echo "$(date): done"

exit 0
