#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use open qw/:std :utf8/;

use Encode;
use Getopt::Long;

my ($programme) = $0 =~ m|^(?:.*/)?(.+)|;
$programme = decode_utf8($programme);

my $repertoire = ""; #par exemple "Vv2_new"
my $journal = ""; #par exemple "Nom_Du_Revue.txt"
my $id    = ""; #par exemple "Nom_Du_Article.txt"
my $data  = ""; #par exemple "Vieillissement_V2_New.txt"

eval	{
	$SIG{__WARN__} = sub {usage(1);};
		GetOptions(
		"reportoire=s"      => \$reportoire,
		"journal=s"         => \$journal,
		"id=s"         => \$id,
		"data=s"         => \$data,
		);
	};
$SIG{__WARN__} = sub {warn $_[0];};

usage(2) if not $repertoire or not $journal or not $id or not $data;

my @a = ();
open(REVUE, "<:utf8", $journal) 
	or die "Couldn't open file $revue, $!";
while(my $revue = <REVUE>) 
{
	chomp($revue);
	$revue =~ s/\s/_/go;
	push(@a, $revue);
}
close REVUE;

open(ID, "<:utf8", $id) or 
	die "Couldn't open file $id, $!";
chomp(my @b = <ID>);
close ID;

open(DATA, ">:encoding(UTF-8)",$data) 
	or die "Couldn't open file $data, $!";

print DATA "\"revue\",\"id\"\n";

for(my $i = 0 ; $i <= $#a ; $i ++)
{	
	open(FILE, "<:utf8", "$repertoire/$b[$i]") or die "Couldn't open file $b[$i]$!";                                                                                                                                                     
	print DATA "**** *revue_$a[$i] *id_$b[$i]\n";
	while(my $ligne = <FILE>)
	{
		$ligne =~ s/\*//g;
		print DATA $ligne;
	}
	close(FILE);
}
close(DATA);
close(ID);

exit 0;

sub usage
{
my $code = shift;

print STDERR "Usage : $programme -r repertoire -j journal -i id -d data \n";

exit $code;
}
