#! /usr/bin/perl -s
#
# This creates an index page for the man pages of a given chapter.
# Usage:
#     ls -1 file_list | mkmandex -sSection -name="maintainter name" \
#         -address="maintainer email address"
#
# Devin Reade, November 1997.
#
# $Id: mkmandex,v 1.1 1997/11/24 05:07:27 gdr Exp $
#

# initialize and check usage
$sec='';
defined($s1)  && ($num = '1')  && ($sec = "Commands and Applications");
defined($s2)  && ($num = '2')  && ($sec = "System Calls");
defined($s3)  && ($num = '3')  && ($sec = "Library Routines");
defined($s3f) && ($num = '3f') && ($sec = "Fortran Routines");
defined($s4)  && ($num = '4')  && ($sec = "Devices");
defined($s5)  && ($num = '5')  && ($sec = "File Formats");
defined($s6)  && ($num = '6')  && ($sec = "Games");
defined($s7)  && ($num = '7')  && ($sec = "Miscellaneous");
defined($s8)  && ($num = '8')  && ($sec = "System Administration");

($sec eq '') && die("section number not specified");
defined($name) || die("-name flag not specified");
defined($addr) || die("-addr flag not specified");
defined($hroot) || die("-hroot flag not specified");

# $hdir = $hroot . '/man/man' . $num . '/';
$hdir = $hroot . '/man/';

# print out the head of the page
printf("<html>\n" .
       "<head>\n" .
       "<!-- This document autogenerated from mkmandex -->\n".
       '<!-- $Id: mkmandex,v 1.1 1997/11/24 05:07:27 gdr Exp $ -->' . "\n" .
       "<title>Chapter %s: %s</title>\n" .
       "</head>\n" .
       "<body bgcolor=\#ffffff>\n" .
       "<h1>Chapter %s: %s</h1>\n" .
       "<pre>\n",
       $num, $sec, $num, $sec);

# get the list of files
$maxlen = 0;
while(<>) {
    chop;
    $pagename = $file = $_;
    $pagename =~ s/\.\df?$//;
    $filelist{$pagename} = "man$num/$file";
    $len = length($pagename);
    ($len > $maxlen) && ($maxlen = $len);
}


# add in the appropriate .so links
if (defined($mkso)) {
    open(infp, "< $mkso") || die("couldn't open $mkso");
    while ($_ = <infp>) {
	s/\#.*//;
	(/^\s*$/) && next;

	if (/^\s*(\S+)\s+(\S+)\s*$/) {
	    $real = $1;
	    $link = $2;
	    $link =~ s,.*/,,;
	    if ($link =~ /(\df?)$/) {
		$suffix = $1;
		if ($suffix eq $num) {
#		    if ($real =~ m,(.*)/,) {
#			$prefix = $1;
#			if ($prefix eq "man$num") {
#			    $real =~ s,.*/,,;
#			}
#		    }
		    $pagename = $link;
		    $pagename =~ s/\.\df?$//;
		    $filelist{$pagename} = $real;
		    $len = length($pagename);
		    ($len > $maxlen) && ($maxlen = $len);
		}
	    } else {
		printf(stderr "%s corrupt at line %d (1) real=%s\n",
		       $mkso, $., $real);
	    }
	} else {
	    printf(stderr "%s corrupt at line %d (2)\n", $mkso, $.);
	}
    }
    close(infp);
}


$pagewidth = 70;
$colwidth = $maxlen + 3;
$columns = int($pagewidth / $colwidth);
($columns == 0) && ($columns = 1);

# special-case the introduction page
$intro = 'intro';
if (defined($filelist{$intro})) {
    printf("<A HREF=\"%s%s.html\">%s</A> (Chapter Introduction)\n\n",
	   $hdir, $filelist{$intro}, $intro);
    undef($filelist{$intro});
}

@keylist = sort(keys(%filelist));

# put everything into neat columns
undef(@rows);
$maxrow = 0;
$entries = scalar(@keylist);
$maxrow = int($entries / $columns);
(($entries % $columns) == 0) || ($maxrow++);
while (scalar(@keylist) > 0) {
    for ($i=0; ($i<$maxrow) && (scalar(@keylist) > 0); $i++) {
	$k = shift(@keylist);
	if ($k eq $intro) {
	    $i--;
	    next;
	}
	$padding = $colwidth - length($k);
	$rows[$i] .= '<A HREF="' . $hdir . $filelist{$k} . '.html">' 
	    . $k . '</A>';
	for ($j = 0; $j < $padding; $j++) {
	    $rows[$i] .= ' ';
	}
    }
}

# print them out	
for ($i=0; $i<$maxrow; $i++) {
    printf("%s\n", $rows[$i]);
}


if (0) {

    die('oh shit');
while (scalar(@keylist) > 0) {
    for ($i=0; ($i<$columns) && (scalar(@keylist) > 0); $i++) {
	$k = shift(@keylist);
	$padding = $colwidth - length($k);
	printf("<A HREF=\"%s\">%s</A>", $filelist{$k}, $k);
	for ($j = 0; $j < $padding; $j++) {
	    print ' ';
	}
    }
    printf("\n");
}
}

#    printf("<A HREF=\"%s\">%s</A><br>\n", $_, $_);

# print out the tail of the page
printf("</pre>\n" .
       "<hr>\n".
       "%s\n" .
       "<A HREF=\"mailto:%s\">&lt;%s&gt;</A>\n" .
       "</body>\n".
       "</html>\n",
       $name, $addr, $addr);
