#/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";

                            #============================================================================================#

                           my ($titretag,$descriptiontag) = &etiquetage($titre,$description);

                            #============================================================================================#


                            print OUT2 "<item><title>$titretag</title><description>$descriptiontag</description></item>\n";
                                   
                        }
                }
                close OUT;
                close OUT2;
                
            }
        }
    }
}
#----------------------------------------------


sub etiquetage {

    my ($t,$d)=@_;

    #titres
    open(TMP,">:encoding(utf-8)","titre.txt");
    print TMP $t;
    close (TMP);

    system("perl tokenise-utf8.pl titre.txt|tree-tagger -token -lemma -no-unknown ~/sources/treetagger/lib/french-utf8.par > titre_tag.txt");

    system("perl treetagger2xml-utf8.pl titre_tag.txt utf8");

    open(TMP2, "<:encoding(utf8)","titre_tag.txt.xml");

    my $t_tag ="";  


    my $ligne = <TMP2>; 


    while (my $ligne = <TMP2>) {        
        chomp $ligne;        
        $t_tag = $t_tag . $ligne;
    }
    
    close (TMP2);



    #descriptions
    open(TMP3,">:encoding(utf-8)","description.txt");
    print TMP3 $d;
    close (TMP3);

    system("perl tokenise-utf8.pl description.txt|tree-tagger -token -lemma -no-unknown ~/sources/treetagger/lib/french-utf8.par > description_tag.txt");
    system("perl treetagger2xml-utf8.pl description_tag.txt utf8");

    open(TMP4, "<:encoding(utf8)","description_tag.txt.xml");

    my $d_tag ="";  


    my $ligne = <TMP4>; 

    while (my $ligne = <TMP4>) {        
        chomp $ligne;
        $d_tag = $d_tag . $ligne;
    }

    close (TMP4);


    return($t_tag,$d_tag);
}