#!/bin/bash

if [ $# != 2 ]; then
	echo "usage: $0 <testusername> <password-history-length>"
	exit 1
fi

username="$1"
history_length="$2"

eval "$(ucr shell)"

echo "Setting password history length to $history_length"
samba-tool domain passwordsettings set --history-length="$history_length"

# or
# udm settings/sambadomain \
# 	--dn "sambaDomainName=$windows_domain,cn=samba,$ldap_base" \
# 	--set passwordHistory="$history_length"

newpassword="Univention.0"
echo "Creating user $username with password $newpassword"
samba-tool user create "$username" "$newpassword"
password="$newpassword"

## or
# udm users/user create \
# 	--set "username=$username" \
# 	--set lastname=name1 \
# 	--set password="$newpassword" \
# 	--set pwdChangeNextLogin=1

sleep 15

newpassword="Univention.1"
echo "Changing password to $newpassword"
samba-tool user password \
	-U "$username%$password" \
	--newpassword="$newpassword"
password="$newpassword"

sleep 15

newpassword="Univention.2"
echo "Changing password to $newpassword"
samba-tool user password \
	-U "$username%$password" \
	--newpassword="$newpassword"
password="$newpassword"

## now we should have Univention.1 in the history
sleep 15

newpassword="Univention.1"
echo "Changing password back to $newpassword"
samba-tool user password \
	-U "$username%$password" \
	--newpassword="$newpassword"
password="$newpassword"

