#!/usr/bin/env perl

use strict;

if (@ARGV != 1) { die "USAGE: mkidx (directory)\n"; }

# input is the name of a directory and current level
# outputs are: directory list, document list, index file for directory
# menu for that directory

# parse input info
my($path);
$path  = $ARGV[0];
$path  =~ s|/*$||; # strip trailing /'s to clean path

local($mkidx::root) = $path;

&mksubidx ($path, 0, 0, "template.htm");
exit 0;

sub mksubidx {
    # USAGE: mksubidx (path) (entry) (template) (index)
    my($path)  	  = shift @_;
    my($level) 	  = shift @_;
    my($entry)    = shift @_;
    my($template) = shift @_;
    my(@updex)    = @_;

    my($i);
    my($line);
    my(@list);
    my($file, $root, $link, $ext);
    my(@dirs)  = ();
    my(@docs)  = ();
    my(@txts)  = ();
    my(@index) = ();

    # if this directory contains a template file, reset entry and level
    # supply the template file to mkhtml here and below
    if (-e "$path/template.htm") {
	@updex = ();
	$level = 0;
	$entry = 0;
	$template = "$path/template.htm";
    }
    # print STDERR "idx: $template\n";

    # put leading portion of updex here
    for ($i = 0; $i < $entry; $i++) {
	push @index, $updex[$i];
    }

    # find all dirs in this directory
    @list = <$path/*>;
    foreach $file (@list) {
	if (! -d $file) { next; }
	$root = &basename ($file);
	$link = $file;
	$link =~ s|^$mkidx::root|ROOT|;

	# list special directories here
	if ($root eq "CVS") { next; }
	if (-e "$file/noindex.idx") { next; }

	push @dirs, $file;

	$line = sprintf "<tr><td class=dir%d> <a class=dl %-30s -%20s </a></td></tr>", $level, "href=$link>", $root;
	push @index, $line;
    }

    # find all docs (htm, txt) in this directory
    @list = (<$path/*.htm>,<$path/*.txt>);
    foreach $file (@list) {
	if (! -f $file) { next; }

	$ext = &extname ($file);
	$root = &rootname ($file);

	$link = $file;
	$link =~ s|.$ext$|.html|;
	$link =~ s|^$mkidx::root|ROOT|;

	# list special files here
	if ($root eq "index") { next; }
	if ($root eq "template") { next; }

	# print STDERR "ext: $ext, root: $root\n";

	push @docs, $file;

	# simple case: no extra information:
	$line = sprintf "<tr><td class=doc%d> <a class=dl %-30s  %20s </a></td></tr>", $level, "href=$link>", $root;
	push @index, $line;
    }

    # add trailing portion of updex here
    for ($i = $entry; $i < @updex; $i++) {
	push @index, $updex[$i];
    }

    for ($i = 0; $i < @dirs; $i++) {
	&mksubidx ($dirs[$i], $level + 1, $i + $entry + 1, $template, @index);
    }

    # output index for this directory
    open (FILE, ">$path/index.idx");
    foreach $line (@index) {
	print FILE "$line\n";
    }
    close (FILE);

    # create html for docs in this directory
    foreach $file (@docs, "$path/index.htm") {
	$ext  = &extname ($file);
	# print STDERR "ext: $ext\n";
	# $root  = &rootname ($file);
	# print STDERR "root: $root\n";
	$link = $file;
	$link =~ s|.$ext$|.html|;
	system "mkhtml -template $template $mkidx::root $file $link";
    }
    return;
}

# behaves just like system basename function (returns last portion of path)
sub basename {

    my ($file) = $_[0];
    my ($base, @words);

    @words = split ("/", $file);
    $base = $words[-1];
    return $base;
}

# behaves just like system basename function (returns last portion of path)
sub extname {
    my ($file) = &basename ($_[0]);
    my($ext) = $file =~ m|\S*\.(\S*)$|;
    return $ext;
}

# behaves just like system basename function (returns last portion of path)
sub rootname {
    my ($file) = &basename ($_[0]);
    my($ext) = $file =~ m|(\S*)\.\S*$|;
    return $ext;
}

# behaves just like system dirname function (strips last portion of path)
sub dirname {

    my ($file) = $_[0];
    my ($base, @words);

    @words = split ("/", $file);
    if (@words == 1) { return "."; }

    pop @words;
    $file = join ('/', @words);
    return $file;
}

###
#    # find all txt files in this directory
#    @list = <$path/*.txt>;
#    foreach $file (@list) {
#	if (! -f $file) { next; }
#
#	$ext = &extname ($file);
#
#	$root = &basename ($file);
#	$root =~ s|.txt$||;
#	$link = $file;
#	$link =~ s|.txt$|.html|;
#	$link =~ s|^$mkidx::root|ROOT|;
#
#	push @txts, $file;
#
#	# simple case: no extra information:
#	$line = sprintf "<tr><td class=doc%d> <a class=dl %-30s  %20s </a></td></tr>", $level, "href=$link>", $root;
#	push @index, $line;
#    }

