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

use Getopt::Long;

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

my $dir  = "";
my $output = "ira_900.txt";

eval	{
	$SIG{__WARN__} = sub {usage(1);};
		GetOptions(
		"dir=s"  => \$input,
		"output=s" => \$output,
		);
	};
$SIG{__WARN__} = sub {warn $_[0];};

usage(2) if not $dir;

opendir(my $dh, $dir) or die "Couldn't open file, $!";
	my @files = readdir($dh);
	closedir($dh);
	my $size = @files;
open(DATA, ">:utf8",$output ) 
or die "Couldn't open file $output, $!";
print METADATA "\"id\",\"sous_corpus\"\n";
foreach my $file(@files){
	$file =~ /(\pL+)_\d*/;
	print DATA "**** *souscorpus_$1\n";
	open(FILE, "<:utf8", "$dir/$file") or die "Couldn't open file $file, $!";
	my @texte = <FILE>;
	print DATA @texte;
	close(FILE);
}
close(DATA);

exit 0;

sub usage
{
my $code = shift;

print  "Usage : $programme -d dir [ -o output ]\n";

exit $code;
}
