#!/usr/bin/perl
#
# Usage:
#   newt2vcf.pl < input-file
# 
# If your file has 'mac-os'-like line termination (CR) then you will
# need to convert it to unix-like ones (LF) before handing to newt2vcf:
#   tr '\015' '\012' < input-file | newt2vcf.pl 
#
# This script will avoid overwriting any existing VCF files in
# the current directory, so you should delete or move the results
# of previous invocations.
# 
# Author: James R Grinter <jrg@watching.org>
# Date:   Sun Mar 24 2002
#

@fieldnames = ("Last Name","First Name","Middle Name","Ms./Mr.","Title","Company","Address 1 - Street (Line 1)","Address 1 - Street (Line 2)","Address 1 - City","Address 1 - State","Address 1 - Zip Code","Address 1 - Country","Address 2 - Street (Line 1)","Address 2 - Street (Line 2)","Address 2 - City","Address 2 - State","Address 2 - Zip Code","Address 2 - Country","E-Mail Address 1","E-Mail Address 2","Phone - Home","Phone - Work","Phone - Fax","Phone - Cellular","Phone - Other 1","Phone - Other 2","Phone - Other 3","Pager","Notes","Folder","Birthday","Anniversary","Affiliate 1","Affiliate 2","Affiliate 3","Affiliate 4");

while (<>) {
	s/^\"(.*)\"/$1/;
	@line = split( /\",\"/ );
	foreach $fn ( 0..$#fieldnames ) {
		$line{$fieldnames[$fn]} = $line[$fn];
	}
	if ($line{"Last Name"} eq "") {
		# company?
		&writeco();
	}
	else {
		&writeper();
	}
}

sub writeco {
	&startvcf($line{'Company'});
	print VCF "fn:$line{'Company'}\n";
	print VCF "n:$line{'Company'};;;;\n";
        print VCF "adr;type=work,pref:$line{'Address 1 - Street (Line 1)'};$line{'Address 1 - Street (Line 2)'};$line{'Address 1 - City'};$line{'Address 1 - State'};$line{'Address 1 - Zip Code'};$line{'Address 1 - Country'}\n";
	$line{'Phone - Work'} =~ s/<[^>]+>\s*//;
	neprint("tel;type=work",$line{'Phone - Work'});
	neprint("tel;type=work",$line{'Phone - Other 1'});
	neprint("tel;type=work",$line{'Phone - Other 2'});
	neprint("tel;type=work",$line{'Phone - Other 3'});
	neprint("email;type=internet",$line{'E-Mail Address 1'});
	neprint("email;type=internet",$line{'E-Mail Address 2'});
	neprint("note",$line{'Notes'});
	&endvcf();
}

sub writeper {
	&startvcf("$line{'First Name'}_$line{'Last Name'}");
	print VCF "fn:$line{'First Name'} $line{'Last Name'}\n";
	print VCF "n:$line{'Last Name'};$line{'First Name'};$line{'Middle Name'};$line{'Ms./Mr.'};$line{'Title'}\n";
	&neprint("bday",$line{'Birthday'});

	if ( $line{'Phone - Home'} eq "" ) {
		# likely company address only
		if ( $line{'Address 1 - Street (Line 1)'} ne "" ) {
			print VCF "adr;type=work:$line{'Address 1 - Street (Line 1)'};$line{'Address 1 - Street (Line 2)'};$line{'Address 1 - City'};$line{'Address 1 - State'};$line{'Address 1 - Zip Code'};$line{'Address 1 - Country'}\n";
		}
	}
	else {
		print VCF "adr;type=home:$line{'Address 1 - Street (Line 1)'};$line{'Address 1 - Street (Line 2)'};$line{'Address 1 - City'};$line{'Address 1 - State'};$line{'Address 1 - Zip Code'};$line{'Address 1 - Country'}\n";
	}
	if ( $line{'Address 2 - Street (Line 1)'} ne "" ) {
		print VCF "adr;type=work:$line{'Address 2 - Street (Line 1)'};$line{'Address 2 - Street (Line 2)'};$line{'Address 2 - City'};$line{'Address 2 - State'};$line{'Address 2 - Zip Code'};$line{'Address 2 - Country'}\n";
	}
	$line{'Phone - Home'} =~ s/<[^>]+>\s*//;
	$line{'Phone - Work'} =~ s/<[^>]+>\s*//;
	$line{'Phone - Cellular'} =~ s/<[^>]+>\s*//;
	$line{'Phone - Fax'} =~ s/<[^>]+>\s*//;
	neprint("tel;type=home",$line{'Phone - Home'});
	neprint("tel;type=work",$line{'Phone - Work'});
	neprint("tel;type=cell",$line{'Phone - Cellular'});
	neprint("tel;type=fax",$line{'Phone - Fax'});
	neprint("tel;type=home",$line{'Phone - Other 1'});
	neprint("tel;type=home",$line{'Phone - Other 2'});
	neprint("tel;type=home",$line{'Phone - Other 3'});
	neprint("tel;type=pager",$line{'Pager'});
	neprint("email;type=internet",$line{'E-Mail Address 1'});
	neprint("email;type=internet",$line{'E-Mail Address 2'});
	neprint("title",$line{'Title'},$line{'Company'});
	neprint("note",$line{'Notes'});
	&endvcf();
}

sub startvcf {
	my( $name ) = @_;
	$count="";
	while ( -f "${name}${count}.vcf" ) {
		$count++;
	}
	open( VCF, ">${name}${count}.vcf") ;
	print VCF "begin:vcard\nversion:3.0\n";
}

sub endvcf {
	print VCF "end:vcard\n";
	close( VCF );
}

sub neprint {
	my ( $label, @values ) = @_;

	$sep = "";
	foreach $value ( @values ) {
		if ( $value ne "" ) {
			if ( $sep eq "" ) {
				print VCF "$label:";
			}
			print VCF "${sep}$value";
			$sep = ", ";
		}
	}
	if ( $sep ne "" ) {
		print VCF "\n";
	}
}

