#!/bin/bash

given_ip="$1"
if [ -z "$given_ip" ]; then
	echo "This example script needs an IP as first argument" >&2
	exit 1
fi

ucr_output=$(ssh "$given_ip" "if test -x /usr/sbin/ucr; then /usr/sbin/ucr shell server/role domainname nameserver1; else exit 1; fi")
rc=$?
if [ "$rc" != 0 ]; then
	echo "No UCS system found on $given_ip" >&2
	exit 1
fi

while read line; do
	case "$line" in
		hostname=*)
			ucs_hostname="${line##hostname=}"
			;;
		domainname=*)
			ucs_domainname="${line##domainname=}"
			;;
		nameserver1=*)
			ucs_nameserver1="${line##nameserver1=}"
			;;
		server_role=*)
			ucs_server_role="${line##server_role=}"
			;;
		*)
			echo "Ignoring unexpected UCR output: $line" >&2
			;;
	esac
done <<<"$ucr_output"

if [ "$ucs_server_role" != domaincontroller_master ]; then
	echo "No UCS Master found on $given_ip" >&2
	exit 1
fi

if [ -z "$ucs_domainname" ]; then
	echo "Could not determine domainname of UCS domain" >&2
	exit 1
fi

if [ -z "$ucs_nameserver1" ]; then
	echo "Could not determine primary nameserver of UCS domain" >&2
	exit 1
fi

discover_ucs_master_from_ip() {
	systemd_resolved_is_running() {
		resolved_pid=$(pgrep -f systemd-resolved)
		if [ -z "$resolved_pid" ]; then
			return 1
		fi
	}

	configure_nameserver_in_systemd_resolved() {
		local nameserver
		nameserver="$1"
		if [ -z "$nameserver" ]; then
			echo "Internal error: configure_nameserver_in_systemd_resolved needs a nameserver IP as argument" >&2
			return 1
		fi

		if ! systemd_resolved_is_running; then
			echo "Confused: 127.0.0.53 is configured as nameserver, but systemd-resolved is not running." >&2
			return 1
		fi

		if ! [ -f /etc/systemd/resolved.conf ]; then
			echo "Confused: 127.0.0.53 is configured as nameserver, but /etc/systemd/resolved.conf is missing." >&2
			return 1
		fi

		resolved_conf_DNS=$(grep ^DNS /etc/systemd/resolved.conf)
		if [ -z "$resolved_conf_DNS" ]; then
			echo "DNS=$nameserver" >> /etc/systemd/resolved.conf
			systemctl restart systemd-resolved
		else
			echo "Found DNS config in /etc/systemd/resolved.conf, not adding $nameserver" >&2
		fi
	}

	discover_domaincontroller_master() {
		local domainname
		domainname="$1"
		if [ -z "$domainname" ]; then
			echo "Internal error: domaincontroller_master_is_resolvable needs a domainname as argument" >&2
			return 1
		fi

		local srv_record
		srv_record=$(dig "_domaincontroller_master._tcp.$domainname" SRV +short | grep -v "^;")
		if [ -z "$srv_record" ]; then
			return 1
		fi
		ucs_master_fqdn=$(awk '{print $4}' <<<"$srv_record")
		if [ -z "ucs_master_fqdn" ]; then
			return 1
		fi
		echo "$ucs_master_fqdn"
	}

	configure_dns_resolver() {
		resolv_nameserver=$(sed -n "s/^nameserver\s*//p" < /etc/resolv.conf)
		if [ "$resolv_nameserver" = "127.0.0.53" ]; then
			# 127.0.0.53 is the systemd-resolved stub resolver.
			configure_nameserver_in_systemd_resolved "$ucs_nameserver1" || exit $?
			ucs_master_fqdn=$(discover_domaincontroller_master "$ucs_domainname")
			if [ -z "$ucs_master_fqdn" ]; then
				echo "Error: Cannot resolve SRV record _domaincontroller_master._tcp.$ucs_domainname" >&2
				return 1
			fi
			echo "$ucs_master_fqdn"
		fi
	}

	configure_dns_resolver || return $?
}

ucs_master_fqdn=$(discover_ucs_master_from_ip) || exit 1
echo "FQDN: $ucs_master_fqdn"

configure_domainname_for_this_host() {
	local domainname
	domainname="$1"
	if [ -z "$domainname" ]; then
		echo "Internal error: domaincontroller_master_is_resolvable needs a domainname as argument" >&2
		return 1
	fi

	hostname=$(hostname)
	ip=$(sed -n "s/^\([:.0-9a-f]*\)\s*$hostname$/\1/p" < /etc/hosts)
	if [ -n "$ip" ]; then
		sed -i "s/^\([:.0-9a-f]*\)\(\s*\)$hostname$/\1\2$hostname.$domainname $hostname/" /etc/hosts
	fi
}

if [ -z "$(dnsdomainname)" ]; then
	echo "Configuring domainname for this host: $ucs_domainname"
	configure_domainname_for_this_host "$ucs_domainname"
fi
