#!/usr/bin/perl

use strict;

=head1 DESCRIPTION
Exports Juno contacts to CSV using the Web Email feature.

This script reads in the HTML as "print"ed from the "print contact"s
feature. Save the resulting HTML page, and feed it to this program.
It will output a CSV file that sites like GMail can import.

Takes one argument, the "printed" HTML page.

=cut

use XML::Twig;

my $t = new XML::Twig( twig_handlers =>
		       { 'td[@class="details"]' => \&td_handler } );

my @contacts = ();

my %all_keys;

$t->parsefile_html(@ARGV[0]);

my @all_keys_order = keys %all_keys;

### begin CSV printout

# CSV headers
foreach my $key (@all_keys_order){
    print '"', $key, '"', ",";
}
print "\n";

# one contact per line
foreach my $contact (@contacts){

    # ordered list of all the contact entries, properly escaped.
    foreach my $key (@all_keys_order){
	my $esc =  $contact->{$key};
	$esc =~ s/\"/\\"/g;
	print '"',$esc, '"';
	print ",";
    }

    print "\n";
}

########################################

sub td_handler{
    my ($t, $elt) = @_;

    my $text = $elt->sprint;
    $text =~ s!<td[^>]+>(.+)</td[^>]*>!$1!xs;

    my %contact_info;

    # this is just how it appears in the source. Hopefully they
    # don't decide to change this.
    while( $text =~ m!\s*([\w ]*) : \s* ([^;<]+) (?:\;|\<br\/\>)!xgs){
	my $key = $1;
	my $value = $2;
	$all_keys{$key} = 1;
	chomp $key;
	chomp $value;
	$value =~ s/\n//g;
	$value =~ s/\s{2,}/ /g;
	$contact_info{$key} = $value;
    }

    push @contacts, \%contact_info;
}


=head1 AUTHOR

©2006 Steve Pomeroy <steve@staticfree.info>
Released under the GNU GPL 2.0
http://staticfree.info/

=cut
