#!/usr/bin/perl
#
# This is an example of using the Text::ParseWords module to convert
# a comma-delimited file to LDIF. Requires Perl 5.005 or higher. Just
# pipe the CSV file to this script and redirect the output to a file:
#
#     cat FILENAME.csv | csv2ldif.pl > FILENAME.ldif
#
# Anthony Greene <agreene@pobox.com>
#

# Load the required module.
use Text::ParseWords;

# Set a default objectclass and suffix.
$objectlass = 'person';
$suffix = 'dc=my organization, dc=com';

# Read lines from STDIN.
while ($line = <STDIN>) {
  @fields = &quotewords(',',0,$line);

  # Set variable values based on the array values.
  # Edit these to match the format of your CSV file.
  $cn    = $fields[0];
  $fname = $fields[1];
  $lname = $fields[2];
  $title = $fields[3];
  $o     = $fields[4];
  $ou    = $fields[5];
  $mail  = $fields[6];
  $phone = $fields[7];
  $fax   = $fields[8];

  # Output the values.
  print "dn: cn=$cn, $suffix\n";
  print "cn: $cn\n";
  print "givenname: $fname\n";
  print "sn: $lname\n";
  print "title: $title\n";
  print "o: $o\n";
  print "ou: $ou\n";
  print "mail: $mail\n";
  print "telephonenumber: $phone\n";
  print "facsimileteletelephonenumber: $fax\n";
  print "objectclass: $objectclass\n";
  print "\n";
}

exit;
