#!/usr/bin/perl
use warnings;
use strict;

# ls *.csv | xargs -i sh -cx "./csv2tsv.pl {} > tsv/{} 2>>/tmp/log"

use Data::Dump qw(dump);

my $cols = 0;
my $delimiter;

while(my $line = <>) {
	chomp $line;
	if ( $cols == 0 ) { # header
		$cols = $line =~ s/([,;])/$1/g;
		$delimiter = $1;
		warn "# delimiter [$delimiter] columns=$cols\n";
	}

	$line =~ s/,$/,_null_/; # protect last empty column

	my @v = split(/$delimiter/, $line);
	if ( $#v > $cols ) {
#warn "FIX $#v $cols", dump(@v);
		my @l2 = splice( @v, -2 ); # keep last two
		my @fix = splice( @v, 6 );
warn "XXX before ",dump( @fix );
		my ( $k, $v ) = ( shift(@fix), '' );
		while ( @fix && (( length($k) + length($fix[0]) ) < 8 ) ) {
			$k .= $k ? ',' : '';
			$k .= shift(@fix);
		}
		$k .= " " x (8 - length($k)); # pad key to 8 spaces
		$v = join(',', @fix);
warn "YYY after  ",dump( $k, $v );
		die "ASSERT key < 8 [$k]" if length($k) < 8;
		die "ASSERT key > 8 [$k]" if length($k) > 8;
		@v = ( @v, $k, $v, @l2 );
	}

	$v[-1] =~ s/_null_//;

	$ENV{DEBUG} || print join("\t", @v), "\n";
}
