#/usr/bin/perl
#-----------------------------------------------------------
use utf8;
use strict;
binmode(STDOUT, ":encoding(UTF-8)");
#-----------------------------------------------------------
# MODE D'EMPLOI : perl parcours-arbo.pl arborescence numRubrique nomFichierXml nomFichierTxt
#-----------------------------------------------------------
if ($#ARGV != 3) {print "Il manque un argument à votre programme....\n";exit;} 
my $rep="../../$ARGV[0]";
my $RUBRIQUE="$ARGV[1]";
mkdir 'PERL';
my $dossier_sortie = "./PERL";
if ($RUBRIQUE =~ '3208') {
	mkdir './PERL/UNES';
	$dossier_sortie .= '/UNES';
	}
elsif ($RUBRIQUE =~ '3476') {
	mkdir './PERL/CINEMA';
	$dossier_sortie .= '/CINEMA';
}
elsif ($RUBRIQUE =~ '3236') {
	mkdir './PERL/ACTU MEDIAS';
	$dossier_sortie .= '/ACTU MEDIAS';
}
elsif ($RUBRIQUE =~ '3260') {
	mkdir './PERL/LIVRES';
	$dossier_sortie .= '/LIVRES';
}
elsif ($RUBRIQUE =~ '3246') {
	mkdir './PERL/CULTURE';
	$dossier_sortie .= '/CULTURE';
}
elsif ($RUBRIQUE =~ '823353') {
	mkdir './PERL/POLITIQUE';
	$dossier_sortie .= '/POLITIQUE';
}
elsif ($RUBRIQUE =~ '3242') {
	mkdir './PERL/SPORT';
	$dossier_sortie .= '/SPORT';
}
elsif ($RUBRIQUE =~ '3244') {
	mkdir './PERL/PLANETE';
	$dossier_sortie .= '/PLANETE';
}
elsif ($RUBRIQUE =~ '3234') {
	mkdir './PERL/ECONOMIE';
	$dossier_sortie .= '/ECONOMIE';
}
elsif ($RUBRIQUE =~ '3232') {
	mkdir './PERL/IDEES';
	$dossier_sortie .= '/IDEES';
}
elsif ($RUBRIQUE =~ '3224') {
	mkdir './PERL/SOCIETE';
	$dossier_sortie .= '/SOCIETE';
}
elsif ($RUBRIQUE =~ '3214') {
	mkdir './PERL/EUROPE';
	$dossier_sortie .= '/EUROPE';
}
elsif ($RUBRIQUE =~ '3210') {
	mkdir './PERL/INTERNATIONAL';
	$dossier_sortie .= '/INTERNATIONAL';
}
elsif ($RUBRIQUE =~ '651865') {
	mkdir './PERL/TECHNOLOGIES';
	$dossier_sortie .= '/TECHNOLOGIES';
}

# on s'assure que le nom du répertoire ne se termine pas par un "/"
$rep=~ s/[\/]$//;
open my $output, ">:encoding(UTF-8)","$dossier_sortie/$ARGV[3]";
open my $output2, ">:encoding(UTF-8)","$dossier_sortie/$ARGV[2]";
print $output2 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<corpus>\n";
#----------------------------------------
&parcoursarborescencefichiers($rep);	#recurse!
#----------------------------------------
print $output2 "</corpus>\n";
close $output;
close $output2;
#----------------------------------------------
exit;
#----------------------------------------------
sub parcoursarborescencefichiers {
    my $path = shift(@_);
    opendir(DIR, $path) or die "can't open $path: $!\n";
    my @files = readdir(DIR);
    closedir(DIR);
    foreach my $file (@files) {
		next if $file =~ /^\.\.?$/;
		$file = $path."/".$file;
		if (-d $file) {
			print "On entre dans le REPERTOIRE : $file \n";
			&parcoursarborescencefichiers($file);	#recurse!
			print "On sort du REPERTOIRE :  $file \n";
		}
		if (-f $file) {
			if ($file =~ /$RUBRIQUE.+\.xml$/) {
				print "Traitement du fichier $file \n";
				open my $input, "<:encoding(UTF-8)",$file;
				$/=undef; # par défaut cette variable contient \n
				my $ligne=<$input> ;
				close($input);
				while ($ligne=~/<item><title>(.+?)<\/title>.+?<description>(.+?)<\/description>/gs) {
					my $titre=&nettoyage($1);
					my $description=&nettoyage($2);
					print $output "$titre \n";
					print $output "$description \n";
					print $output "----------------------------\n";
					print $output2 "<item><titre>$titre</titre><description>$description</description></item>\n";
				}
			}
		}
    }
}
#----------------------------------------------
sub nettoyage {
	my $texte=shift @_;
	$texte=~s/(^<!\[CDATA\[)|(\]\]>$)//g;
	$texte.=".";
	$texte=~s/\.+$/\./;
	return $texte;
}
#----------------------------------------------
