#!/usr/bin/perl
# -----------------------------------------------------------------
#
# 'csv2vcard.pl'
#
# Convert a comma-delimited list of waypoints to vCard format.
#
# This script is very ... basic.
# I use this to import geographic locations into the onboard Nav system 
# of my car (some brand of the Volkswagen Group). 
#
# NOTE: If you use csv from Google Docs, you may need to run "dos2unix" first
#
# -----------------------------------------------------------------
# This program is free software; you can redistribute it and/or
# modify it under the terms of the version 2 of the GNU General
# Public License as published by the Free Software Foundation.
#
# 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.
# -----------------------------------------------------------------
#
# Copyright (c) 2021 Joerg Hau <joerg.hau@schweizerschrauber.ch>.
#
# Revision History:
#   2021-04-11, JHa, first working version, based on csv2gpx.pl
#   2021-07-27, JHa, now skips # lines and rounds to 5 digits
# -----------------------------------------------------------------

use strict;
use warnings;

# input file is delimited with TAB and comma (!):
#   name TAB address TAB phone TAB latitude, longitude TAB symbol TAB sequence
#   Some Place	Via SomeRoad 5, 12345 SomeCity	021-345.678	47.1234, 6.5678	Flag	12
#
# output is:
# BEGIN:VCARD
# VERSION:3.0
# GEO:47.1234;6.5678
# FN;CHARSET=UTF-8:12-Some Place
# TEL;TYPE=WORK,VOICE:021-345.678
# END:VCARD
#
# NOTE: The other fields (such as the street address) are not used, 
# since the Nav system translates the GEO tags into Street Adresses anyway.
# They are here since I use the same file format for the script csv2gpx.pl ;-)
#

my $template="BEGIN:VCARD
VERSION:3.0
FN;CHARSET=UTF-8:SEQ-NAME
TEL;TYPE=WORK,VOICE:FON
GEO:LAT;LON
END:VCARD\n";


# -----------------------------------------------------------------
# Print usage mode
# -----------------------------------------------------------------
sub usage
{
print <<EOF
$0 - Convert a list of waypoints to vCard format 
for use with VW/Audi/Skoda/Seat onboard Nav systems.

Copyright (c) 2021 Joerg Hau <joerg.hau(at)schweizerschrauber.ch>.

This program is free software; you can redistribute it and-or
modify it under the terms of version 2 of the GNU General Public
License as published by the Free Software Foundation.

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.

Usage: $0 listfile

EOF
}

# -----------------------------------------------------------------
# testing
# -----------------------------------------------------------------
my $numArgs = $#ARGV + 1;
unless ($numArgs==1) {
    usage;
    die "Error: Please provide input filename.\n"
    }


# -----------------------------------------------------------------
# read file
# -----------------------------------------------------------------
my ($infile) = @ARGV;

if (! -r $infile) {
    die "Can't read '$infile'\n";
    }
if (! -f $infile) {
    die "Input $infile is not a plain file\n";
	}

open (FILE, $infile) or die ("Could not open '$infile'!");
my ($coords,$lon,$lat,$lo,$la,$name,$seq,$cmt,$desc,$fon,$sym);

while (<FILE>)
    {
    next if /^#/;
    chop $_;
    ($name,$desc,$fon,$coords,$sym,$seq) = split '\t', $_;
    $coords =~ tr/ //ds;
    ($la,$lo) = split ',', $coords;
    
    # do some rounding 
    $lat = sprintf("%.5f", $la);
    $lon = sprintf("%.5f", $lo);
    
	my $temp = $template;
	$temp =~ s/LAT/$lat/g;
	$temp =~ s/LON/$lon/g;
	$temp =~ s/SEQ/$seq/g;
	$temp =~ s/NAME/$name/g;
    $temp =~ s/CMT/$desc/g;
    $temp =~ s/DESC/$desc/g;
    $temp =~ s/FON/$fon/g;
    $temp =~ s/SYM/$sym/g;
    
    # The VAG system does not understand concatenated vCard files, 
    # so we need to write every entry in its own file.
    #
	my $outfile = "$seq-$name\.vCard";
	$outfile =~ s/\s+//g;		# remove all whitespace from filename
	
	print "Writing $outfile ..";

	open(my $fh, '>', $outfile) or die "Could not open file '$outfile' $!";
	print $fh $temp;
	close $fh;
	
	print ". done.\n";
    }

close (FILE);

1;
