<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- License: public domain.
     Distribution: http://lcr.math.ist.utl.pt/texdoc-php/ 
     Author: Joao P Matos jmatos@math.ist.utl.pt -->
<head>
<title>Items</title>
<?php
include("css.php");
?>
</head>
<body>
<?php

include("config.php");

### It seems natural to give higher precedence to files in the local tree
### but build lists starting with the distribution tree.
$texmftrees2 = array_reverse($texmftrees,TRUE);

### TODO! 
### Changes should make possible to use an arbitrary set of texmf trees

### Detecting the absolute location of this script both 
### in the file system and url space

$ourfs = dirname($_SERVER['SCRIPT_FILENAME']);
$oururl = dirname($_SERVER['PHP_SELF']);

### checklink checks all trees for the file in the given order
### returns $link (a relative URL to the document)

function checklink($relpath)
{
  global $texmftrees2, $ourfs, $oururl, $class, $zextensions;
  reset($texmftrees2['fs']);
  $link = "";
  while (list( $treekey, $treepath ) = each($texmftrees2['fs']))
    {
      foreach ($zextensions as $zext)
	{
	  if ( $_GET[$treekey] <> "no" )
	    {
	      $docpath = $ourfs . "/" . $treepath . "doc/";
	      if ( file_exists($docpath . $relpath . $zext))
		{
		  if ( $treekey == "local" )
		    {
		      $class = " class=\"local\" ";
		    }
		  $link = $texmftrees2['url'][$treekey] . "doc/" . $relpath . $zext ;
		  break;
		} 
	      $texpath = $ourfs . "/" . $treepath . "tex/";
	      if ( file_exists($texpath . $relpath . $zext))
		{
		  if ( $treekey == "local" )
		    {
		      $class = " class=\"local\" ";
		    }
		  $link = $texmftrees2['url'][$treekey] . "tex/" . $relpath . $zext;
		  break;
		}     
	    }
	}
    }
  return $link;  
} 

##### Parse the texdoc data files
$section=$_GET["section"];
$line=0;

while (list( $treekey, $treepath) = each($texmftrees['fs']))
{
  if ($_GET[$treekey] <> "no")
    {
      $texdocdat=$treepath . "texdoctk/" . $texmftrees['datname'][$treekey] ;
      if (file_exists($texdocdat))
	{    
	  $row = 0;
	  $handle = fopen ($texdocdat,"r");
	  while ($data[$row] = fgetcsv ($handle, 1000, ";")) 
	    {
	      if ( strpos($data[$row][0],"#") === false )
		{
		  if ( count($data[$row]) == 1 )
		    {
		      $section_name = str_replace("@", "", $data[$row][0]);
		    }
		  else
		    {
		      if ( $section_name==$section)
			{
			  if ($treekey == "local")
			    {
			      $class = " class=\"local\" ";
			    }
			  else
			    {
			      $class = "";
			    }
			  $mydata[$data[$row][0]]["link"] = $data[$row][2];
			  $mydata[$data[$row][0]]["description"] = $data[$row][1];
			  $colorcode = "";
			  $link = checklink($mydata[$data[$row][0]]["link"]);
			  if ($link == "")
			    {
			      $colorcode = " style=\"color:red\" ";
			      $link = $treepath . $mydata[$data[$row][0]]["link"];
			      
			    }
			  $linha[$line]= "<tr><td><a  " . $class . " href=\"" . $link . "\" target=\"_blank\"" . $colorcode . ">"  . $mydata[$data[$row][0]]["description"] . "</a></td></tr>\n";
			  $line++;
			}
		    }
		}
	      $row++;
	    }
	  fclose ($handle);
	}
    }
}
echo "<h3>" . $section . "</h3>";
?>

<table>
<?php
$numline= $line;
$line=0;
while ($line < $numline)
{
  echo $linha[$line];
  $line++;
}
?>
</table>
<?php

?>
</body>
</html>
