#!/usr/bin/perl 

# Kyle Davenport kdd@quickening.zapto.org
#
# requires: openldap-clients
# expects: ldap entries in standard netscape addressbook format, eg,
#     cn=newNetscapeAddress, ou=Addressbook, o=quickening,c=US
#     givenname=givenname
#     sn=sirname
#     objectclass=top
#     objectclass=person
#     objectclass=organizationalPerson
#     facsimiletelephonenumber=facsimiletelephonenumber
#     mail=mail
#     homephone=homephone
#     telephonenumber=telephonenumber
#     cellphone=cellphone
#     birthday=birthday
#     pagerphone=pagerphone
#     streetaddress=streetaddress
#     l=location
#     postalcode=postalcode
#     st=state
#     countryname=countryname
#     nickname=nickname
#     description=description
#     homeurl=homeurl
#     title=title
#     xmozillanickname=xmozillanickname
#     xmozillaanyphone=xmozillaanyphone
#     xmozillausehtmlmail=xmozillausehtmlmail
#
#  
# This script will dump an ldap addressbook into a format for the Sharp SL-5x00's addressbook.
# Copy standardout to qtopiadesktop's directory = ~/.palmtopcenter/addressbook/addressbook.xml
#  on the SL-5x00 and run qtopia.  It will reformat and add unique tags to the addressbook. 
# Then just sync with the zaurus.  Alternately, just the contacts can be inserted into an
#  existing addressbook.xml file.
# >>>  Backup your old addressbook first !
#
# Zaurus addressbook expects entries like this:
#<Contact FirstName="FirstName" MiddleName="MiddleName" LastName="LastName" FileAs="LastName, FirstName MiddleName" JobTitle="JobTitle" Department="Dept" Company="Company" BusinessPhone="WorkPhone" BusinessFax="WorkFax" BusinessMobile="WorkMobile" DefaultEmail="email@domain" Emails="email@domain" HomePhone="HomePhone" HomeFax="HomeFax" HomeMobile="HomeMobile" BusinessStreet="WorkStreet" BusinessCity="WorkCity" BusinessState="WorkState" BusinessZip="WorkZip" BusinessCountry="WorkCountry" BusinessPager="WorkPager" BusinessWebPage="WorkWebPage" HomeStreet="HomeStreet" HomeCity="HomeCity" HomeState="HomeState" HomeZip="HomeZip" HomeCountry="HomeCountry" HomeWebPage="HomeWebPage" Spouse="Spouse" Gender="1" Birthday="Birthday" Anniversary="Anniversary" Nickname="NickName" Notes="Descriptive Note goes here" rid="13" rinfo="1"  Categories="1018282819" Uid="-1020266548" />
#
#	This program is free software; you can redistribute it and/or modify
# 	it under the terms of the GNU General Public License as published by the Free
#	Software Foundation; either version 1, or (at your option) any
#	later version.
#
#    	This program is distributed in the hope that it will be useful,
#    	but WITHOUT ANY WARRANTY; without even the implied warranty of
#    	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See 
#    	the GNU General Public License for more details.

#	Changes
#	v 0.01 inital release
#

use Net::LDAP qw(:all);      

$basename = "o=quickening,c=US";
$hostname = "quickening.zapto.org";
#$filter   = "ou=Addressbook";
$filter   = "ou=ou";
open  DB,"ldapsearch -b \'$basename\' -h \'$hostname\' \'$filter\'|" or die "ouch!";

print STDERR "skipping:\n";
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE Addressbook ><AddressBook>\n";
print "<RIDMax>\n";
print " 13\n";
print "</RIDMax>\n";
print " <Groups>\n";
print " </Groups>\n";
print " <Contacts\n";

while (<DB>){
	chomp;
	if ((/^$/) or eof()){
		print " />\n";
		$fn = "";
	}
	else {
	    s/"/&quot;/g;
	    s/</&lt;/g;
	    s/>/&gt;/g;
	    case: {
		/$basename/	&& do { print "<Contact"; };
		/^givenname=/ 	&& do { s/givenname=//	; print " FirstName=\"$_\""; $fn=$_ 	; last case; }; 
		/^sn=/		&& do { s/sn=//		; print " LastName=\"$_\" FileAs=\"$_, $fn\""; last case; };
		/^facsimiletelephonenumber/ && do { s/facsimiletelephonenumber=//; print " HomeFax=\"$_\""; last case; };
		/^mail=/	&& do { s/mail=//	; print " DefaultEmail=\"$_\" Emails=\"$_\""; last case; };
		/^homephone=/	&& do { s/homephone=//	; print " HomePhone=\"$_\""		; last case; };
		/^telephonenumber/ && do { s/telephonenumber=//; print " BusinessPhone=\"$_\""	; last case; };
		/^cellphone=/	&& do { s/cellphone=//	; print " HomeMobile=\"$_\""		; last case; };
		/^pagerphone=/	&& do { s/pagerphone=// ; print " BusinessPager=\"$_\""		; last case; };
		/^streetaddress/ && do { s/streetaddress=//; print " HomeStreet=\"$_\""		; last case; };
		/^l=/		&& do { s/l=//		; print " HomeCity=\"$_\""		; last case; };
		/^postalcode=/	&& do { s/postalcode=// ; print " HomeZip=\"$_\""		; last case; };
		/^st=/		&& do { s/st=//		; print " HomeState=\"$_\""		; last case; };
		/^countryname=/	&& do { s/countryname=//; print " HomeCountry=\"$_\""		; last case; };
		/^nickname=/	&& do { s/nickname=//	; print " Nickname=\"$_\""		; last case; };
		/^o=/		&& do { s/o=//		; print " Conmpany=\"$_\""		; last case; };
		/^description=/	&& do { s/description=//; print " Notes=\"$_\""			; last case; };
		/^homeurl=/	&& do { s/homeurl=//	; print " HomeWebPage=\"$_\""		; last case; };
		/^birthday=/	&& do { s/birthday=//	; print " Birthday=\"$_\""		; last case; };
		/^title=/	&& do { s/title=//	; print " Title=\"$_\"";
					( /Mrs/ ) ? $sex = 2 : $sex = 1; print " Gender=\"$sex\"";last case; };
		print STDERR "$_\n";
	    }
	}
};

print " </Contacts>\n";
print "</AddressBook>\n";
