#!/bin/sh
#
#  Last modified: 30 October 2000
#

FLAGS=

# Create two temporary files.
umask 077
TMPFILE=`mktemp /tmp/mutt.ldap.XXXXXX`
RESULTS=`mktemp /tmp/mutt.ldap.XXXXXX`

if [ -n "$LDAPSERVER" ]; then
	FLAGS="$FLAGS -h $LDAPSERVER"
fi
if [ -n "$LDAPBASEDN" ]; then
	FLAGS="$FLAGS -b $LDAPBASEDN"
fi

# Search.
ldapsearch $FLAGS '(&(objectclass=inetorgperson)(|(cn='"$1"'*)(givenname='"$1"'*)(surname='"$1"'*)(mail='"$1"'*)))' mail cn roomNumber 2> /dev/null >> $TMPFILE
ldapsearch -x $FLAGS '(&(objectclass=inetorgperson)(|(cn='"$1"'*)(givenname='"$1"'*)(surname='"$1"'*)(mail='"$1"'*)))' mail cn roomNumber 2> /dev/null >> $TMPFILE

# Parse.
cat $TMPFILE | awk '
	/^mail:/ {MAIL=substr($0,6)}
	/^cn:/ {NAME=substr($0,4)}
	/^roomNumber:/ {ROOM=substr($0,12)}
	/^$/ { MAIL=gensub("^ *| *$","","g",MAIL) }
	/^$/ { NAME=gensub("^ *| *$","","g",NAME) }
	/^$/ { ROOM=gensub("^ *| *$","","g",ROOM) }
	/^$/ { if(length(NAME) > 0) print MAIL "\t" NAME "\t" ROOM}
	/^$/ { NAME = "" }
' > $RESULTS

# Sort and present results.
sort -u $RESULTS > $TMPFILE
echo Querying ${LDAPSERVER}.... `cat $TMPFILE | wc -l` entries found.
cat $TMPFILE

# Clean up.
rm -f $TMPFILE $RESULTS
