#!/bin/bash
#
# ::copy::
# ::maintainer::
#
CMD=$(basename $0)

export CONFDIR=/etc/kusa
export LANG="C"

usage()
{
	echo "usage: $CMD [ldapoptions]" >&2
	exit 1
}

# (MAIN)

# search admin dn and password
#
admin_dn=
[ -f /etc/ldap.conf ] && {
	admin_dn=$(grep "^rootbinddn[ ,	]" /etc/ldap.conf | sed -e 's/^rootbinddn  *//')
}
[ "X$admin_dn" = "X" ] && {
	[ -f /etc/ldap/ku-rootdn.conf ] && \
		admin_dn=$(grep "^rootdn[ ,	]" /etc/ldap/ku-rootdn.conf | \
			sed -e 's/rootdn[ ,	]*//')
}

[ "X$admin_dn" = "X" ] && {
	admin_dn=`kusa-conf ldap.admin` 2>/dev/null
}
[ "X$admin_dn" = "X" ] && {
	echo "unable to retrieve rootdn (rootbinddn, or ldap.admin)" >&2
	exit 1
}


if [ -f /etc/ldap.secret ]
then
	admin_pw="-y /etc/ldap.secret"	# use stored password
else
	if admin_pw=`kusa-conf ldap.admin_password 2>/dev/null`
	then
		admin_pw="-w $admin_pw"
	else
		admin_pw="-W"			# ask for password
	fi
fi

exec ldapsearch -x -D "$admin_dn" $admin_pw "$@"
