#!/bin/bash
#
# Univention Mail Server
#
# SPDX-FileCopyrightText: 2011-2025 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only

. /usr/lib/univention-server/lib/server_password_change/debug.sh

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

if [ "$1" = "prechange" ] ; then
	if /etc/init.d/postfix status | grep -q "is running" ; then
		univention-config-registry set mail/postfix/stoppedbyserverpasswordchange=yes
		echo " ($(date '+%F %T.%N')) stopping postfix due to upcoming server password change" | logger -t "server-password-change"
		/etc/init.d/postfix stop
	fi
fi

if [ "$1" = "postchange" -o "$1" = "nochange" ] ; then
	# update listfilter password
	univention-config-registry commit /etc/listfilter.secret
	# update ldap query definitions
	univention-config-registry commit /etc/postfix/ldap.*
	# start postfix only if it has been stopped by this script
	if [ "$mail_postfix_stoppedbyserverpasswordchange" = "yes" ] ; then
		echo "($(date '+%F %T.%N')) starting postfix after server password change" | logger -t "server-password-change"
		/etc/init.d/postfix start
		univention-config-registry unset mail/postfix/stoppedbyserverpasswordchange
	fi
fi
