#!/bin/bash

eval "$(/usr/sbin/ucr shell)"

set -x
set -e

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

ucs_parseCredentials "$@"

test -n "$binddn" || false
test -n "$bindpwdfile" || false

create_user () {
	local username=$1
	udm users/user create \
		--binddn "$binddn" \
		--bindpwdfile "$bindpwdfile" \
		--position "cn=users,$ldap_base" \
		--set username="$username" \
		--set firstname="$username first" \
		--set lastname="$username last" \
		--set password="univention"
}

change_password () {
	local username="$1"
	local new_password="$2"
	udm users/user modify \
		--binddn "$binddn" \
		--bindpwdfile "$bindpwdfile" \
		--dn "uid=$username,cn=users,$ldap_base" \
		--set password="$new_password"
}

get_json_attr () {
    local json="$1"
    # e.g. ["result"]["welcomeMessage"]
    local attr="$2"
    cat "$json" | python -c "import sys,json;data=json.loads(sys.stdin.read()); print data$attr"
   
}

tin20_login () {
	local username="$1"
	local password="$2"
	local data='{"jsonrpc":"2.0","method":"Tinebase.login","params":{"username":"'$username'","password":"'$password'"}, "id":6}'
	local cookies=$(mktemp)
	local header='Content-Type: application/json'
	local url="https://$hostname.$domainname/tine20/"
	local out=$(mktemp)
	curl -c "$cookies" -o $out \
		-s -H "$header" --data-binary "$data" "$url" || die 110 "Failed connect to tine20"
	local success="$(get_json_attr "$out" '["result"]["success"]')"
	if [ "$success" = "True" ]; then
		echo "login passed"
		return 0
	fi
	echo "login failed"
	return 1
}

active_sync () {
	local username="$1"
	local password="$2"
	local url="https://$hostname.$domainname/tine20/Microsoft-Server-ActiveSync"
	wget --user "$username" --password="$password" "$url"
	return $?
}

webdav_test () {
	local username="$1"
	local password="$2"
	local url="http://$hostname.$domainname/tine20/webdav"
	test -n "$(echo -e "${username}\n${password}\n" | cadaver "$url" | grep 'dav:/tine20/webdav/>')" && return 0
	return 1
}

remove_user () {
	udm users/user remove \
		--binddn "$binddn" \
		--bindpwdfile "$bindpwdfile" \
		--dn "uid=$1,cn=users,$ldap_base"
}

die () {
	echo "$1" >&2
	exit 1
}

if [ ! -e "$(which cadaver)" ]; then
	ucr set repository/online/unmaintained='yes'
	univention-install -y cadaver makepasswd
	ucr set repository/online/unmaintained='no'
fi

username1="tinexyz123"
username2="tinenext"

trap "{ 
	remove_user "$username1"
	remove_user "$username2"
}" EXIT

create_user "$username1"
create_user "$username2"

sleep 10 # wait_for_replication

# standard
tin20_login "$username1" "univention" || die "Login was denied"
tin20_login "$username1" "univention123" && die "Login with wrong password"
active_sync "$username1" "univention" || die "ActiveSync logon failed"
active_sync "$username1" "univention1" && die "ActiveSync logon with wrong password"
webdav_test "$username1" "univention" || die "webdav logon failed"
webdav_test "$username1" "univention2" && die "webdav logon with wrong password"

# password change
tin20_login "$username2" "univention" || die "tine login was denied"
change_password "$username2" "Univention.99ä"

sleep 10 # wait_for_replication
tin20_login "$username2" "Univention.99ä" || die "tine login was denied"
tin20_login "$username2" "univention" && die "tine login with wrong password"

exit 0
