#!/usr/bin/perl -w

# http://www.experts-exchange.com/Programming/Languages/Scripting/Q_24798100.html

use strict; 
use Spreadsheet::WriteExcel; 

sub write_sheet ($$$) {
  my ($workbook, $sheetname, $filename) = @_;
  my $sheet = $workbook->addworksheet($sheetname);
  my $string0 = 0;
  my $string1 = 0;
  my $string2 = 0;
  my $string3 = 0;
  my $string4 = 0;
  my $string5 = 0;
  my $string6 = 0;
  my $number5 = 0;
  my $number6 = 0;
  my $number7 = 0;
  my $number8 = 0;
  my $number9 = 0;
  my $linenostart = 1;
  my $format1 = $workbook->add_format();
  $format1->set_num_format('#,##0.00');

# http://search.cpan.org/~jmcnamara/Spreadsheet-WriteExcel/lib/Spreadsheet/WriteExcel.pm 
  if ("sued" eq $ARGV[$#ARGV-1]) {
    $sheet->set_column(0, 0, 32);
    $sheet->set_column(1, 1, 13.71);
    $string1 = 1;
    $sheet->set_column(2, 2, 21.57);
    $sheet->set_column(3, 3, 8.14);
    $string3 = 1;
    $sheet->set_column(4, 4, 8.43);
    $string4 = 1;
    $sheet->set_column(5, 6, 9.57);
    $sheet->set_column(7, 7, 11);
    $sheet->set_column(8, 8, 12);
    $sheet->set_column(9, 9, 11.71);
    $sheet->set_column(10, 10, 12.29);
    $sheet->set_column(11, 11, 4);
    $sheet->set_column(12, 12, 7.14);
    $sheet->set_column(13, 14, 8.57);
    $sheet->set_column(15, 15, 7.71);
    $sheet->set_column(16, 16, 8.57);
    $sheet->set_column(17, 17, 8);
    $sheet->set_column(18, 18, 9.43);
    $sheet->set_column(19, 19, 5.43);
    $sheet->set_column(20, 20, 8);
    $sheet->set_column(21, 21, 11.14);
    $linenostart = 2;
  }
  if ("nord_service" eq $ARGV[$#ARGV-1]) {
    $sheet->set_column(0, 0, 11.57);
    $string0 = 1;
    $sheet->set_column(1, 1, 9.57);
    $sheet->set_column(2, 2, 9.43);
    $string2 = 1;
    $sheet->set_column(3, 3, 8.57);
    $string3 = 1;
    $sheet->set_column(4, 4, 25.43);
    $string4 = 1;
    $sheet->set_column(5, 5, 10.57);
    $string5 = 1;
    $sheet->set_column(6, 6, 8.71);
    $sheet->set_column(7, 7, 7.57);
    $number7 = 1;
  }
  if ("nord_diff" eq $ARGV[$#ARGV-1]) {
    $sheet->set_column(0, 0, 32.86);
    $sheet->set_column(1, 1, 13.71);
    $string1 = 1;
    $sheet->set_column(2, 2, 21.14);
    $sheet->set_column(3, 3, 12.29);
    $sheet->set_column(4, 4, 9.71);
    $sheet->set_column(5, 5, 16.71);
    $number5 = 1;
    $sheet->set_column(6, 6, 12.29);
    $number6 = 1;
    $sheet->set_column(7, 7, 9.00);
    $number7 = 1;
    $linenostart = 2;
  }
  if ("nord_return" eq $ARGV[$#ARGV-1]) {
    $sheet->set_column(0, 0, 32.00);
    $sheet->set_column(1, 1, 13.71);
    $string1 = 1;
    $sheet->set_column(2, 2, 21.14);
    $sheet->set_column(3, 3, 12.29);
    $sheet->set_column(4, 4, 9.71);
    $sheet->set_column(5, 5, 9.00);
    $number5 = 1;
    $sheet->set_column(6, 6, 12.29);
    $number6 = 1;
    $linenostart = 2;
  }
  if ("nord_diff_special" eq $ARGV[$#ARGV-1]) {
    $sheet->set_column(0, 0, 9.57);
    $sheet->set_column(1, 1, 9.43);
    $string2 = 1;
    $sheet->set_column(2, 2, 9.43);
    $string3 = 1;
    $sheet->set_column(3, 3, 8.57);
    $string4 = 1;
    $sheet->set_column(4, 4, 25.43);
    $string5 = 1;
    $sheet->set_column(5, 5, 10.57);
    $string6 = 1;
    $sheet->set_column(6, 6, 8.71);
    $sheet->set_column(7, 7, 8.57);
    $number8 = 1;
    $sheet->set_column(8, 8, 8.29);
    $number9 = 1;
    $linenostart = 2;
  }
  if ("nord_return_special" eq $ARGV[$#ARGV-1]) {
    $sheet->set_column(0, 0, 9.57);
    $sheet->set_column(1, 1, 9.43);
    $string2 = 1;
    $sheet->set_column(2, 2, 9.43);
    $string3 = 1;
    $sheet->set_column(3, 3, 8.57);
    $string4 = 1;
    $sheet->set_column(4, 4, 25.43);
    $string5 = 1;
    $sheet->set_column(5, 5, 10.57);
    $string6 = 1;
    $sheet->set_column(6, 6, 8.71);
    $sheet->set_column(7, 7, 8.57);
    $number8 = 1;
    $sheet->set_column(8, 8, 8.29);
    $number9 = 1;
    $linenostart = 2;
  }
     
	open(CSV, "$filename") or die "cannot open $!";
	while (<CSV>) {
    	chomp;
    	my(@vals)=split(/;/, $_);
#    	my $format = undef;
    	foreach my $vidx (0..$#vals) {
        if (($vidx == 0 && $string0 == 1) || ($vidx == 1 && $string1 == 1) || ($vidx == 2 && $string2 == 1) || ($vidx == 3 && $string3 == 1) || ($vidx == 4 && $string4 == 1) || ($vidx == 5 && $string5 == 1) || ($vidx == 6 && $string6 == 1)) {
        	$sheet->write_string(($.-1), $vidx, $vals[$vidx]);
        } elsif (($. > $linenostart) && (($vidx == 5 && $number5 == 1) || ($vidx == 6 && $number6 == 1) || ($vidx == 7 && $number7 == 1) || ($vidx == 8 && $number8 == 1) || ($vidx == 9 && $number9 == 1))) {
        	$sheet->write_number(($.-1), $vidx, $vals[$vidx], $format1);
        } else {
        	$sheet->write(($.-1), $vidx, $vals[$vidx]);
     	  }
    	}
	}
	close(CSV);
	return 1;
} 

my $wb = Spreadsheet::WriteExcel->new($ARGV[$#ARGV]);

foreach my $f ( @ARGV ) {	
  if ($f ne $ARGV[$#ARGV]) {	
    if ( -e $f ) {
        my $t = $f;
        $t =~ s/^.*\/|\..*$//g;
        write_sheet($wb,$t,$f);
    } else {
      if ($f ne $ARGV[$#ARGV-1]) {
        print "$f is not a file\n";
      }
    }
  }
}

$wb->close() or die "Error closing file: $!";