#!/bin/sh
#
# Univention Directory Manager REST
#  join script
#
# SPDX-FileCopyrightText: 2019-2026 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only

## joinscript api: bindpwdfile

VERSION=2

. /usr/share/univention-join/joinscripthelper.lib

joinscript_init

. /usr/share/univention-lib/all.sh

eval "$(ucr shell ldap/base)"

domain_admins_dn="$(umc_udm groups/group list --filter name="$(custom_groupname "Domain Admins")" | sed -ne 's/^DN: //p')"
ucr set \
	directory/manager/rest/authorized-groups/domain-admins?"$domain_admins_dn" \
	directory/manager/rest/authorized-groups/dc-backup?"cn=DC Backup Hosts,cn=groups,$ldap_base" \
	directory/manager/rest/authorized-groups/dc-slaves?"cn=DC Slave Hosts,cn=groups,$ldap_base" || die

if [ $JS_LAST_EXECUTED_VERSION -le 0 ]; then
	service univention-directory-manager-rest restart
fi

ucs_addServiceToLocalhost 'Univention Directory Manager REST' "$@" || die

univention-directory-manager groups/group create "$@" --ignore_exists --position "cn=groups,$ldap_base" --set name="udm-rest-metrics" --set description="Group for access to UDM REST API metrics" || die

joinscript_save_current_version

exit 0
