#!/usr/local/bin/perl
#ci version 0.1  part of the WN server package
#Usage: indexmaker [-t title] [-d directory] [-o outfile]

$VERSION = "0.1";

	require "getopts.pl";
	$title="Index";

	$outfile = "docindex.html";

	&Getopts('d:o:t:');
	$words = shift;
	$outfile = $opt_o if $opt_o ne "";
	$dir = $opt_d if $opt_d ne "";
	$title = $opt_t if $opt_t ne "";

	if ( $dir ne "") {
		$x = substr( $dir, -1);
		$dir = $dir."/" if $x ne "/";
	}
	open( WORDS, "<$words") || die "Can't open file: $!";
	open( OUT, ">$outfile") || die "Can't open file: $!";

	printf( OUT "<html>\n<head>\n<title>%s</title>\n</head>\n", $title);
	printf( OUT "<body>\n<!-- pnuts -->\n<h2>%s</h2>\n", $title);

	$firstlet = "A";
	while ( $curword = <WORDS> ) {
		chop( $curword);
		$curword =~ s/^[^a-zA-Z]*//;
		$curword =~ s/[<>]//g;
		$query = $curword;
		$query =~ s/\s/\+/g;
		$firstlet = $curword;
		$firstlet = substr( $firstlet, 0, 1);
		$firstlet =~ y/a-z/A-Z/;
		if ( $firstlet ne $prevlet) {
			$firstlet =~ /^A$/ || printf( OUT "</ul>\n");
			printf( OUT "<ul><b>-- %s --</b><p>\n", $firstlet);
			$prevlet = $firstlet;
		}
		printf( OUT "<li> <a href=\"%ssearch=context?%s\">",
				$dir, $query);
		printf( OUT "%s</a>\n", $curword);
	}
	printf( OUT "</ul><p>\n<!-- pnuts -->\n</body>\n</html>\n");
	close( WORDS);
	close( OUT);
