#!/usr/bin/perl
#
# pine2mutt-0.1p: convert pine's addressbook to mutt's aliases format
#
# Michael Velten <michael@michnet.de>
# Patched by Mark Stosberg <mark@summersault.com>

die "Usage: $0 pinefile muttfile\n" if (($P,$M) = @ARGV) != 2;
open(P) || die "$P: $!";
open(M, ">$M") || die "$M: $!";
($_ = join("\0", <P>)) =~ s/\n\0\s[\s:]\s//g;
@addr = split "\0";
for (@addr) {
	next if /#DELETED/;
	s/^(.*?\t)(.*?)\t(\(*)(.+?)\)*(\t.+|$)/alias\t$1$4\t\($2\)/;
	my $comment = $5;
	$comment =~ s/^\s*//;
	if ($comment) {
		chomp;
		$_ .= " # $comment\n";
	}

    s/\(.*\)$// if $3 =~ /\(/ || $2 !~ /.+/;
    print M;
}
close(M);
close(P);

