#!/usr/bin/perl -w

# -------------------------------------------------
# extract Address content from a Palm T5 pdb file
# and prepare it for inport into Ximian Evolution
#
# Author  : Ch. Bueche
# Version : 0.5
# Date    : 22.8.2005
# -------------------------------------------------

# -------------------------------------------------
# full evolution record examples :
#
#	BEGIN:VCARD
#	VERSION:2.1
#	X-EVOLUTION-FILE-AS:Bueche, Charles
#	FN:Charles Bueche
#	N:Bueche;Charles
#	TEL;WORK;VOICE:011/222.33.44
#	TEL;WORK;FAX:01/12.12.121
#	TEL;HOME:011/2334.34.33
#	TEL;CELL:011/122.11.11
#	EMAIL;INTERNET:user@dom.com
#	EMAIL;INTERNET:user2@dom2.com
#	EMAIL;INTERNET:user3@dom3.com
#	ORG:company
#	UID:pas-id-3F9BDC8500000000
#	END:VCARD
# -------------------------------------------------

use strict;

use Palm::Address;
use Unicode::String qw(latin1 utf8);

my $pdb = new Palm::PDB;
$pdb->Load("AddressDB.pdb");

my @meanings = ("Work", "Home", "Fax", "Other", "E-mail", "Main", "Pager", "Mobile");

my $i = 0;
my $records_with_email = 0;
while (1) {

	my $record = $pdb->{records}[$i];
	if ( defined($record) ) {

		# get all potential e-mail addresses from phone fields
		my @email_addresses = ();
		foreach my $phone ("phone1", "phone2", "phone3", "phone4", "phone5") {
			# check if field is used
			my $field = $record->{fields}{$phone};
			if (defined($field)) {
				# get phone field meaning
				my $meaning_idx = $record->{phoneLabel}{$phone};
				my $meaning = $meanings[$meaning_idx];

				#print "found number : $phone, $field, meaning : $meaning\n";

				if ($meaning eq 'E-mail'){
					push @email_addresses, $field;
				}
			} # end if
		} # end foreach phone
			

		if (scalar @email_addresses) {
			# This entry has at least 1 email address
			$records_with_email++;

			# record header
			print "BEGIN:VCARD\n";
			print "VERSION:2.1\n";

			# Name and Given name

			# get rid of "&" for XML format
			my $name = $record->{fields}{name};
			if (not defined $name) {
				$name = "undef";
			}
			$name =~ s/&/and/g;

			my $firstname = "";
			if ( defined( $record->{fields}{firstName} ) ) {
				$firstname = $record->{fields}{firstName};
				$firstname =~ s/&/and/g;
			}


			# Name and Given name
			print to_utf8("X-EVOLUTION-FILE-AS:$record->{fields}{name}");
			if ( defined( $record->{fields}{firstName} ) ) {
				print ", ";
				print to_utf8("$record->{fields}{firstName}");
			}
			print "\n";

			print "FN:";
			if ( defined( $record->{fields}{firstName} ) ) {
				print to_utf8("$record->{fields}{firstName} ");
			}
			print to_utf8("$record->{fields}{name}\n");
			
			print to_utf8("N:$record->{fields}{name}");
			if ( defined( $record->{fields}{firstName} ) ) {
				print ";";
				print to_utf8("$record->{fields}{firstName}");
			}
			print "\n";

			# company
			my $company = $record->{fields}{company};
			if ( defined($company) ) {
				print to_utf8("ORG:$company\n");
			}
					

			foreach my $emailaddress (@email_addresses) {

				print "EMAIL;INTERNET:$emailaddress\n";

			} # end foreach

			# maybe other email addresses in notes field. This is used at last
			# by SuperNames, but one can imagine a user sticking other addresses
			# there as well. The regex shall be adapted from the Regex book to be
			# more efficient.
			my $notes = $record->{fields}{note};
			if ( ( defined($notes) ) ) {

				my @notes = split /\n/, $notes;
				foreach my $line (@notes) {

					# extract email address
					if ( $line =~ m/.*\W(.+@.+)\W*/ ) {

						my $notesmail = $1;
						print "EMAIL;INTERNET:$notesmail\n";
					}

				} # end foreach

			} # end if

			# get phones
			my @work_phones = ();
			foreach my $phone ("phone1", "phone2", "phone3", "phone4", "phone5") {
				# check if field is used
				my $field = $record->{fields}{$phone};
				if (defined($field)) {
					# get phone field meaning
					my $meaning_idx = $record->{phoneLabel}{$phone};
					my $meaning = $meanings[$meaning_idx];
					if ($meaning eq 'Work'){
						print "TEL;TYPE=WORK;TYPE=VOICE;X-EVOLUTION-UI-SLOT=1:$field\n";
					}
					if ($meaning eq 'Home'){
						print "TEL;TYPE=HOME;TYPE=VOICE;X-EVOLUTION-UI-SLOT=2:$field\n";
					}
					if ($meaning eq 'Mobile'){
						print "TEL;TYPE=CELL;X-EVOLUTION-UI-SLOT=3:$field\n";
					}
					if ($meaning eq 'Fax'){
						print "TEL;TYPE=WORK;TYPE=FAX;X-EVOLUTION-UI-SLOT=4:$field\n";
					}
				} # end if
			} # end foreach phone

			# address
			my $address = '';
			my $street = $record->{fields}{address};
			if ( defined($street) ) {
				$address = $street;
			}
			my $city = $record->{fields}{city};
			if ( defined($city) ) {
				$address .= ";$city;";
			}
			my $zip = $record->{fields}{zipCode};
			if ( defined($zip) ) {
				$address .= ";$zip";
			}
			my $country = $record->{fields}{country};
			if ( defined($country) ) {
				$address .= ";$country";
			}
			if ($address) {
				print to_utf8("ADR;TYPE=HOME:;;$address\n");
			}

			# use the ID in each palm record to generate a record ID
			# the goal is to let the phone correlate existing records
			# instead of duplicating them
			# doesn't work... Evo ignores it and creates its own uid
			#my $id = $record->{id};
			#if ( defined($id) ) {
			#	print 'UID:palm-' . $id . "\n";
			#	#print "UID:$id\n";
			#}

			# record footer
			print "END:VCARD\n\n";

		}    # end if some email in record

		# next record  
		$i++;
	} # end if defined record()
	else {
		last;	# exit while(1) loop
	}

}    # end while

print STDERR "Total records              : $i\n";
print STDERR "Records with email address : $records_with_email\n";

exit 0;


# -------------------------------------------
# subs
# -------------------------------------------

sub to_utf8 {

	# from http://www.infocopter.com/perl/utf-8.htm

	my $tmp = $_[0];
	my $in = latin1($tmp);
	return $in->utf8;
}

#
#
# eof
