#!/usr/bin/perl -X
use strict;

my $cat;
my $id;
my $type;
my %data;
my %dataTypes;

# Print de codes?
my $codes;
# Print de raw data
my $raw;
# Print de geparste code
my $parse = 1;

open (MYFILE, $ARGV[0]);
while (<MYFILE>) {

  $_ =~ s/[\t\n\r\f]//g;

  if ($_ =~ m/\[.*/) {
    #print $_;
    if ($_ =~ m/\[Phone.*/) {
      print "PHONE\n";
      $cat = "Phone";
    } else {
      $cat = "??";
    }
  } else {
    if ($cat ne "Phone") {next;}
    my @lijn = split(/ = /);

    if ( $lijn[0] =~ m/.*Location.*/ ) {
      $id = $lijn[1];
    } else {

      if ($type) {
        $data{$id}{$type} = $lijn[1];
        $type = "";
      } elsif ($lijn[1]) {
        $type = $lijn[1];
        $dataTypes{$lijn[1]} = $lijn[1];
      }
    }
  }
}

close (MYFILE); 

if ($codes) {
  print "Codes: \n";
  foreach my $tkey (keys %dataTypes) {
    print $tkey . " ";
  }
  print "\n\n\n";
}


if ($raw) {
  foreach my $key (keys %data) {
    print $key;
    foreach my $tkey (keys %dataTypes) {
      print $tkey . " " . $data{$key}{$tkey} . "\n";
    }
  }
}


if ($parse) {
  # print the first line containing the col titles 
  print "Name,Given Name,Additional Name,Family Name,Yomi Name,Given Name Yomi,Additional Name Yomi,Family Name Yomi,Name Prefix,Name Suffix,Initials,Nickname,Short Name,Maiden Name,Birthday,Gender,Location,Billing Information,Directory Server,Mileage,Occupation,Hobby,Sensitivity,Priority,Subject,Notes,Group Membership,E-mail 1 - Type,E-mail 1 - Value,E-mail 2 - Type,E-mail 2 - Value,E-mail 3 - Type,E-mail 3 - Value,E-mail 4 - Type,E-mail 4 - Value,Phone 1 - Type,Phone 1 - Value,Phone 2 - Type,Phone 2 - Value,Phone 3 - Type,Phone 3 - Value,Phone 4 - Type,Phone 4 - Value,Address 1 - Type,Address 1 - Formatted,Address 1 - Street,Address 1 - City,Address 1 - PO Box,Address 1 - Region,Address 1 - Postal Code,Address 1 - Country,Address 1 - Extended Address\n";

  # Parse data
  # Tinker here if all your data isnt in the .csv output
  foreach my $key (keys %data) {
    print "".
      $data{$key}{Name} . "," . #Name,
      $data{$key}{FirstName} . "," . #Given Name,
      "," . #Additional Name,
      $data{$key}{LastName} . "," . #Family Name,
      "," . #Yomi Name,
      "," . #Given Name Yomi,
      "," . #Additional Name Yomi,
      "," . #Family Name Yomi,
      "," . #Name Prefix,
      "," . #Name Suffix,
      "," . #Initials,
      "," . #Nickname,
      "," . #Short Name,
      "," . #Maiden Name,
      "," . #Birthday,
      "," . #Gender,
      "," . #Location,
      "," . #Billing Information,
      "," . #Directory Server,
      "," . #Mileage,
      $data{$key}{Company} . "," . #Occupation,
      "," . #Hobby,
      "," . #Sensitivity,
      "," . #Priority,
      "," . #Subject,
      $data{$key}{Custom1} ;
    print " - " if ($data{$key}{Custom1} && $data{$key}{Note});
    print $data{$key}{Note} . "," . #Notes,
      "," ; #Group Membership,
    print "* " if ($data{$key}{Email});
    print "," . #E-mail 1 - Type,
      $data{$key}{Email} . "," . #E-mail 1 - Value,
      "," . #E-mail 2 - Type,
      "," . #E-mail 2 - Value,
      "," . #E-mail 3 - Type,
      "," . #E-mail 3 - Value,
      "," . #E-mail 4 - Type,
      "," ; #E-mail 4 - Value,
    print "Other" if ($data{$key}{NumberGeneral});
    print "," . #Phone 1 - Type,
      $data{$key}{NumberGeneral} . "," ; #Phone 1 - Value,
    print "Home" if ($data{$key}{NumberHome});
    print "," . #Phone 2 - Type,
      $data{$key}{NumberHome} . "," ; #Phone 2 - Value,
    print "Work" if ($data{$key}{NumberWork});
    print "," . #Phone 3 - Type,
      $data{$key}{NumberWork} . "," ; #Phone 3 - Value,
    print "Mobile" if ($data{$key}{NumberMobile});
    print "," . #Phone 4 - Type,
      $data{$key}{NumberMobile} . "," ; #Phone 4 - Value,
    print "Home" if ($data{$key}{Address});
    print "," . #Address 1 - Type,
      $data{$key}{Address} . "," . #Address 1 - Formatted,
      $data{$key}{Address} . "," . #Address 1 - Street,
      $data{$key}{City} . "," . #Address 1 - City,
      "," . #Address 1 - PO Box,
      "," . #Address 1 - Region,
      $data{$key}{Postal} . "," . #Address 1 - Postal Code,
      $data{$key}{Country} . "," . #Address 1 - Country,
      "," . #Address 1 - Extended Address
      "\n";
  }
}
