#!/usr/bin/env ruby
#encoding: utf-8
Version = '0.00'
if ARGV[0] == '-V'; puts Version; exit end
<<'DOC'

= ldif2vcard - convert Thunderbird ldif to vcard for Evolution

= Synopsis
   ldif2vcard filename[.ldif] [selectkey]

= Description
This little script converts an addressbook exported from |Thunderbird| in
|ldif| format into a vcard file suitable for |Evolution|, with the same
name, but |.vcf| extension. It is not meant to be a general ldif to vcard
converter, as it looks only for fields used in the |Thunderbird|
addressbook and tries to match those with fields used by the |Evolution|
addressbook. 

If a selection key is given, only records matching that key are converted,
and the key is appended to the output file name.

= Requirements
This script uses the |base64| program, which for Ubuntu is available in the
|coreutils| package

= Author
[Wybo Dekker](wybo@dekkerdocumenten.nl)

= Copyright
Released under the [GNU General Public License](www.gnu.org/copyleft/gpl.html)
DOC

require 'erb'
file = ARGV[0] or raise ArgumentError,"Need filename of ldif file\n"
key = ARGV[1]
file = file.sub(/\.ldif$/,'')
outfile = "#{file}#{key}.vcf"
vcard = ERB.new(DATA.read,nil,'%')
inp = open("#{file}.ldif")
out = open(outfile,"w")
n = 0
while rec = inp.gets("\n\n") do
  next if key && rec !~ /#{key}/m
  # These are the identifiers used from the ldif file:
  birthday,birthmonth,birthyear,c,description,facsimiletelephonenumber,
  givenName,homePhone,l,mail,mobile,mozillaCustom4,mozillaHomeCountryName,
  mozillaHomeLocalityName,mozillaHomePostalCode,mozillaHomeState,
  mozillaHomeStreet,mozillaHomeUrl,mozillaSecondEmail,nsAIMid,
  o,ou,pager,postalCode,sn,st,street,telephoneNumber,title = nil
  rec.split(/\n/).each do |line|
    if line =~ /^([[:graph:]]+):: (.*)/
      line = [$1,`echo #{$2} | base64 --decode`].join(': ')
    end
    k,v = line.split(/: /,2)
    next if k == "dn" || k == "objectclass"
    eval %Q{#{k} = %q{#{v}}}
  end
  out.print vcard.result(binding)
  n += 1
end
puts "#{outfile} written, #{n} records#{key ? ' matching '+key : ''}"

__END__
BEGIN:VCARD
VERSION:3.0
REV:2011-02-11T14:00:57Z
UID:pas-id-4D55411900000001
ADR;TYPE=HOME:;;<%= mozillaHomeStreet %>;<%= mozillaHomeLocalityName %>;<%= mozillaHomeState %>;<%= mozillaHomePostalCode %>;<%= mozillaHomeCountryName %>
ADR;TYPE=WORK:;;<%= street %>;<%= l %>;<%= st %>;<%= postalCode %>;<%= c %>
TEL;X-EVOLUTION-UI-SLOT=5;TYPE=HOME,FAX:<%= facsimiletelephonenumber %>
TEL;X-EVOLUTION-UI-SLOT=4;TYPE=CELL:<%= pager %>
TEL;X-EVOLUTION-UI-SLOT=3;TYPE=CELL:<%= mobile %>
TEL;X-EVOLUTION-UI-SLOT=2;TYPE=HOME,VOICE:<%= homePhone %>
TEL;X-EVOLUTION-UI-SLOT=1;TYPE=WORK,VOICE:<%= telephoneNumber %>
EMAIL;X-EVOLUTION-UI-SLOT=2;TYPE=HOME:<%= mozillaSecondEmail %>
EMAIL;X-EVOLUTION-UI-SLOT=1;TYPE=WORK:<%= mail %>
X-MOZILLA-HTML:FALSE
CATEGORIES:<%= nsAIMid %>
N:<%= [mozillaCustom4,sn].join(' ') %>;<%= givenName %>;;;
FN:<%= [givenName,mozillaCustom4,sn].join(' ') %>
NOTE:<%= description %>
BDAY:<%= birthyear ? [birthyear,birthmonth,birthday].join('-') : '' %>
ORG:<%= o %>;<%= ou %>;
TITLE:<%= title %>
URL:<%= mozillaHomeUrl %>
END:VCARD

