#!/usr/bin/perl -w
###########################################################
#
# z2m Make mail aliases for mutt from Zaurus addressbook
# 
# (c) 2002-11-02 by Ralf Hüls <Ralf.Huels@ePost.de>
#
# This program is a dirty hack, use it at your own risk ;-)
# Use and distribution of this program is permitted under
# the terms of the "Artistic License":
# http://www.opensource.org/licenses/artistic-license.php
#
# If you modify this program to create something useful or
# interesting, I'd like to hear about it.
#
# Ralf Hüls <Ralf.Huels@ePost.de>
# http://www.teleute.ping.de/
# DSA PGP key: E131C5A4
# RSA PGP key: 4931A04F
#
###########################################################
#
# Input file name. The default assumes that you synced
# your Zaurus using Qtopiadesktop. 
#
my $filename="$ENV{HOME}/.palmtopcenter/addressbook/addressbook.xml";
#
###########################################################
#
# Output file name. Include this into your .muttrc
#
my $outfile="$ENV{HOME}/muttalias_z";
#
###########################################################
###########################################################
###########################################################

use strict;
use Encode;

my $file;
my @lines;
my $line;
my $firstname;
my $lastname;
my $handle;
my $mail;


open G,$filename or die("$!: $filename");
$file=join '',<G>;
close G;

$file =~ s/\n//g;
$file=decode("utf-8",$file);
$file =~ s/ü/ue/g;
$file =~ s/ä/ae/g;
$file =~ s/ö/oe/g;
$file =~ s/Ü/Ue/g;
$file =~ s/Ä/Ae/g;
$file =~ s/Ö/Oe/g;
$file =~ s/ß/ss/g;
@lines=split /></,$file;

open F,">$outfile" or die("$!: $outfile");

foreach $line (@lines){
 next unless $line =~ /^Contact/;
 next unless $line =~ /DefaultEmail/;

 $firstname=$line;
 $firstname =~ s/.*FirstName="(.*?)".*/$1/;
 $lastname=$line;
 $lastname =~ s/.*LastName="(.*?)".*/$1/;
 $mail=$line;
 $mail =~ s/.*DefaultEmail="(.*?)".*/$1/;
 $handle=substr($firstname,0,1).$lastname;
 print "alias $handle $firstname $lastname <$mail>\n";
 print F "alias $handle $firstname $lastname <$mail>\n";
}
close F;