#!/usr/bin/perl -w
use strict;

-d "autogen" or mkdir "autogen", 0755;

open IDXALL, ">indxal.htm" or die $!;
print IDXALL <<END;
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0//EN">
<!-- *** This file is auto-generated - edit cavetab.csv instead -->
<html lang="en">
<head>
<title>Loser Plateau area : Cave description index</title>
</head>
<body>
<h1>Kataster Gruppe 1623:<br>Loser Augst-Eck - INDEX</h1>

<!-- <dl compact> -->
<table border=0 frame=0>
END

<>; # eat header line
while (<>) {
   chomp;
   y/\t/ /;
   $_ = "\",$_,\"";
   s/","/\t/g;
   s/\\"/"/g;
   my ($dummy, $kat_num, $kat_status, $ents, $other_number, $file, $name, $x4, $x5) = split /\t/, $_;
   my $number = $kat_num;
   if ($number eq "") {
      $number = $other_number;
      $other_number = "";
   }
   my $e = $ents;
   $e =~ s/ +/&nbsp;/g;
   #print IDXALL "<dt><a name=\"$number\">$number&nbsp;<small>$e</small></a><dd>";
   print IDXALL "<tr><td><a name=\"$number\">$number&nbsp;<small>$e</small></a></td><td>";
   if ($file eq "" && $number !~ m!/!) {
      $file = "autogen/$number.htm";
      open FILE, ">$file" or die $!;
      print FILE <<END;
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html lang=en>
<head>
<title>
END
      if ($kat_num ne "") {
         print FILE "1623:$kat_num\n";
      } else {
         print FILE "$number\n";
      }
      print FILE <<END;
</title>
</head>
<body>
<center><table border=0 width=100%>
<tr><th align=left><font size=+2>$number</font></th>
<th align=center><font size=+2>$name</font></th>
<th align=right><font size=+2>$kat_status</font></th></tr>
</table></center>

Blah blah blah...

<hr>
<!-- LINKS -->
<img alt="&gt;" src="../../icons/lists/0.png">
<!--
<a href="index.htm#$number">Schwarzmooskogel ridge area</a><br>
<img alt="&gt;" src="../../icons/lists/0.png">
-->
<a href="../indxal.htm#$number">Full Index</a><br>
<img alt="&gt;" src="../../icons/lists/0.png">
<a href="../areas.htm">Other Areas</a><br>
<img alt="&gt;" src="../../icons/lists/0.png">
<a href="../index.htm">Back to Expedition Intro page</a>

</body>
</html>
END
      close FILE;
   }
   if ($file ne "") {
      print IDXALL "<a href=\"$file\">";
      my $f = $file;
      $f =~ s/#.*//;
      if (! -f $f) {
         print STDERR "Warning: file `$f' not found\n";
      } else {
         open FILE, $f;
	 my $n;
	 while (<FILE>) {
	    if (m!<font size=\+2(?: lang=[^>]*)?>([^>]*?)(?:&nbsp;|\s)+(?:$number)</font>!i) {
	       $n = $1;
	       last;
	    }
	    if (m!<font size=\+2(?: lang=[^>]*)?>([^>]*?(?:h&ouml;hle|schacht|shaft))</font>!i) {
	       $n = $1;
	       last;
	    }
	    if (m!<th align=center><font size=\+2(?: lang=[^>]*)?>([^>]*?)</font>!i) {
	       $n = $1;
	       last;
	    }
	    if (m!<th align=center>([^>]*?)</th>!i) {
	       $n = $1;
	       last;
	    }
	 }
	 close FILE;
	 if (!defined $n) {
	    print STDERR "no name found in $f for $number\n";
	 } else {
	    my $n2 = $n;
	    my $name2 = $name;
	    $n2 =~ s/ \(.*\)$//;
	    $name2 =~ s/ \(.*\)$//;
	    $n2 =~ s/ CUCC\b//;
	    $name2 =~ s/ CUCC\b//;
	    if ($name2 =~ m!^\Q$n2\E(?:[ /]\Q$number\E)?$!i) {
	       #print STDERR "OK $number $n\n";
	    } else {
	       print STDERR "BAD $number : `$n' != `$name' in $f\n";
	    }
	 }
      }
   }
   if ($name eq "") {
      $name = "?";
   }
   print IDXALL $name;
   if (length $x4) {
      print IDXALL " ($x4)";
   }
   if (length $other_number) {
      print IDXALL " ($other_number)";
   }
   if (length $file) {
      print IDXALL "</a>";
   }
   if (length $x5) {
      print IDXALL " - $x5";
   }
   #print IDXALL "\n";
   print IDXALL "</tr>\n";
}
print IDXALL <<END;
<!--</dl>-->
</table>
<hr>
<!-- LINKS -->
<img alt="&gt;" src="../icons/lists/0.png">
Back to <a href="../index.htm">CUCC Home page</a><br>
<img alt="&gt;" src="../icons/lists/0.png">
Back to <a href="index.htm">Expedition Intro page</a><br>
<img alt="&gt;" src="../icons/lists/0.png">
<b>Main Indices:</b><br>
<img alt="---&gt;" src="../icons/lists/1.png">
<a href="infodx.htm"><b>Index</b> to Expo</a> information pages<br>
<img alt="---&gt;" src="../icons/lists/1.png">
<a href="areas.htm">Description of CUCC's area</a> and split to subareas<br>
<img alt="---&gt;" src="../icons/lists/1.png">
List of (links to) <a href="pubs.htm">published reports and logbooks</a><br>
<img alt="&gt;" src="../icons/lists/0.png">
<b>Pictures:</b><br>
<img alt="---&gt;" src="../icons/lists/1.png">
<a href="gall0.htm">Text only Index</a><br>
<img alt="---&gt;" src="../icons/lists/1.png">
<a href="gallery/0.htm">Index pages (with thumbnails)</a><br>
<img alt="&gt;" src="../icons/lists/0.png">
Other info:<br>
<img alt="---&gt;" src="../icons/lists/1.png">
Table of <a href="folk/index.htm">members of CUCC expeditions</a> 1976-99<br>
<img alt="---&gt;" src="../icons/lists/1.png">
<a href="others/index.htm">Other groups</a> who have worked in the area.<br>
<img alt="---&gt;" src="../icons/lists/1.png">
<a href="1626/index.htm">Adjacent area 1626</a>

</body>
</html>
END
close IDXALL;