#!/bin/sh
#
#####
#####  Conversion shell script to convert into ldap format.
#####    Used outlook express to create the lists, then exported them
#####    into a file called add_book.csv.  Then convert them
#####    into ldap format (ldif) and import into the ldap directory.
#####
#####    Make sure you are in the proper directory : /etc/ldap/adm/test
#####
#####    convert : sh ./convert.sh > ldif_input_file.txt
#####      build : ldif2ldbm -i ldif_input_file.txt -f ./slapd.conf 
#####      index : ldif2index -i ldif_input_file.txt -f ./slapd.conf
#####       test : ldbmcat /var/ldap/databases/test/id2entry.dbb | more
#####

#####
#####  Set the record number variable to zero.  Begin a while loop
#####    that will read each entry in the phonebook.  Increment the 
#####    record counter, and use awk to split up the input line.
#####    We will break it into name, email, home phone, and company
#####

#####
#####  Setup organization record
#####
#echo "dn: o=org, c=US"
#echo "o: org"
#echo "objectclass: organization"
#echo ""
sed s///g < add_book.csv > add_book2.csv
cat add_book2.csv |
  while read line
    do 
      #echo "Address Line is : $line"
      #####
      #####  Use the comma for the field seperator
      #####
      echo $line | awk -F, '
        {
           #####
           #####  Open with the dn: section on its own line
           #####
           printf "dn: "
             #####
             #####  Now loop thru the fields in the line, and 
             #####    break into respective ldif formatted attributes
             #####
             #####  1 = First Name
             { printf "cn="$3 ", o=org, c=US \n" }
             { print "cn: "$3  }
             if ( $2 > "" ) { print "sn: "$2 }              ##  2 = Last Name
             if ( $3 > "" ) { }                             ##  3 = Full Name
             if ( $4 > "" ) { print "mail: "$4 }            ##  4 = Email
             if ( $5 > "" ) { print "postalAddress: "$5 }   ##  5 = Bus Street
             if ( $6 > "" ) { print "postalCity: "$6 }      ##  6 = Bus City
             if ( $7 > "" ) { print "postalCode: "$7 }      ##  7 = Bus Zip
             if ( $8 > "" ) { print "postalState: "$8 }     ##  8 = Bus State
             if ( $9 > "" ) { print "postalCountry: "$9 }   ##  9 = Bus Country
             if ( $10 > "" ) { print "postalWebPage: "$10 } ## 10 = Bus Web Page
             if ( $11 > "" ) { print "postalPhone: "$11 }   ## 11 = Bus Phone
             if ( $12 > "" ) { print "postalFax: "$12 }     ## 12 = Bus Fax
             if ( $13 > "" ) { print "postalPager: "$13 }   ## 13 = Pager
             if ( $14 > "" ) { print "Company: "$14 }       ## 14 = Company
             if ( $15 > "" ) { print "Title: "$15 }         ## 15 = Title
           #####
           #####  Print blank line seperator between records
           #####
           print "objectclass: person\n"
           exit
        } '
    done
