#!/usr/bin/perl
$i=0;
$j=0;
print "File le nom du fichier url qui se trouve dans le folder LISTES-URL! \n";
$url=<STDIN>;
print "$url";
chomp($url);
open(LECTURE, "../LISTES-URL/$url") or die"impossible de trouver $url!! `\n";
print "File le nom du file SANS EXTENSION pour le tableau en html qui sera saved dans le folder programmes/ ffs! \n";
$my_tab=<STDIN>;
print "$my_tab";
chomp($my_tab);
open(ECRITURE, ">$my_tab$j.html");
print ECRITURE "<html>\n<head>\n<title>tableau de liens</title>\n</head><body>\n<table border=\"1\">\n" ;
while($ligne=<LECTURE>)
{
	print $ligne;
	chomp($ligne);
	print ECRITURE "<tr><td><a href=\"$ligne\">URL$i</a></td>";
	$wgett="wget -O ../PAGES-ASPIREES/toto$i.html $ligne";
	system($wgett);
	if($?==0)
	{
		print ECRITURE "<td><a href= \"../PAGES-ASPIREES/toto$i.html\">FILE$i</a></td>";
		$lynx="lynx -dump ../PAGES-ASPIREES/toto$i.html > ../DUMP-TEXT/toto$i.txt";
		system($lynx);
		print ECRITURE "<td><a href= \"../DUMP-TEXT/toto$i.txt\">TEXT$i</a></td>";
		$contextfile="../DUMP-TEXT/toto$i.txt";
		open(CONTEXTE, "$contextfile");
		$i++;
		while($lignetxt=<CONTEXTE>)
			{

			if($lignetxt =~ /barrage/i)
				{
				$avant = $`;
				$apres = $';
				$barrage = $&;
			
			
				print ECRITURE "<td><i>...$avant<b>$barrage</b>$apres...</i></td></tr>";
				if($i%20==0 || eof(LECTURE))
				{
					if($j==0)
					{
						$j++;
						print ECRITURE "<align=\"center\"><a href=\"$my_tab$j.html\">next</a></align></table></body></html>";
						close(ECRITURE);
						open(ECRITURE, ">$my_tab$j.html");
						print ECRITURE "<html>\n<head>\n<title>tableau de liens</title>\n</head><body>\n<table border=\"1\">\n" ;
					}
					else
					{
						$j--;
						print ECRITURE "<align=\"center\"><a href=\"$my_tab$j.html\">prec</a></align>";
						$j++;
						$j++;
						if(<LECTURE>)
						{
							print ECRITURE "<align=\"center\"><a href=\"$my_tab$j.html\">...next</a></align>";
							close(ECRITURE);
							open(ECRITURE, ">$my_tab$j.html");
							print ECRITURE "<html>\n<head>\n<title>tableau de liens</title>\n</head><body>\n<table border=\"1\">\n" ;
						}
					
						
								
					}
		
			
				}			
				last;
				}
				
		
	
		
				}
	}
	else
	{	
	close(CONTEXTE);
	print ECRITURE "<td>PROBLEME</td>";
	print ECRITURE "<td>PROBLEME</td>";
	print ECRITURE "<td>PROBLEME</td></tr>";
	if($i%20==0 || eof(LECTURE))
				{
					if($j==0)
					{
						$j++;
						print ECRITURE "<align=\"center\"><a href=\"$my_tab$j.html\">next</a></align></table></body></html>";
						close(ECRITURE);
						open(ECRITURE, ">$my_tab$j.html");
						print ECRITURE "<html>\n<head>\n<title>tableau de liens</title>\n</head><body>\n<table border=\"1\">\n" ;
					}
					else
					{
						$j--;
						print ECRITURE "<align=\"center\"><a href=\"$my_tab$j.html\">prec</a></align>";
						$j++;
						$j++;
						if(<LECTURE>)
						{
							print ECRITURE "<align=\"center\"><a href=\"$my_tab$j.html\">...next</a></align>";
							close(ECRITURE);
							open(ECRITURE, ">$my_tab$j.html");
							print ECRITURE "<html>\n<head>\n<title>tableau de liens</title>\n</head><body>\n<table border=\"1\">\n" ;
						}
					
						
								
					}
		
			
				}			
	$i++;
	}
}

print ECRITURE "\n</table>\n</body>\n</html>";
close(LECTURE);
close(ECRITURE);