#!/bin/bash
#
# Univention Self Service
#  postinst file for the debian package
#
# SPDX-FileCopyrightText: 2019-2026 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only

#DEBHELPER#

systemctl try-restart univention-directory-listener


if [ "$1" = configure ] && dpkg --compare-versions "$2" lt 8.0.1~; then
    python3 - <<EOF
from univention.management.console.log import MODULE
from univention.management.console.modules.passwordreset.tokendb import TokenDB
from univention.config_registry import ucr


SELFSERVICE_MASTER = ucr.get("self-service/backend-server", ucr.get("ldap/master"))
IS_SELFSERVICE_MASTER = '%(hostname)s.%(domainname)s' % ucr == SELFSERVICE_MASTER

if IS_SELFSERVICE_MASTER:
    db = TokenDB(MODULE)
    try:
        db.migrate_timestamp_to_timestamptz()
    finally:
        db.close_db()
EOF
fi
if [ "$1" = configure ] && dpkg --compare-versions "$2" lt 7.1.0~; then
    if (ucr get self-service/ldap_attributes | grep -qE '(^st,|,st,|st$)' && ! ucr get self-service/ldap_attributes | grep -qE '(^c,|,c,|c$)'); then
        ucr set self-service/ldap_attributes="$(ucr get self-service/ldap_attributes),c"
    fi
fi
if [ "$1" = configure ] && dpkg --compare-versions "$2" lt 5.0.1-19~; then
	/usr/share/univention-self-service/univention-setup-self-service-portal-links
	udm portals/entry modify \
		--dn "cn=self-service,cn=entry,cn=portals,cn=univention,$(ucr get ldap/base)" \
		--set activated=FALSE
fi

exit 0
