#!/usr/bin/bash

udm groups/group create --position cn=groups,$(ucr get ldap/base)  --set name=bar
udm users/user remove --dn uid=foo1,cn=users,dc=ucs,dc=test
for i in $(seq 1 10); do udm users/user remove --dn uid=aaa$i,cn=users,$(ucr get ldap/base); done
sleep 10

function remove_from_group() {
    tail -n0 -f /var/log/univention/connector-ad.log | sed '/sync to ucs:   \[          user\] \[    modify\] uid=aaa3,cn=users,dc=ucs,dc=test/ q'
    echo "modify group before AD sync"
    date
    udm groups/group modify --remove users=uid=foo1,cn=users,dc=ucs,dc=test --dn cn=bar,cn=groups,dc=ucs,dc=test
    sleep 5
    echo "Uhhh scary ghost member :O"
    udm groups/group list --filter cn=bar
}


for i in $(seq 1 10); do
    udm users/user create --position cn=users,$(ucr get ldap/base) --set username=aaa$i --set lastname=aaa$i --set password=univention
done
udm users/user create --position cn=users,$(ucr get ldap/base) --set username=foo1 --set lastname=foo1 --set password=univention

tail -n0 -f /var/log/univention/connector-ad.log | sed '/sync to ucs:   \[          user\] \[    modify\] uid=foo1,cn=users,dc=ucs,dc=test/ q' > /dev/null

echo "sync started"
sleep 8

for i in $(seq 1 10); do
    udm users/user modify --dn="uid=aaa$i,cn=users,$(ucr get ldap/base)" --set firstname=univention2
done
udm groups/group modify --append users=uid=foo1,cn=users,dc=ucs,dc=test --dn cn=bar,cn=groups,dc=ucs,dc=test
remove_from_group &
sleep 2
