#/usr/bin/perl


#-----------------------------------------------------------
my $rep="$ARGV[0]";
my $rubrique = "$ARGV[1]";
my %dico;
$rep=~ s/[\/]$//;

my $output1="./profondeur/$rubrique.xml";
if (!open (FILEOUT,">$output1")) { die "impossible d'ouvrir $output1"};
print FILEOUT "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
print FILEOUT "<PARCOURS>\n";
print FILEOUT "<NOM>CYNDEL et MANON</NOM>\n";
close (FILEOUT);

#----------------------------------------
&parcoursarborescencefichiers($rep);	
#----------------------------------------

open (FILEOUT,">>:encoding(utf-8)", $output1);
print FILEOUT "</PARCOURS>\n";
close(FILEOUT);
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 "<NOUVEAU REPERTOIRE> ==> ",$file,"\n";
			&parcoursarborescencefichiers($file);	
			print "<FIN REPERTOIRE> ==> ",$file,"\n";
		}
		if (-f $file) {
			
            if ($file =~ /$rubrique.+\.txt$/) {
                
                print "<",$i++,"> ==> ",$file,"\n";

		print "<",$i++,"> ==> ",$file,"\n";
				open(FILE, "<:encoding(utf-8)", "$file");
		
				my$texte = "";
				my%dico;
                open (OUT, ">>:encoding(utf-8)", "./profondeur/$rubrique.txt");
                open (OUT2, ">>:encoding(utf-8)", "./profondeur/$rubrique.xml");
               
                my $texte="";
                while (my$ligne = <FILE>){
				chomp $ligne;
				$ligne =~ s/\r//g;
				$texte = $texte . $ligne;
				}
                close FILE;

                $texte =~ s/>\s+</></g;
                while ($texte =~m/Le Monde[^\|]+\|[^\|]+\|([^<]+)<article/g) {
                	my $article= $1;
			         $article=~s/&lt;.+?&gt;//g;
			         $article=~ s/^.+?(\s){21}//g;


                        if (!(exists $dico{$article})) {
                            $dico{$article} = 1; 

                            print OUT "$article\n";

                         


                            print OUT2 "<item><article>$article</article></item>\n";
                                   
                        }
                }
                close OUT;
                close OUT2;
                
            }
        }
    }
}
#----------------------------------------------