#! /bin/bash
#
# Univention Bind
#  join script
#
# SPDX-FileCopyrightText: 2001-2026 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only

## joinscript api: bindpwdfile

VERSION=3
. /usr/share/univention-join/joinscripthelper.lib
joinscript_init

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

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

wait_for_dns () {
	if [ "$server_role" != "domaincontroller_master" ]
	then
		for ((i=0; i<600; i++))
		do
			echo "wait for named ?"
			if ! host -W 1 "$ldap_master" 2>&1 | grep -Fq "timed out"
			then
				break
			fi
		done
	fi
}

if test -z "$ldap_server_ip" -a -z "$ldap_server_name"; then
	exit 1
fi


if [ $JS_LAST_EXECUTED_VERSION = 0 ]; then
	wait_for_dns
	#search for existing zone

	IP="$(get_default_ip_address)"
	NETMASK="$(get_default_netmask)"

	zone="$(univention-directory-manager dns/forward_zone list "$@" --filter zone=$domainname | grep ^DN:)"

	if [ -z "$zone" ]; then
		ERRMSG="Failed creating the DNS zone $domainname."
		exec_log_failure /usr/share/univention-admin-tools/univention-dnsedit "$@" --ignore-exists "$domainname" add zone "root@$domainname." 1 28800 7200 604800 10800 "$hostname.$domainname."
		if [ $? != 0 ]; then exit 1; fi
	fi

	wait_for_dns
	ERRMSG="Failed adding the DNS host record $hostname ($IP) to zone $domainname."
	exec_log_failure /usr/share/univention-admin-tools/univention-dnsedit "$@" --ignore-exists "$domainname" add a "$hostname" "$IP" || die

	reversezone=$(univention-ipcalc6 --netmask "$NETMASK" --ip "$IP" --output reverse --calcdns)
	if [ -z "$reversezone" ]; then exit 1; fi
	ptr=$(univention-ipcalc6 --netmask "$NETMASK" --ip "$IP" --output pointer --calcdns)
	if [ -z "$ptr" ]; then exit 1; fi

	ERRMSG="Failed creating the DNS reverse zone $reversezone."
	exec_log_failure /usr/share/univention-admin-tools/univention-dnsedit "$@" --ignore-exists --reverse "$reversezone" add zone "root@$domainname." 1 28800 7200 604800 10800 "$hostname.$domainname." || die

	ERRMSG="Failed adding the DNS ptr record $ptr ($hostname.$domainname) to zone $reversezone."
	exec_log_failure /usr/share/univention-admin-tools/univention-dnsedit "$@" --ignore-exists --reverse "$reversezone" add ptr "$ptr" "$hostname.$domainname." || die

	univention-config-registry set dns/master/address=127.0.0.1
	# touch config files just in case the listener module bind.py has not created them yet
	touch /etc/bind/univention.conf /etc/bind/univention.conf.proxy
	# start the bind service
	systemctl restart named.service

	# Append local server as DNS server to the zones, except if disabled by UCR
	if ! is_ucr_false dns/nameserver/registration/forward_zone; then
		zone="$(udm dns/forward_zone list "$@" --filter "(&(zone=$domainname)(!(nameserver=${hostname}.${domainname}.)))" | sed -ne 's|DN: ||p')"
		if [ -n "$zone" ]; then
			udm dns/forward_zone modify "$@" --dn "$zone" --append nameserver="${hostname}.${domainname}."
		fi
	fi
	if ! is_ucr_false dns/nameserver/registration/reverse_zone; then
		zone="$(udm dns/reverse_zone list "$@" --filter "(&(subnet=$reversezone)(!(nameserver=${hostname}.${domainname}.)))" | sed -ne 's|DN: ||p')"
		if [ -n "$zone" ]; then
			udm dns/reverse_zone modify "$@" --dn "$zone" --append nameserver="${hostname}.${domainname}."
		fi
	fi
fi

joinscript_save_current_version

exit 0
