#/usr/bin/perl


#-----------------------------------------------------------
my $rep="$ARGV[0]";
my $rubrique = "$ARGV[1]";
my %dico;
$rep=~ s/[\/]$//;

my $output1="./surface/$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.+\.xml$/) {
                
                print "<",$i++,"> ==> ",$file,"\n";
                
                $codage = "utf-8";
                open (FIC, "<:encoding($codage)", $file);
                open (OUT, ">>:encoding($codage)", "./surface/$rubrique.txt");
                open (OUT2, ">>:encoding($codage)", "./surface/$rubrique.xml");
               
                my $texte="";
                while (my $ligne = <FIC>) {
                    chomp $ligne;
                    $ligne =~ s/\r//g;
                    $texte = $texte . $ligne;
                }
                close FIC;
                $texte =~ s/>\s+</></g;
                while ($texte =~m/<item><title>([^<]+?)<\/title><link>(?:[^<]+?)<\/link><description>([^<]+?)<\/description>/g) {
                        my $titre = $1;
                        my $description = $2;
                        $titre=~s/&lt;.+?&gt;//g;
                        $titre.=".";
                        $titre=~s/\?\.$/\?/;
                        $description=~s/&lt;.+?&gt;//g;

                        if (!(exists $dico{$titre})) {
                            $dico{$titre} = 1; 

                            print OUT "$titre\n";
                            print OUT "$description\n\n";


                            print OUT2 "<item><title>$titre</title><description>$description</description></item>\n";
                                   
                        }
                }
                close OUT;
                close OUT2;
                
            }
        }
    }
}
#----------------------------------------------
