#!/usr/bin/perl

#
#                 Convert a file of CSVs into unix aliases (e.g., mutt)
#
# The resulting aliases can be sourced
# by the standard unix mail programs and mutt.
#  
# Synopsis: csv2aliases [options] csv-file alias-file
#
# Aliases are created from the fields named "Nachname" , "Vorname".
# The alias name will be the concatenation of those two, first "Vorname", then "Nachname").
# With the -s option the 2 components can be swapped before concatenation.
# If there is no name but an organization, then that will be used, provided
# option -f was given.
# The alias value will be the list of email adresses
# (fields "E-Mail" and, possibly, "E-Mail 2").
#
# Contacts without an email are ingored.
#
# Type 'csv2aliases -h' to see the list of options.
#
# Note:
# o  based on the Perl module Text::xSV.
#    You can download it from
#                    http://search.cpan.org/~tilly/
#    Here are hints how to install it
#        http://www.cpan.org/misc/cpan-faq.html#How_install_Perl_modules
#
# Author:
#    Gabriel Zachmann, Sep 2004, www.gabrielzachmann.org
#
# Suggestions, comments, and improvements are welcome.
#



require 5.004;
require "getopts.pl";
use Text::xSV;

#use diagnostics;

#  process options
#  save program/options for later
$invokation = join " ", @ARGV;
$invokation = "$0 $invokation";
Getopts('hslwf');

if ( $opt_h )
{
	print "usage: csv2aliases [options..] csv-file alias-file\n";
	print " convert csv file into alias file,\n";
	print " options:\n";
	print " -s        - swap the name components (nachname ## vorname)\n";
	print " -l        - don't make alias all lowercase\n";
	print " -w        - print warning for records without a name\n";
	print " -f        - use company name if no name is in record\n";
	print " -h        - this help message\n";
	exit 0;
}


# open i/o files
$infile = shift;
$outfile = shift;

if ( ! defined($outfile) )
{
	die "csv2aliases no output file defined!\n";
}
else
{
	open OUTFILE, ">$outfile"
        or die "csv2aliases: couldn't open alias file $outfile: $!\n";
}

if ( ! defined($infile) )
{
	die "csv2aliases no csv file defined!\n";
}
if ( -d $infile )
{
	die "csv2aliases csv file $infile is a directory!\n";
}

#  read csv file
my $csv = Text::xSV->new( sep => ';' );
$csv->open_file("$infile");
$csv->bind_header();

#  print header of alias file
print OUTFILE "\n";
print OUTFILE "# Alias file\n";
print OUTFILE "# created by '$invokation'\n";
print OUTFILE "# on " . `date`;
print OUTFILE "# from file $infile\n";
print OUTFILE "\n";

RECORD:
while ( $csv->get_row() )
{
    my ($vorname) = $csv->extract("Vorname");
    my ($nachname) = $csv->extract("Nachname");
    my ($firma) = $csv->extract("Firma");
    my ($email) = $csv->extract("E-Mail");
    my ($teflon) = $csv->extract("Telefonnummer");
    my ($teflonbuero) = $csv->extract("Telefonnummer (geschäftlich)");
    my ($handy) = $csv->extract("Handy");
    my ($strasse) = $csv->extract("Straße");
    my ($plz) = $csv->extract("PLZ");
    my ($stadt) = $csv->extract("Stadt");
    my ($email2) = $csv->extract("E-Mail 2");

	# check whether record should appear in aliases file
	if ( $email eq "" )
	{
		next RECORD;
	}

	if ( $email !~ m/.*\@.*/o )
	{
		if ( $opt_w )
		{
			warn "csv2aliases: E-Mail does not contain an \@!\n";
			warn " email=$email\n";
			warn " skipping ..\n\n";
		}
		next RECORD;
	}

	if ( $opt_f )
	{
		$vorname = "$firma";
	}

	if ( $vorname eq "" && $nachname eq "" )
	{
		if ( $opt_w )
		{
			warn "csv2aliases: Couldn't find a name in record\n";
			warn " email=$email\n";
			warn " skipping ..\n\n";
		}
		next RECORD;
	}

	#  output an alias like:
	#
	#  # Last name, First name
	#  # other fields ...
	#  alias <last name><first name> mail-adress-list

	if ( defined($opt_s) )
	{
		$help = $nachname;  $nachname = $vorname;  $vorname = $nachname;
	}

	print OUTFILE "# $vorname $nachname\n";

	# clean up names for alias
	$vorname = cleanname( $vorname );
	$nachname = cleanname( $nachname );

	if ( ! defined($opt_l) )
	{
		$vorname = lc( $vorname );
		$nachname = lc( $nachname );
	}

	print OUTFILE "# $teflon, $teflonbuero, $handy\n";
	print OUTFILE "# $strasse, $plz, $stadt\n";

	print OUTFILE "alias $vorname$nachname $email";
	if ( "$email2" ne "" )
	{
		print OUTFILE ",$email2";
	}
	print OUTFILE "\n\n";
}


sub cleanname
{
	$_ = shift;
	s/\s/_/og;
	s/ä/ae/og; s/ö/oe/og; s/ü/ue/og;
	s/Ä/Ae/og; s/Ö/Oe/og; s/Ü/Ue/og;
	s/ß/ss/og;
	s/é/e/og; s/è/e/og;
	s/á/a/og; s/à/a/og;
	s/\W//og;
	return $_;
}

