#!/afs/athena/contrib/perl5/perl -w

# Possibly applicable copyrights for various sections of this script
# are appended at the end of this file.

use strict;
use POSIX qw( getcwd chdir );

print getNAME(@ARGV);
exit 0;

sub usage {  die "usage: $0 man-directory\n";  }

$0 =~ s@.*/@@;
(@ARGV == 1) || usage();

my ($mandir) = @ARGV;

(-d $mandir) || die "$0: '$mandir' is not a directory\n";
chdir($mandir);
$mandir = getcwd();

my ($names, $i) = '';
for $i (<man?*>) {
  (-e $i) || die "$0: entry for '$i' disappeared?";
  (-d $i) || next;
  chdir($i) || die "$0: can't cd to $i";
  my (@files) = <*>;
  $names .= `/usr/lib/getNAME @files` if @files;
  chdir($mandir) || die "$0: can't cd to $mandir";
}

print $names;

#------------------------------------------------------------------------------

package getNAME;
# Re-implements the functionality of /usr/lib/getNAME from BSD distribution.
# Doesn't currently correctly implement the "-t" and "-i" options, however.

use strict;
use vars qw( $tocrc $intro );
use Carp;
use FileHandle;

sub getfrom {
  my ($file) = @_;
  my $fh = new FileHandle $file, 'r'
    or croak "can't open $file ($!)";

  my ($TH, $SH, @desc);

  1 while chomp($TH = <$fh>) && ($TH !~ /^\.TH/i);
  return unless $TH;

  1 while chomp($SH = <$fh>) && ($SH !~ /^\.SH/i); 
  return unless $SH;
  push @desc, $SH
    while chomp($SH = <$fh>) && ($SH !~ /^\.SH/i); 

  if (wantarray) {
    ($TH, join(' ', @desc));
  } else {
    $TH . "\t" . join(' ', @desc);
  }
}

sub main::getNAME { 
  if (wantarray) {
    map getfrom($_)."\n", @_;
  } else { 
    join '', map getfrom($_)."\n", @_;
  }
}

__END__

#------------------------------------------------------------------------------

sed  </tmp/whatisx.$$ \
	-e 's/\\-/-/' \
	-e 's/--/-/' \
	-e 's/\\\*-/-/' \
	-e 's/ VAX-11//' \
	-e 's/\\f[PRIB0123]//g' \
	-e 's/\\s[-+0-9]*//g' \
	-e '/ - /!d' \
	-e 's/.TH [^ ]* \([^ 	]*\).*	\(.*\) -/\2 (\1)	 -/' \
	-e 's/	 /	/g' >/tmp/whatis$$

awk '{	title = substr($0, 1, index($0, "- ") - 1)
	synop = substr($0, index($0, "- "))
	count = split(title, n, " ")
	for (i=1; i<count; i++) {
		if ( (pos = index(n[i], ",")) || (pos = index(n[i], ":")) )
			n[i] = substr(n[i], 1, pos-1)
		if ( n[i] !~ /^ *$/ )
			printf("%s\t%s %s\t%s\n", n[i], n[1], n[count], synop)
	}
}' </tmp/whatis$$ >/tmp/windex$$

$expand -24,28,32,36,40,44,48,52,56,60,64,68,72,76,80,84,88,92,96,100 \
	/tmp/whatis$$ | sort | $unexpand -a > whatis
$expand -16,32,36,40,44,48,52,56,60,64,68,72,76,80,84,88,92,96,100 \
	/tmp/windex$$ | sort | $unexpand -a > windex

chmod 644 whatis >/dev/null 2>&1
chmod 644 windex >/dev/null 2>&1
rm -f /tmp/whatisx.$$ /tmp/whatis$$ /tmp/windex$$
exit 0

# Possibly applicable copyrights on sections of this script:
#
#	Copyright (c) 1980 Regents of the University of California.
#	All rights reserved.  The Berkeley software License Agreement
#	specifies the terms and conditions for redistribution.
#
#	(c) 1986,1987,1988,1989  Sun Microsystems, Inc
#	(c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
#	          All rights reserved.
#
#	Copyright (c) 1985 by Digital Equipment Corporation, Maynard, MA
#	All rights reserved.
#
#	Copyright (c) 1997   Albert Dvornik   <bert@mit.edu>
#		Massachusetts Institute of Technology

