#!/usr/local/bin/perl
#
###########################################################################
##
##  Program:  convertAddrBook.pl
##
##  Purpose:  Convert a PINE addressbook to the format specified for the
##            MailDrop "Text Addresses" import plug-in.  This script will
##            handle individual names and groups, but will not handle
##            recursive groups (yet).
##
##  Date:     version 1.1  27 Jul 1998
##
##  Author:   Peter Murray, Digital Media Services
##            Case Western Reserve University
##            pem@po.cwru.edu
##
##  Legalities:
##            Copyright 1997 by Case Western Reserve University.
##            All rights reserved.
##
##  Revision History:
##    19 May 1997  pem  Initial Release
##    27 Jul 1998  pem  Updated documentation to show how to import the
##                      output of this program into MailDrop.
##
$addrbookFile = ".addressbook";
$addrbookFile = $ARGV[0] if $ARGV[0] ne '';

open (ADDRBOOK, $addrbookFile) || die "Could not open address book ($addrbookFile): $!\n";
while (<ADDRBOOK>) {
  chomp;
  next if /^#/;
  my($id,$name,$addr) = split(/\t/,$_);
  if ($addr =~ s/^\(//) {
    until ($addr =~ s/\)$//) {
      $_ = <ADDRBOOK>;
      chomp;
      s/^\s+//;
      $addr .= $_;
    }
    $group{$id} = join "\t",$name,$addr;
  } else {
    $person{$id} = join "\t",$name,$addr;
  }
}

foreach $item (sort keys %person) {
  my($name,$addr) = split(/\t/,$person{$item});
  print "\"$name\" <$addr>\n";
}

foreach $item (sort keys %group) {
  my($name,$addr) = split(/\t/,$group{$item});
  print "\"$name\" <GROUP\@GROUP>\n";
  foreach $address (split(/\,/,$addr)) {
    if (defined($person{$address})) {
      my($name,$addr) = split(/\t/,$person{$address});
      print "\"$name\" <$addr>\n";
    } else {
      print "$address\n";
    }
  }
}

=head1 NAME

convertAddrBook.pl - Convert PINE addressbook into MailDrop "Text Addresses" import form

=head1 SYNOPSIS

  convertAddrBook.pl [filename] 

=head1 DESCRIPTION

This script will convert a PINE addressbook file into a format suitable
for importing into MailDrop using the "Text Addresses" filter.  There is 
one possible parameter.

=over 4

=item filename

Location of the addressbook.  .addressbook assumed if no parameter is given.

=back

Output is sent to STDOUT.  Once you have this output, transfer this file
to your Macintosh hard drive and import it into MailDrop:

=over 4

=item

Open the address book.  (Under the 'Mail' menu, select 'Address Book')

=item

Under the 'File' menu, select 'Import...'.

=item

Select 'Text Addresses' next to 'Import Type'

=item

Find the output from the convertAddrBook.pl program using the file browser and select 'Import'.

=back

=head1 EXAMPLES

=over 4

convertAddrBook.pl ~/.addressbook > to-maildrop.txt

=back

=head1 COPYRIGHT

Copyright 1997-8 by Case Western Reserve University.
All rights reserved.

=head1 AUTHOR

 Peter Murray
 Library Systems Manager
 Digital Media Services
 Case Western Reserve University
 pem@po.cwru.edu
 http://www.cwru.edu/home/pem.html

Updates available from http://www.cwru.edu/dms/homes/pem/projects/convertAddrBook.html

=cut
