#!/bin/bash

eval "$(ucr shell)"


MY_ACCOUNT_LDIF=$(univention-s4search sAMAccountName="$hostname\$" \
  rIDSetReferences serverReferenceBL)

MY_RID_SET_DN=$(echo "$MY_ACCOUNT_LDIF" | ldapsearch-wrapper| \
  sed -n 's/^rIDSetReferences: //p')

if [ -n "$MY_RID_SET_DN" ]; then
	MY_RID_SET_LDIF=$(univention-s4search -b "$MY_RID_SET_DN" rIDPreviousAllocationPool rIDAllocationPool rIDNextRID)
	rIDPreviousAllocationPool=$(echo "$MY_RID_SET_LDIF" | ldapsearch-wrapper| sed -n 's/^rIDPreviousAllocationPool: //p')
	rIDAllocationPool=$(echo "$MY_RID_SET_LDIF" | ldapsearch-wrapper| sed -n 's/^rIDAllocationPool: //p')
	rIDNextRID=$(echo "$MY_RID_SET_LDIF" | ldapsearch-wrapper| sed -n 's/^rIDNextRID: //p')

	echo "This server already has a RID Set:"
	echo -e "\trIDPreviousAllocationPool:\t$rIDPreviousAllocationPool\t(This is what is currently used locally)"
	echo -e "\trIDAllocationPool:\t\t$rIDAllocationPool\t(This is what the RID Manager allocated for our use)"
	echo -e "\trIDNextRID:\t\t\t$rIDNextRID\t\t(local counter)"

	read -p "Allocate a new one anyway? (yes/NO): "
	if [ "${REPLY^^*}" != 'YES' ]; then
		exit 2
	fi
fi

MY_SERVER_DN=$(echo "$MY_ACCOUNT_LDIF" | ldapsearch-wrapper| \
  sed -n 's/^serverReferenceBL: //p')

MY_NTDS_GUID=$(univention-s4search -b "CN=NTDS Settings,$MY_SERVER_DN" \
  -s base objectGUID | ldapsearch-wrapper| \
  sed -n 's/^objectGUID: //p')


RID_MANAGER_LDIF=$(univention-s4search objectClass=rIDManager fSMORoleOwner)

RID_MASTER=$(echo "$RID_MANAGER_LDIF" | ldapsearch-wrapper | \
  sed -rn 's/^fSMORoleOwner: CN=NTDS Settings,CN=([^,]*).*/\1/p')

RID_MANAGER_DN=$(echo "$RID_MANAGER_LDIF" | ldapsearch-wrapper | \
  sed -n 's/^dn: //p')


DRSUAPI_EXOP_FSMO_RID_ALLOC=$(python -c 'from samba.dcerpc import drsuapi; print drsuapi.DRSUAPI_EXOP_FSMO_RID_ALLOC')

./getncchanges "$RID_MASTER" -UAdministrator \
   --exop="$DRSUAPI_EXOP_FSMO_RID_ALLOC" --dn="$RID_MANAGER_DN" --dest-dsa="$MY_NTDS_GUID"


echo "Waiting 10 seconds for replication"
sleep 10s

MY_ACCOUNT_LDIF=$(univention-s4search sAMAccountName="$hostname\$" \
  rIDSetReferences serverReferenceBL)

MY_RID_SET_DN=$(echo "$MY_ACCOUNT_LDIF" | ldapsearch-wrapper| \
  sed -n 's/^rIDSetReferences: //p')

if [ -n "$MY_RID_SET_DN" ]; then
	MY_RID_SET_LDIF=$(univention-s4search -b "$MY_RID_SET_DN" rIDPreviousAllocationPool rIDAllocationPool rIDNextRID)

	rIDPreviousAllocationPool=$(echo "$MY_RID_SET_LDIF" | ldapsearch-wrapper| sed -n 's/^rIDPreviousAllocationPool: //p')
	rIDAllocationPool=$(echo "$MY_RID_SET_LDIF" | ldapsearch-wrapper| sed -n 's/^rIDAllocationPool: //p')
	rIDNextRID=$(echo "$MY_RID_SET_LDIF" | ldapsearch-wrapper| sed -n 's/^rIDNextRID: //p')

	echo "Current RID Set (after waiting 10 seconds, maybe look on the master..):"
	echo -e "\trIDPreviousAllocationPool:\t$rIDPreviousAllocationPool\t(This is what is currently used locally)"
	echo -e "\trIDAllocationPool:\t\t$rIDAllocationPool\t(This is what the RID Manager allocated for our use)"
	echo -e "\trIDNextRID:\t\t\t$rIDNextRID\t\t(local counter)"
else
	echo "No RID Set replicated after 10 seconds, checking the RID master $RID_MASTER:"
	REMOTE_ACCOUNT_LDIF=$(univention-s4search -H "ldap://$RID_MASTER" sAMAccountName="$hostname\$" \
		rIDSetReferences serverReferenceBL)
	REMOTE_RID_SET_DN=$(echo "$REMOTE_ACCOUNT_LDIF" | ldapsearch-wrapper| \
		sed -n 's/^rIDSetReferences: //p')
	if [ -n "$REMOTE_RID_SET_DN" ]; then
		REMOTE_RID_SET_LDIF=$(univention-s4search -b "$REMOTE_RID_SET_DN" rIDAllocationPool)

		rIDAllocationPool=$(echo "$MY_RID_SET_LDIF" | ldapsearch-wrapper| sed -n 's/^rIDAllocationPool: //p')

		echo "OK, RID master $RID_MASTER allocated this RID Set for us (check replication, seems to be quite slow..):"
		echo -e "\trIDAllocationPool:\t\t$rIDAllocationPool\t(This is what the RID Manager allocated for our use)"
	else
		echo "No RID Set allocated for us by the RID master $RID_MASTER. Check log.samba on $RID_MASTER."
	fi
fi
