#!/usr/bin/perl -w

=head1 NAME

dfvcard-pilotaddress - parse a vCard file and prepare a QSF XML file

=head1 VERSION

Version 0.0.1

=head1 SYNOPSIS

 dfvcard-pilotaddress FILENAME
 dfvcard-pilotaddress -h|--help|--version

=head1 DESCRIPTION

dfvcard-pilotaddress parses a vCard file and prepares a
QSF XML file suitable for pilot-qof.

Specify '-' as the filename to parse STDIN.

=head1 OBJECTS

pilot_expenses is part of pilot-qof.
Can also be used with gpe-expenses - compatibility with the
default SQLite gpe-expenses backend is pending.

L<http://qof.sourceforge.net/>

L<http://pilot-qof.sourceforge.net/>

L<http://gpe-expenses.sourceforge.net/>

=head1 AUTHOR

Neil Williams, C<< <codehelp at debian.org> >>

=head1 BUGS

Please report bugs via the pilot-qof package, either
in the Debian BTS or via SourceForge trackers.

=head1 COPYRIGHT & LICENSE

  Copyright 2007 Neil Williams.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.

=cut

use strict;
use Text::vCard::Addressbook;
use XML::Simple;
use XML::Writer;
use Class::Struct;
use Date::Parse;
use Date::Format;
use XML::QOFQSF qw(QSFParse QSFWrite);
use Number::Format qw(:subs);
use Math::BigInt;
use Data::Random qw(:all);

my $our_version = "0.0.1";
sub usageversion {
    print(STDERR <<END)
dfvcard-pilotaddress version $our_version

Usage:
 dfvcard-pilotaddress FILENAME
 dfvcard-pilotaddress -h|--help|--version

Options:
 -h|--help:           print this usage message and exit
 --version:           print this usage message and exit

dfvcard-pilotaddress parses a vCard file to prepare a
QSF XML file containing pilot_address records for pilot-qof.

Specify '-' as the filename to parse STDIN.

END
        || die "$0: failed to write usage: $!\n";
exit 0;
}

my $stdin = "false";
while( @ARGV ) {
    $_= shift( @ARGV );
    if (/^-$/) {
 		$stdin = "true";
    	next;
    }
    last if m/^--$/;
    if (!/^-/) {
        unshift(@ARGV,$_);
        last;
    }
    if (/^(-h|--help|--version)$/) {
        &usageversion();
        exit( 0 );
    }
}
my $file = "";
$file = "-" if ($stdin eq "true");
$file = $ARGV[0] if ($stdin eq "false");
&usageversion if (!$file);
die "Cannot find $file. Please specify a vCard file.\n" 
	if ((! -f $file) && ($file ne '-'));
	
my $address_book = Text::vCard::Addressbook->new({
        'source_file' => "$file",
});

my @contacts=();
my %obj;

foreach my $vcard ($address_book->vcards()) {
	my $names = $vcard->get({ 'node_type' => 'name' });
	my $c = new Contact;
	foreach my $name (@{$names}){
		$c->entryLastname($name->family());
		$c->entryFirstname($name->given());
	}
	my $phones = $vcard->get('tel');
	$c->entryPhone1($$phones[0]->value());
	$c->entryPhone2($$phones[1]->value());
	$c->entryPhone3($$phones[2]->value());
	$c->entryPhone4($$phones[3]->value());
	my $email = $vcard->get('email');
	$c->entryPhone5($$email[0]->value());
	my $addresses = $vcard->get({ 'node_type' => 'addresses' });
	foreach my $address (@{$addresses}) {
		$c->entryAddress($address->street());
		$c->entryCity($address->city());
		$c->entryZip($address->post_code());
		$c->entryCountry($address->country());
	}
	push @contacts, $c;
}

$obj{'pilot_address'} = \@contacts;

QSFWrite(\%obj);
