#!/bin/bash

# Auto-magic infotab filler v1.0
# Andrina Kelly, July 2005

# Pass args at execution in the following manner
# ./info_filler [ldap_admin] [ldap_password] [ldap server] [path/to/csv_file]

# READING IN FROM COMMA DELIM FILE (CSV) FOR OTHER FIELDS
# Set up comma delim file in the following order:
# shortname,street,city,state,zip,country,work tel,mobile tel,fax,pager,
# mail,mail2,mail3,aim,jabber,msn,icq,yahoo,url,blog
# make sure all fields have some content, otherwise import will fail
# for that user.

# fill in your own server details
DC_PART1="example"
DC_PART2="com"
# Add more DC's if required here and on lines 63, 64, 87 & 155 to correspond
# DC_PART3=""

# Variables retrieved from args passed at execution
USERNAME="$1"
PASSWORD="$2"
SERVER="$3"
DELIM_FILE="$4"

USERS=`cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1}' > /tmp/users`

cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $2}' > /tmp/street
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $3}' > /tmp/city
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $4}' > /tmp/state
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $5}' > /tmp/zip
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $6}' > /tmp/country
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $7}' > /tmp/work
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $8}' > /tmp/mobile
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $9}' > /tmp/fax
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $10}' > /tmp/pager
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $11}' > /tmp/mail
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $12}' > /tmp/mail2
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $13}' > /tmp/mail3
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $14}' > /tmp/aim
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $15}' > /tmp/jabber
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $16}' > /tmp/msn
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $17}' > /tmp/icq
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $18}' > /tmp/yahoo
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $19}' > /tmp/url
cat $DELIM_FILE | awk 'BEGIN {FS = ","} {print $1, $20}' > /tmp/blog

cat /tmp/users | while read line
do

# FIRST AND LAST NAME PORTION OF SCRIPT

# Take list of users, find their first and last name
# from the original entry in the "Basic" tab of WGM
# For each user take the first entry in the "Name" field
# of the "Basic" tab and set that as the first name, take
# second and third entry as second name - this deals with
# double-barreled last names, also removes any numeric
# characters before filling in info fields

FIRST=`ldapsearch -x -b "dc=$DC_PART1,dc=$DC_PART2" uid=${line} cn | grep cn: | sed -e 's/cn: //g' | sed -e 's/[0-9]//g'| awk '{print $1}'`
LAST=`ldapsearch -x -b "dc=$DC_PART1,dc=$DC_PART2" uid=${line} cn | grep cn: | sed -e 's/cn: //g' | sed -e 's/[0-9]//g' | awk '{print $2 $3}'`
STREET=`cat /tmp/street | grep -w ${line} | sed -e "s/${line}\ //g"`
CITY=`cat /tmp/city | grep -w ${line} | sed -e "s/${line}\ //g"`
STATE=`cat /tmp/state | grep -w ${line} | sed -e "s/${line}\ //g"`
ZIP=`cat /tmp/zip | grep -w ${line} | sed -e "s/${line}\ //g"`
COUNTRY=`cat /tmp/country | grep -w ${line} | sed -e "s/${line}\ //g"`
WORK_TEL=`cat /tmp/work | grep -w ${line} | sed -e "s/${line}\ //g"`
MOBILE=`cat /tmp/mobile | grep -w ${line} | sed -e "s/${line}\ //g"`
FAX=`cat /tmp/fax | grep -w ${line} | sed -e "s/${line}\ //g"`
PAGER=`cat /tmp/pager | grep -w ${line} | sed -e "s/${line}\ //g"`
MAIL=`cat /tmp/mail | grep -w ${line} | sed -e "s/${line}\ //g"`
MAIL2=`cat /tmp/mail2 | grep -w ${line} | sed -e "s/${line}\ //g"`
MAIL3=`cat /tmp/mail3 | grep -w ${line} | sed -e "s/${line}\ //g"`
AIM=`cat /tmp/aim | grep -w ${line} | sed -e "s/${line}\ //g"`
JABBER=`cat /tmp/jabber | grep -w ${line} | sed -e "s/${line}\ //g"`
MSN=`cat /tmp/msn | grep -w ${line} | sed -e "s/${line}\ //g"`
ICQ=`cat /tmp/icq | grep -w ${line} | sed -e "s/${line}\ //g"`
YAHOO=`cat /tmp/yahoo | grep -w ${line} | sed -e "s/${line}\ //g"`
URL=`cat /tmp/url | grep -w ${line} | sed -e "s/${line}\ //g"`
BLOG=`cat /tmp/blog | grep -w ${line} | sed -e "s/${line}\ //g"`

# echo $STREET
cat << EOF >> /tmp/users.ldif
dn: uid=$line,cn=Users,dc=$DC_PART1,dc=$DC_PART2
changetype: modify
replace: givenName
givenName: $FIRST
-
replace: sn
sn: $LAST
-
replace: street
street: $STREET
-
replace: l
l: $CITY
-
replace: st
st: $STATE
-
replace: postalCode
postalCode: $ZIP
-
replace: c
c: $COUNTRY
-
replace: telephoneNumber
telephoneNumber: $WORK_TEL
-
replace: mobile
mobile: $MOBILE
-
replace: facsimileTelephoneNumber
facsimileTelephoneNumber: $FAX
-
replace: pager
pager: $PAGER
-
replace: mail
mail: $MAIL
-
add: mail
mail: $MAIL2
-
add: mail
mail: $MAIL3
-
replace: apple-imhandle
apple-imhandle: AIM:$AIM
-
add: apple-imhandle
apple-imhandle: JABBER:$JABBER
-
add: apple-imhandle
apple-imhandle: MSN:$MSN
-
add: apple-imhandle
apple-imhandle: ICQ:$ICQ
-
add: apple-imhandle
apple-imhandle: YAHOO:$YAHOO
-
replace: labeledURI
labeledURI: $URL
-
replace: apple-webloguri
apple-webloguri: $BLOG

EOF
done

ldapmodify -H ldap://$SERVER -c -x -D uid=$USERNAME,cn=users,dc=$DC_PART1,dc=$DC_PART2 -w $PASSWORD -f /tmp/users.ldif

# rm to get rid of temp files created in script - comment out line 178 to
# keep a copy of your ldif file as a log

rm -f /tmp/users
rm -f /tmp/street
rm -f /tmp/city
rm -f /tmp/state
rm -f /tmp/zip
rm -f /tmp/country
rm -f /tmp/work
rm -f /tmp/mobile
rm -f /tmp/fax
rm -f /tmp/pager
rm -f /tmp/mail
rm -f /tmp/mail2
rm -f /tmp/mail3
rm -f /tmp/aim
rm -f /tmp/jabber
rm -f /tmp/msn
rm -f /tmp/icq
rm -f /tmp/yahoo
rm -f /tmp/url
rm -f /tmp/blog
rm -f /tmp/users.ldif

exit 0