D.2. collate-all

#!/usr/bin/perl -w

use Getopt::Std;

$opt_h	= 0;
$opt_i	= "HTML.index";
$opt_b	= "-Ilocal -Slocal";
$opt_s	= "-Ilocal -Sglobal";
$opt_a	= "-g -f";

$usage =
"Usage: collate-all [-h] [-i index] [-b book-opts] [-s set-opts] [-a opts]

This utility searches the HTML.index file generated by Cogent's customized
.dsl stylesheets for the BOOK keyword, and then generates a set index and
an index for each book that it found, using the -B option in the modified
collateindex.pl.

Options:
    h   print this helpful message
    i   the name of the index file (default is HTML.index)
    b   options for generating book indices (default is \"$opt_b\")
    s   options for generating a set index (default is \"$opt_s\")
    a   options for both books and sets (default is \"$opt_a\")

Example usage:

\$ collate-index -i html/HTML.index
";


die $usage if ! getopts('hi:s:b:a:');

if($opt_h) {
	print $usage;
	exit 0;
}

open(I, "$opt_i") || die "can't open $opt_i: $!";

local(%books);

while(<I>) {
	next unless /^BOOK /;
	chop;
	s/^BOOK //;
	$books{$_} = 1;
}


$" = ", ";

foreach $book (keys %books) {
	$f = $book;
	$f =~s/[ \-\+\(\)\:\'\/\t]+//g; # Get rid of non-alpha charaterss.
        $f =~s/(.{30}?).*/$1/;      # Use only the first 30 characters.
	$f = "index$f.sgml";

	print "Collating index for \"$book\", output to $f...\n";

 	system("collateindex -B \"$book\" $opt_a $opt_b -o $f $opt_i");
}

print "Processing index for set, output to indexset.sgml...\n";

system("collateindex -x $opt_a $opt_s -o indexset.sgml $opt_i");