#!/usr/bin/perl -s

################################################################
# Fazedor das labels pra etaps
#
# Se nao funcionar, nao fui eu que fiz.
# Se funcionar, fui O:-) <rubenfonseca@di.uminho.pt>
################################################################

use strict;
use warnings;
use Text::CSV_XS;
use utf8;

my $csv = Text::CSV_XS->new({binary => 1});
my $file = shift or die "Need a CSV file";

my $nigerianos = 0;
my $ucranianos = 0;

my $count = 0;

my %map = (
	SC => 'Workshop on Software Composition',
	HAV => 'Heap Analysis and Verification',
	OpenCert => 'Foundations and Techniques for Open Source Software Certification',
	Bytecode => 'Second Workshop on Bytecode Semantics, Verification, Analysis and Transformation',
	MOMPES => '4th International Workshop on Model-based Methodologies for Pervasive and Embedded Software',
	'SLA++P' => 'Model-driven High-level Programming of Embedded Systems',
	'LDTA' => 'Seventh Workshop on Language Descriptions, Tools and Applications',
	'ETAPS' => 'European Joint Conferences on Theory And Practice of Software', 
	'MBT' => '3rd Workshop on Model Based Testing',
	'HFL' => 'Hardware design using Functional Languages',
	'ACCAT' => 'Applied and Computational Category Theory',
	'Mobility, Ubiquity, and Security' => 'Mobility, Ubiquity, and Security',
	'GT' => 'Sixth International Workshop on Graph Transformation and Visual Modeling Techniques',
	'Program Transformation with Stratego/XT' => 'Program Transformation with Stratego/XT',
	'TERMGRAPH' => '4th International Workshop on Computing with Terms and Graphs',
	'FinCo' => 'Foundations of Interactive Computation',
	'FESCA' => 'Formal Foundations of Embedded Software and Component-Based Software Architectures',
	'Beyond the Generators' => 'Beyond the Generators: Practical Techniques for Real-World Software Generation',
	'AVIS' => 'Canceled (AVIS)',
	'QAPL' => 'Fifth Workshop on Quantitative Aspects of Programming Languages',
	'COCV' => 'Sixth Workshop on Compiler Optimization Meets Compiler Verification',
	'IFIP' => 'IFIP',
        'JJ' => 'JJ',
	'WITS' => 'Seventh Workshop on Issues in the Theory of Security',
);

# Parse :D
my $db = {};
open F, "<$file";
while(<F>) {
	if($csv->parse($_)) {
		my @fields = $csv->fields;

		next if $fields[0] eq 'Id';
		if($fields[5] =~ /(nigeria|kenya|gambia|congo|ghana)/i # && $fields[13] eq 'pending'
                  ) {
			$nigerianos++;
			next;
		}

		if($fields[5] =~ /ukraine/i  #  && $fields[13] eq 'pending'
                  ) {
			$ucranianos++;
			next;
		}

		$fields[1] = join(' ', map { ucfirst($_) } (split / /, lc($fields[1])));
		$fields[4] = join(' ', map { ucfirst($_) } (split / /, lc($fields[4])));
		$fields[5] = join(' ', map { ucfirst($_) } (split / /, lc($fields[5])));

		# Protect some symbols LaTex doesn't like
		for(@fields) { s!\\!\$\\backslash\$!g } 
		for(@fields) { s!([\{\}\_\^\$\#\%\&])!\\$1!g } 

		$fields[4] =~ s/ De / de /g;
		$fields[4] =~ s/ And / and /g;
		$fields[4] =~ s/ Do / do /g;
		$fields[4] =~ s/ Da / da /g;
		$fields[4] =~ s/ Dos / dos /g;
		$fields[4] =~ s/ Of / of /g;
		$fields[4] =~ s/\bIst\b/IST/g;
		$fields[4] =~ s/\bIp\b/IP/g;
		$fields[4] =~ s/\bUsp\b/USP/g;
		$fields[4] =~ s/\bCwi\b/CWI/g;

		next unless $fields[2];

		# Save
		my @sat = construct_authorized_entrance($fields[7], $fields[8]);
                unshift(@sat,"JJ");
		for my $foo (@sat) {
			       $db->{$foo} ||= [];
			push @{$db->{$foo}}, \@fields;
		}
		
	} else {
		die $_;
	}
}

close F;
for my $conf (sort keys %{$db}) {
	my $ourcach = $map{$conf} or die $conf;

        print "DEBUG $conf\n";

        my $g = preambulo("$conf");

	print $g <<EOF;
	\\begin{center}
                \\includegraphics[width=\\textwidth]{header-small}
		\\vskip 5mm
		\\Huge{\\sf{$ourcach}}
		\\scriptsize
		\\begin{longtable}{lll}
EOF

	for my $user (sort {$a->[1] cmp $b->[1]} @{$db->{$conf}}) {
		# if(length($user->[1]) > 20) {
		# 	$user->[1] = substr($user->[1], 0, 20);
		# 	$user->[1] = $user->[1] . "\\ldots";
		# }

		if(length($user->[4]) > 40) {
			$user->[4] = substr($user->[4], 0, 40);
		 	$user->[4] = $user->[4] . "\\ldots";
		}

		print $g <<EOF;
		$user->[1] \& $user->[4], $user->[5] \& $user->[6] \\\\
EOF
	}

	print $g <<EOF;
		\\end{longtable}
	\\end{center}
	\\newpage
EOF
posambulo($g);
}


sub construct_authorized_entrance {
	my ($type, $satalite) = @_;

	my @sat = get_acronim_from_satalite($satalite);

	if($type eq 'Conferences \& Satellite Events') {
		unshift @sat, "ETAPS";
	} elsif($type eq 'Satellite Events Only') {
		# nothing to see here, move along
	} else {
		die "Conference type not recognized: $type\n";
	}

	return @sat;
}


sub get_acronim_from_satalite {
	my ($satalite) = @_;

	my @res;
	my @confs = split /\;/, $satalite;
	for my $conf (@confs) {
		$conf =~ /^([^\-\:]+)/;
		$conf = $1;
		$conf =~ s/^\s+//;
		$conf =~ s/\s+$//;
		push @res, $conf;
	}

	return @res;
}

sub preambulo {
	my ($filen) = @_;
	my $g;

	# Preamblo
        $filen =~ s/[\/, ]/_/g;
	open $g, ">out$filen.tex" or die("cantopen '$filen'\n");

	print $g <<EOF;
\\documentclass[12pt,a4paper]{article}
\\usepackage{a4wide}
\\usepackage[a4paper,top=1cm,left=1cm,right=1cm,bottom=2cm]{geometry}
\\usepackage{color}
\\usepackage{graphicx}
\\usepackage{longtable}
\\usepackage[utf8]{inputenc}
\\usepackage{ucs}
\\pagestyle{empty}

\\begin{document}
EOF

	return $g;
}

sub posambulo {
	my ($g) = @_;

	# Posambulo (<-- ?)
	print $g "\n\\end{document}\n";
	close $g;
}

