#!/usr/bin/perl
#
# This creates a HTML 'map' of a web site.
#
# This software is copyright (C) 1995, 1996 by Joey Hess <joey@kitenet.net>
# May be distributed under the terms of the GNU public license, version
# 2 or higher.

#Pages under the
$topdir='/home/pub/'; #directory are indexed.

#They have to end in 
$extention=".html"; #or they won't be shown at all

#And they can't be in a directory that matches this regexp:
$exclude="include|dwww|auto";

BEGIN { push (@INC,'include/') }
require 'cgi-lib.pl';
print &PrintHeader;

print "<html><title>Everything At Kite</title><body><h1>Everything At Kite</h1>\n";
print "<UL>\n";

#Display a page.
sub ShowPage { my $dir=shift; my $file=shift; my $infront=shift;
	open (IN,'<'.$topdir.$dir."/".$file);
	my $title=$file;
	while (<IN>) {
		($t)=m/\<title\>(.*)\<\/title\>/i;
		if ($t) {
			$title=$t;
			close IN;
		}
	}
	close IN;
	
	print "$infront<A HREF=\"$dir/$file\">$title</A>\n";
}

#Index the passed directory
sub IndexDir { my $dir=shift;
	opendir(DIR,$topdir.$dir);
	my @files;
	while ($_=readdir(DIR)) {
		$files[$#files+1]=$_;	
	}
	close DIR;
	my $didindex=undef;
	if ((-e $topdir.$dir.'/index.html') and ($dir=~/$exclude/i eq undef)) {
		&ShowPage($dir,'index.html','<LI>');
		print '<UL>';
		$didindex=1;
        }
	foreach $fn (@files) {
	  	if ((-d "$topdir$dir/$fn/") && ($fn ne '.') && ($fn ne '..') && ($fn=~/$exclude/i eq undef)) {
			$didindex=IndexDir("$dir/$fn") || $didindex;
	  	}
	  	elsif ((!-d $fn) and ($fn ne 'index.html') and ($fn=~/$extention$/ ne undef ) and ($dir=~/$exclude/i eq undef)) {
			if (!$didindex) { print "<LI>$dir\n<UL>"; $didindex=1 }
  			&ShowPage($dir,$fn,'<LI>');
	  	}
	}
	print "</UL>\n" if $didindex;
	return $didindex;
}

&IndexDir();
print "</UL>\n";
&PrintFooter;
print "</body></html>\n";
