<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet href="." type="application/x-xsp"?>
<?xml-stylesheet href="/webpage2html.xsl.en" type="text/xsl"?>

<xsp:page
  language="Perl"
  xmlns:xsp="http://apache.org/xsp/core/v1" 
  xmlns:esql="http://apache.org/xsp/SQL/v2" 
  xmlns:conv="http://xmlns.knowscape.com/xsp/CharsetConv"
  xmlns:web="http://axkit.org/NS/xsp/webutils/v1"
>

<xsp:logic>
#  use XML::XPath;
#  use XML::XPath::XMLParser;

  my $ut = time;
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($ut);
  my $ROOT = '/var/www/webcie/xml';
  my @lang = qw/en es fr/;

  sub hasmenu {
    my $path = shift;
    my $fn = '';
    foreach my $lang (@lang) {
      $fn = "$path/menu.xml.$lang";
      return $fn if -r $fn;
    }
    $fn = "$path/menu.xml";
    return $fn if -r $fn;
    return 0;
  }

</xsp:logic>

<webpage version="0.1">

<title>WebCIE: site map</title>
<author e-mail="turbo@cie.unam.mx">turbo@cie.unam.mx</author>

<body>

<ul>
<xsp:logic>
<![CDATA[
  my $lastindent = my $indent = 1;
  open F, "find $ROOT -type d | sort |";
  while(<F>) {
    chomp;
    next if m/CVS$/;
    next unless my $fn=hasmenu($_);
    s:/var/www/webcie::;
    my $my_uri = "$_/";
    my $tail = '';
    { 
      my @foo = split '/', $my_uri; # Is there a better way to count slashes?
      $indent = $#foo; 
      $tail = $foo[$indent]; 
    };
# HACK! HACK! HACK! HACK! HACK! 
# Direct Manipulation of the AxKit/XPath Node Set!
    for (;$lastindent < $indent; ++$lastindent)
      { my $elem = XML::XPath::Node::Element->new(q(ul)); $parent->appendChild($elem); $parent=$elem; };
    for (;$lastindent > $indent; --$lastindent) 
      { $parent=$parent->getParentNode; };
# END OF HACK
    my $text = "Missing menu file!";
    if ($fn) {
      my $xp = XML::XPath->new(filename => $fn);
      $text = $xp->find('/menu/item/text()');
      $text = $my_uri unless $text;
    }
]]>
<li>
  <a><xsp:attribute name="href"><xsp:expr>$my_uri</xsp:expr></xsp:attribute>
    <xsp:expr>$text</xsp:expr>
  </a>
</li>
<![CDATA[
  }
  close(F);
]]>
</xsp:logic>
</ul>

</body>

</webpage>
</xsp:page>
