#!/bin/bash
#
# MS Office 365 provisioning
#  preinst file for the debian package
#
# Copyright 2016-2025 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/>.

while [ $# -gt 0 ]
do
	case "$1" in
		"--binddn")
			shift
			binddn="$1"
			shift
			;;
		"--bindpwdfile")
			shift
			pwdfile="$1"
			shift
			;;
		"--error-file")
			shift
			errorfile="$1"
			shift
			;;
		*)
			shift
			;;
	esac
done

# Check if univention-saml has the correct version on the DC master
. /usr/share/univention-lib/ldap.sh
username=$(ucs_convertDN2UID "$binddn" -D "$binddn" -y "$pwdfile")

if ! univention-ssh "$pwdfile" "$username"@$(ucr get ldap/master) dpkg --compare-versions \"5.0.4-26\" lt-nl \"\$\(dpkg-query --showformat='\$\{Version\}' --show univention-saml\)\"; then
	echo "Error: The package univention-saml on the DC Master ($(ucr get ldap/master)) must at least be 5.0.4-26 before the app can be updated. Please update the DC Master to at least erratalevel 159." > "$errorfile"
	exit 1
fi

exit 0
