#!/bin/sh

# This script will update Apple's LDAP database to allow it to work properly with address book applications.
# Author: Martin Twombly
# Created: 2004, April 1
# Modified: 2004, April 28
# Modified: 2004, May 5

# Requires modification to match your server settings. See examples for help.

### School District Example - Server DNS: mail.district.k12.ca.us
## ldapsearch -x -LL -b 'cn=users,dc=district,dc=k12,dc=ca,dc=us' 'cn=*' | grep uidNumber: >> /tmp/tempfile.txt
## Any ldapsearch command should be modified similarly.
#### Need to configure
ldapsearch -x -LL -b 'cn=users,dc=yourdomain,dc=com' 'cn=*' | grep uidNumber: >> /tmp/tempfile.txt

while read line; do

uidNumber=`echo $line | cut -f2 -d " "`
if expr $uidNumber \> 1000 > /dev/null ; then
#### Need to configure
ldapsearch -x -LL -b 'cn=users,dc=yourdomain,dc=com' uidNumber=$uidNumber | grep cn: >> /tmp/tempfile2.txt
#### Need to configure
ldapsearch -x -LL -b 'cn=users,dc=yourdomain,dc=com' uidNumber=$uidNumber | grep uid: >> /tmp/tempfile3.txt
while read line; do
# Determine first and last names. If three spaces exist in cn, then set lastname to the third name.
test=`echo $line | cut -f4 -d " "`
if [ -n "$test" ] ; then
echo "test exists"
lastName=`echo $line | cut -f4 -d " "`
else
lastName=`echo $line | cut -f3 -d " "`
fi
firstName=`echo $line | cut -f2 -d " "`
echo $firstName
echo $lastName

done < /tmp/tempfile2.txt
while read line; do
# Each record contains an apple-generateduid. This test drops that line and only uses the uid: attribute.
test=`echo $line | cut -f1 -d " "`
if [ "$test" != "apple-generateduid:" ] ; then
shortName=`echo $line | cut -f2 -d " "`
echo $shortName
fi
done < /tmp/tempfile3.txt
rm /tmp/tempfile2.txt
rm /tmp/tempfile3.txt
echo $uidNumber
echo $firstName
echo $lastName
echo $shortName
### School District Example - Server DNS: mail.district.k12.ca.us
## echo "dn: uid="$shortName",cn=users,dc=district,dc=k12,dc=ca,dc=us">>/tmp/modfile.txt
#### Need to configure
echo "dn: uid="$shortName",cn=users,dc=yourdomain,dc=com">>/tmp/modfile.txt
echo "changetype: modify">>/tmp/modfile.txt
echo "replace: sn">>/tmp/modfile.txt
echo "sn: "$lastName>>/tmp/modfile.txt
echo "-">>/tmp/modfile.txt
echo "add: mail">>/tmp/modfile.txt
### School District Example - Server DNS: mail.district.k12.ca.us - email address = username@district.k12.ca.us
## echo "mail: "$shortName"@district.k12.ca.us">>/tmp/modfile.txt
#### Need to configure
echo "mail: "$shortName"@yourdomain.com">>/tmp/modfile.txt
echo "-">>/tmp/modfile.txt
echo "add: gn">>/tmp/modfile.txt
echo "gn: "$firstName>>/tmp/modfile.txt
echo "">>/tmp/modfile.txt
### School District Example - Server DNS: mail.district.k12.ca.us
## ldapmodify -D "cn=users,dc=district,dc=k12,dc=ca,dc=us" -f /tmp/modfile.txt -w yourpassword
#### Need to configure !update yourpassword to your password!
ldapmodify -D "cn=users,dc=yourdomain,dc=com" -f /tmp/modfile.txt -w yourpassword
rm /tmp/modfile.txt
fi
done < /tmp/tempfile.txt


#cat modfile.txt
rm /tmp/tempfile.txt