#!/usr/bin/perl
#
# PROGRAM NAME: make_contents.perl
#
# LOCATION: /servers/www/labdocs.documents/
#
# Comments:
# This script will create a webpage showing a table of contents for
# all the labdocs
#
# Updated by Roger.Mamer@sonoma.edu
# Updated by Richard Gordon 3/25/98
# ------------------------------------------------------------------------

#
# First lets go the the lab documents directory!
#
chdir("/servers/www/labdocs/documents") || die "ERROR: Can not find lab
documents Directory!";

# Print the top part of the web page
print <<EOF
<HTML>
<HEAD>
<!-- Contents page created by make_contents.perl -->

<TITLE>SSU CS Department Documentation Project</TITLE>

</HEAD>
<P>
<H2> Documents </H2>
EOF
;

open(IN, "find ./ -type f -print | sort |");
while (<IN>){
	# remove line terminator
	chomp;

	# the filename is the last component of the path and keeps its spaces
	@pathcomponents = split("/");	
	$filename = $pathcomponents[$#pathcomponents];

	# replace spaces with %20 for html paths
	while (/\x20/) {
		s/\x20/%20/;
	}

	# remove leading .
	s/^\.//;

	# separate out each component of the path
	#@pathcomponents = split("/");	

	$fullpath = $_;

	# ignore filenames that begin or end with ~
	if (($filename !~ /^~/) && ($filename !~ /~$/)) {

		# find the first component of the path that differs from the previous one
		# and indent in proportion to the number of components
		for ($i = 1; $i <= $#pathcomponents; $i++){
			if ($oldpathcomponents[$i] ne $pathcomponents[$i]){
				for ($j = 0; $j <= $i-1; $j++){
					print"&nbsp; &nbsp; &nbsp;";
				}

				# If the mismatch comes before the last component it is not a link, just a different directory
				if ($i ne $#pathcomponents) {
					print "$pathcomponents[$i]<BR>\n";
				}
				else {
					print "<A HREF=\"http://cs.sonoma.edu/labdocs/documents$fullpath\">$filename</A> <BR>\n";
				}
			}
		}
		@oldpathcomponents=@pathcomponents;
	}
}       

# Print the bottom part of the web page
print <<EOF
</BODY>
</HTML>
EOF
;
