#!/usr/bin/perl -w

# $Id: Psion2Zaurus.pl,v 1.6 2002/07/15 12:50:07 muecke Exp $

# Convert the given vCard file to the XML format expected by the Zaurus addressbook app.

# Written & Copyright 2002 Wolfram Saringer w.saringer@bsg.at
# Comes without any warranty. May be freely redistributed, but please keep
# the reference to its original author.

# Print the header of the XML file
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE Addressbook ><AddressBook>\n";
#print "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><!DOCTYPE Addressbook ><AddressBook>\n";
print " <RIDMax>\n";
print "  11\n";
print " </RIDMax>\n";
print " <Groups>\n";
print " </Groups>\n";
print " <Contacts>\n";


# Initialize fields:
$vorname = "";
$nachname = "";
$titel = "";
$telmobilprivat = "";
$telprivat = "";
$emailprivat = "";
$strasseprivat = "";
$plzprivat = "";
$ortprivat = "";
$bundeslandprivat = "";
$firma = "";
$position = "";
$telbuero = "";
$telmobilbuero = "";
$faxbuero = "";
$emailbuero = "";
$web = "";
$strassebuero = "";
$plzbuero = "";
$ortbuero = "";
$bundeslandbuero = "";
$landbuero = "";
$notiz = "";

while (<>)
  {
    chomp;
    s/\r$//; # Remove trailing CR...

    if( m/^BEGIN:VCARD/ )
      {
	# Initialize fields:
	$vorname = "";
	$nachname = "";
	$telmobilprivat = "";
	$telprivat = "";
	$faxprivat = "";
	$emailprivat = "";
	$strasseprivat = "";
	$plzprivat = "";
	$ortprivat = "";
	$bundeslandprivat = "";
	$firma = "";
	$position = "";
	$telbuero = "";
	$telmobilbuero = "";
	$faxbuero = "";
	$emailbuero = "";
	$web = "";
	$strassebuero = "";
	$plzbuero = "";
	$ortbuero = "";
	$bundeslandbuero = "";
	$landbuero = "";
	$notiz = "";
	$emails = "";
      }
    # If this is the end of an entry, write out the current values:
    elsif ( m/^END:VCARD/ )
      {
       $field = "";
       $field = $field . "FirstName=\"" . $vorname . "\" ";
       $field = $field . "LastName=\"" . $nachname . "\" ";
       $field = $field . "FileAs=\"" . $vorname . " " . $nachname . "\" ";
       if($emailbuero)
	 {
	   $field = $field . "DefaultEmail=\"" . $emailbuero . "\" ";
	   $emails = $emailbuero;
	 }
       elsif($emailprivat)
	 {
	   $field = $field . "DefaultEmail=\"" . $emailprivat . "\" ";
	   if($emails)
	     { $emails .= ","; }
	   $emails .= $emailprivat;
	 }

       $field = $field . "Emails=\"" . $emails . "\" ";
       $field = $field . "HomeStreet=\"" . $strasseprivat . "\" ";
       $field = $field . "HomeCity=\"" . $ortprivat . "\" ";
       $field = $field . "HomeState=\"" . $bundeslandprivat . "\" ";
       $field = $field . "HomeZip=\"" . $plzprivat . "\" ";
       $field = $field . "HomePhone=\"" . $telprivat . "\" ";
       $field = $field . "HomeFax=\"" . $faxprivat . "\" ";
       $field = $field . "HomeMobile=\"" . $telmobilprivat . "\" ";
       $field = $field . "Company=\"" . $firma . "\" ";
       $field = $field . "JobTitle=\"" . $position . "\" ";
       $field = $field . "BusinessPhone=\"" . $telbuero . "\" ";
       $field = $field . "BusinessFax=\"" . $telbuero . "\" ";
       $field = $field . "BusinessStreet=\"" . $strassebuero . "\" ";
       $field = $field . "BusinessCity=\"" . $ortbuero . "\" ";
       $field = $field . "BusinessState=\"" . $bundeslandbuero . "\" ";
       $field = $field . "BusinessZip=\"" . $plzbuero . "\" ";
       $field = $field . "BusinessMobile=\"" . $telmobilbuero . "\" ";
       #   $field = $field . "Birthday=\"" . $geburtstag . "\" ";
       $field = $field . "Notes=\"" . $notiz . "\" ";

       # Escape some characters:
       $field =~ s/&/\&amp;/g;
       $field =~ s/</\&lt;/g;
       $field =~ s/>/\&gt;/g;

       # Complete with tags:
       $field = "<Contact " . $field;
       $field = $field . "/>";

       print $field . "\n";
      }
    elsif ( m/^N;/ ) # Name line...
      {
	# split this line:
	m/.*:([^;]*);([^;]*);([^;]*);/;
	# nachname; vorname; 2. vorname; titel; 
	$nachname = $1;
	$vorname = $2;
	$titel = $3;
      }
    elsif ( m/^ADR;HOME;/ ) # home address
      {
	m/:[^;]*;[^;]*;([^;]*);([^;]*);([^;]*);([^;]*);/;
	# Strasse, ort, bundesland, plz
	$strasseprivat = $1;
	$ortprivat = $2;
	$bundeslandprivat = $3;
	$plzprivat = $4;
      }
    elsif ( m/^ADR;WORK;/ ) # home address
      {
	m/.*:[^;]*;[^;]*;([^;]*);([^;]*);([^;]*);([^;]*);/;
	# Strasse, ort, bundesland, plz
	$strassebuero = $1;
	$ortbuero = $2;
	$bundeslandbuero = $3;
	$plzbuero = $4;
      }
    elsif ( m/^TEL;HOME;FAX;/ )
      {
	m/.*:(.*)/;
	$faxprivat = $1;
      }
    elsif ( m/^TEL;HOME;CELL;/ )
      {
	m/.*:(.*)/;
	$telmobilprivat = $1;
      }
    elsif ( m/^TEL;HOME;/ )
      {
	m/.*:(.*)/;
	$telprivat = $1;
      }
    elsif ( m/^TEL;WORK;FAX;/ )
      {
	m/.*:(.*)/;
	$faxbuero = $1;
      }
    elsif ( m/^TEL;WORK;CELL;/ )
      {
	m/.*:(.*)/;
	$telmobilbuero = $1;
      }
    elsif ( m/^TEL;WORK;/ )
      {
	m/.*:(.*)/;
	$telprivat = $1;
      }
    elsif ( m/^ORG;/ )
      {
	m/.*:(.*);/;
	$firma = $1;
      }
    elsif ( m/^EMAIL;INTERNET;WORK;/ )
      {
	m/.*:(.*)/;
	$emailbuero = $1;
      }
    elsif ( m/^EMAIL;INTERNET;HOME;/ )
      {
	m/.*:(.*)/;
	$emailprivat = $1;
      }
    elsif ( m/^NOTE;/ )
      {
	m/.*:(.*)/;
	$notiz = $1;
      }
  }

# Put on the tail of the XML file
print " </Contacts>\n";
print "</AddressBook>\n";
