#!/bin/bash
#
# Univention Bind
#
# 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

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

if [ -n "$bind_bindpw" -a "$bind_bindpw" != "/etc/machine.secret" ]; then
	# Skip the passwdord change
	echowithtimestamp "skip named server password change"
	exit 0
fi

if [ "$1" = "postchange" ] ; then
	echowithtimestamp "updating named zone files after server password change"
	# update zone files
	sed -i "s|\!x-bindpw=\(.*\),x-tls|\!x-bindpw=$(cat /etc/machine.secret),x-tls|" /etc/bind/univention.conf.d/*
	# Reload or restart LDAP-BIND (if running)
	if [ "$dns_backend" = "ldap" ]; then
		echowithtimestamp "reload or restart named after server password change"
		systemctl reload-or-try-restart named.service
	fi
fi
