#!/usr/bin/perl

# A little perl script to produce foaf::Person from vCards.
#
# Written by Kjetil Kjernsmo, kjetilk@cpan.org, 2004-10-19
# License: Same terms as Perl itself.

use XML::FOAFKnows::FromvCard;
use Encode;

my $file;
my $seeAlsoUri;
my $myUri;
my $myEmail;
my $privacy;
my $attribute;

foreach my $clp (@ARGV) {
  if ($clp =~ m/^--seeAlso=(\S+)$/) {
    $seeAlsoUri = $1;
  } elsif ($clp =~ m/^--uri=(\S+)$/) {
    $myUri = $1;
  } elsif ($clp =~ m/^--email=(\S+)$/) {
    $myEmail = $1;
  } elsif ($clp =~ m/^--privacy=(\S+)$/) {
    $privacy = $1;
  } elsif ($clp =~ m/^--attribute=(\S+)$/) {
    $attribute = $1;
  } else {
    $file = $clp;
  }
}

unless ($file) {
  print 'Usage: [--seeAlso=URI|--uri=URI|--email=your@email.address|--privacy=(PRIVATE|PUBLIC)|--attribute=VCARDATTRIBUTE] file' . "\n";
  exit(1);
}

open(VCF, "< ". $file) || die "Cannot open $file";
my @data = <VCF>;
close VCF;



my $formatter = XML::FOAFKnows::FromvCard->format(decode_utf8(join('',@data),
							      Encode::FB_XMLCREF),
						  (uri => $myUri,
						   seeAlso => $seeAlsoUri,
						   email => $myEmail,
						   attribute => $attribute,
						   privacy => $privacy));
my $out = $formatter->document('UTF-8');
if ($out =~ m/foaf:knows/) {
  print encode_utf8($out);
} else {
  print STDERR "No vCards produced usable output\n";
}
