#!/usr/bin/perl
# ++ 0.002 2021-10-14 17:00:14 743a41d0e2c94c3de0e852851c5473620718f1ec
our $VERSION = "0.002";

# Modified version of https://github.com/qris/thunderbird-ldif

use strict;
use warnings;

use File::Basename;
use File::Mork;
use Data::Dumper;

unless (@ARGV and $ARGV[0]) {
	die "Usage: $0 <mab-file>\n";
}

my ( $name, $path, $ext ) =      fileparse( $ARGV[0], qr/\.[^.]*/ );
my $output_ldif = "$path$name.ldif";
open my $LDIF, '>', $output_ldif or die "$output_ldif:$!";

my $mork = File::Mork->new($ARGV[0], verbose => 1) 
    || die $File::Mork::ERROR."\n";

warn "$0 version: $VERSION\n";

# -- Print LDIF version string
print $LDIF "version: 1\n";

my $record_count = 0;

foreach my $entry ($mork->entries) {
  next if $entry->{'ListName'};  # Ignore distribution lists
  $record_count++;
  my @dn;
  push @dn, "cn=$entry->{'DisplayName'}" if $entry->{'DisplayName'};
  push @dn, "mail=$entry->{'PrimaryEmail'}" if $entry->{'PrimaryEmail'};
  die "unrecognised entry: ".Dumper($entry) unless @dn;
  print $LDIF "\ndn: ".join(",", @dn)."\n";
  print $LDIF <<EOF;
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
objectclass: mozillaAbPersonAlpha
EOF
    
  my $fields = {
    DisplayName => 'cn',
    PrimaryEmail => 'mail',
    FirstName => 'givenName',
    LastName => 'sn',
    LastModifiedDate => 'modifytimestamp',
    HomeAddress => 'mozillaHomeStreet',
    HomeAddress2 => 'mozillaHomeStreet2',
    HomeCity => 'mozillaHomeLocalityName',
    HomeState => 'mozillaHomeState',
    HomeZipCode => 'mozillaHomePostalCode',
    HomeCountry => 'mozillaHomeCountryName',
    NickName => 'mozillaNickname',
    SecondEmail => 'mozillaSecondEmail',
    PreferMailFormat => 'mozillaUseHtmlMail',
    Custom1 => 'mozillaCustom1',
    Custom2 => 'mozillaCustom2',
    Custom3 => 'mozillaCustom3',
    Custom4 => 'mozillaCustom4',
    Company => 'company',
    _AimScreenName => 'nsAIMid',
    WorkPhone => 'telephoneNumber',
    FaxNumber => 'facsimileTelephoneNumber',
    CellularNumber => 'mobile',
  };

  my %valuable_fields;
  foreach my $fieldname (keys %$entry)
  {
    if ($entry->{$fieldname})
    {
      $valuable_fields{$fieldname} = $entry->{$fieldname};
    }
  }

  # delete $valuable_fields{RecordKey};
  # delete $valuable_fields{ID};
  # delete $valuable_fields{LowercasePrimaryEmail};
  # delete $valuable_fields{PopularityIndex};
  # delete $valuable_fields{DbRowID};
  # delete $valuable_fields{PhotoURI};
  # delete $valuable_fields{PhotoType};
  # delete $valuable_fields{AllowRemoteContent};

  foreach my $fieldname (keys %$fields) {
    if ($valuable_fields{$fieldname}) {
      my $ldif_name = $fields->{$fieldname};
      print $LDIF "$ldif_name: ".$valuable_fields{$fieldname}."\n";
    }
    delete $valuable_fields{$fieldname};
  }
  
  # if (keys %valuable_fields) {
  #   warn "unused fields in entry:  \n";
  #   print "$_ " for keys %valuable_fields;
  #   print "\n";
  # }
}

close $LDIF;
print "$output_ldif written: $record_count entries\n";
