#!/usr/bin/perl -w
#
# make-howtoindex --- generate an index.html for the doc-linux package
#
# Copyright 1997 Dirk Eddelbuettel <edd@debian.org>. Released under the GPL.
#
# $Id: make-howtoindex,v 1.7 1998/03/19 03:37:15 edd Exp $

use strict;
use English;
use vars qw($opt_h $opt_i $opt_o $opt_v $opt_u $opt_t);
use Getopt::Std;

my $out = "index.html";
my $now = localtime;
$PROGRAM_NAME =~ s|.*/||;

sub write_header() {
    print OUT <<EOF;
<html>
<head>
<title>Debian GNU/Linux HOWTO Document Index</title>
</head>

<body>
<h1>Debian GNU/Linux HOWTO Document Index</h1>   

EOF
}

sub write_footer() {
    print OUT <<EOF;
<hr>
<address>
<small>
Please send comments to <a href="mailto:edd\@debian.org">
Dirk Eddelbuettel</a>.<br>
This page was created automatically at $now 
by the script $PROGRAM_NAME.
</small>
</address>
</body>
</html>       
EOF
}

sub write_table_header() {
    print OUT "<table><tr>\n";
}

sub write_table_footer() {
    print OUT "</tr></table>\n";
}

sub write_main($$$) {
    my ($dir,$stub,$title) = @_;

    opendir DIR, $dir or die "Cannot open $dir: $!\n";
    print OUT "<td valign=top><dl>\n";
    print OUT "<h2>$title</h2>\n" ;
    my @files = grep {! /^\./  && -f "$dir/$ARG" } readdir(DIR);   
    my ($file,$name,$header);
    foreach $file (sort @files) {
	($name = $file) =~ s/\.gz$//;
	print OUT "<a href=\"$stub$file\">$name</a><br>\n";
	print "Processing $file\n" if $opt_v;
    }
    print OUT "</dl></td>\n";
    closedir DIR;
}

sub show_usage() {
    print <<EOF

Debian GNU/Linux $PROGRAM_NAME.   Copyright (C) 1997 Dirk Eddelbuettel.
This is free software; see the GNU General Public Licence version 2 
or later for copying conditions.  There is NO warranty.

Usage: $PROGRAM_NAME [options ...] dir1 dir2 ...

Options:  -o<output_file>   	write to this file ($out)
	  -h                	show this help
          -v                	be verbose

Examples:

       $PROGRAM_NAME /path/to/howto/dir /another/path

EOF
}

getopts('ho:v');

show_usage if ($opt_h);
$out = $opt_o if ($opt_o);

open OUT, ">$out" or die "Cannot open $out: $!\n";
print "Writing to file $out\n" if $opt_v;
write_header;
my ($i,$dir,$subdir);
for ($i=0; $i<=$#ARGV; $i++) {
    $dir = $ARGV[$i];
    print "Reading from directory $dir\n" if $opt_v;
    if ($dir =~ m|^.*/HOWTO/(.*)$|) {
	$subdir = $1 . "/";
    } else {
	$subdir = "";
    }
    my $title = "Documents in the directory /usr/doc/HOWTO/" . $subdir;
    write_table_header;
    write_main($dir, $subdir, $title);
    write_table_footer;
}
write_footer;
print "Done.\n" if $opt_v;
close OUT;







