#!/bin/perl -w
# $Id: modinfo,v 2.9 1998/11/11 03:25:45 hasegawa Exp $
# copyright (c)1997-1998 Yoshinori Hasegawa <hasegawa@madoka.org>

&main(@ARGV);

sub main {
  local(@args) = @_;
  local($dir);
  if (!@args) {
    &usage();
    exit(1);
  }
  $dir = shift(@args);
  &traverse("$dir/module", '');
}

sub traverse {
  local($base, $dir) = @_;
  local(@files, $file);
  opendir(DIR, "$base/$dir") || die;
  @files = readdir(DIR);
  closedir(DIR);
  foreach $file (sort(@files)) {
    next if $file =~ /^\./;
    next if $file eq 'SCCS';
    next if $file eq 'RCS';
    next if $file eq 'CVS';
    next if $file =~ /\,v$/;
    if (-d "$base/$dir/$file") {
      if ($dir) {
        &traverse($base, "$dir/$file");
      } else {
        &traverse($base, $file);
      }
    } elsif ($file =~ /\.plm/) {
      if ($dir) {
        print "<DT><A HREF=\"../module/$dir/$file\">$dir/$file\n";
        print "</A></DT>";
        &info("$base/$dir/$file", "$dir/$file");
      } else {
        print "<DT><A HREF=\"../module/$file\">$file\n";
        print "</A></DT>";
        &info("$base/$file", $file);
      }
    }
  }
}

sub info {
  local($file, $name) = @_;
  if (open(FILE, $file)) {
    while (defined($line = <FILE>)) {
      $line =~ tr/\r\n//d;
      last if $line eq '__END__';
    }
    while (defined($line = <FILE>)) {
      $line =~ tr/\r\n//d;
      if ($line =~ /^\s*$name\s*\-\s*(.*)$/) {
        print "<DD>    $1\n";
        print "</DD>\n";
      }
    }
    close(FILE);
  }
}

sub usage {
  print 'usage: perl modinfo <plum-top-directory>', "\n";
}
