#!/usr/bin/perl

use Getopt::Std;
use Pod::Usage;

getopts('ho:u');

if ($opt_h)
{
    pod2usage( -verbose => 2);
    exit;
}

if ($opt_o)
{
    open(OUT, "> $opt_o") || die "Can't open $opt_o for output";
    binmode (OUT, ":utf8") if ($opt_u);
    select OUT;
}

$header = <>;
chomp $header;
$header =~ s/^\x{FEFF}//o if ($opt_u);
@head = get_csv($header);

$gap = 0;
while(<>)
{
    chomp;
    @dat = get_csv($_);
    next unless (@dat);
    $i = 0;
    if ($gap)
    { print "\n"; }
    else
    { $gap = 1; }
    print join("\n", map {"\\$head[$i++] $_"} @dat);
    print "\n";
}

sub get_csv
{
    my ($str) = @_;
    my (@dat) = $str =~ m/(?=.)((?:"(?:""|[^"])*")|(?:[^,]*))(?:,|$)/og;
    my ($empty) = 1;

    foreach (@dat)
    { 
        s/""/"/og if (s/^"(.*)"$/$1/o);
        $empty = 0 if ($_ ne '');
    }
    if ($empty)
    { return undef; }
    else
    { return @dat;}
}

__END__

=head1 NAME

csv2sh - converts Comma Separated Variables into Shoebox/Toolbox

=head1 SYNOPSIS

  csv2sh [-o file] [-u] infile

    -o file     Optional output file otherwise output goes to stdout
    -u          Assume file is in Unicode (so ignore BOM, etc.)

=head1 DESCRIPTION

csv2sh reads a comma separated variables file, as output from a spreadsheet
and converts it into standard format markers. The marker names (without \)
are assumed to be the column headers. Blank lines are ignored.
