#!/usr/local/bin/perl
#
# index-events - make an & index topic for events/news/help
#
# Called by compose-tricky
#
# Take a MUSH help.txt format file on the stdin, and write a
# "& index" entry or entries.
# Lines with entries to be indexed start with &'s.
# Write the resulting entries to the stdout, also in help.txt format,
# in columns and paged.
# Idea by Schmidt@Dune, perl version by Alan Schwartz (Javelin/Paul)
#
# Usage: index-events < news.txt > index.news
#

# Collect all the topic names
while (<STDIN>) {
   /^&\s+(.*)\s*\n/ && push(@entries,$1);
}

# Make 'em all lower-case and sort 'em.
grep(tr/A-Z/a-z/,@entries);
@sorted = sort @entries;

$= = 99999;	# Lines on a page
$- = 99999;	# Lines remaining - start at top of form
$i = 0;
$titlecount = 1;
$header = 1;
$maxlines = 17;
$maxlen = 25;
$separator ="-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";

while (@sorted) {
  if ($header) {
    print STDOUT ($titlecount == 1) ? "& Index\n" : "& Index-${titlecount}\n";
    print $separator;
    $header = 0;
  }
  $entry1 = shift(@sorted);
  $entry2 = shift(@sorted);
  if (length($entry1) > $maxlen) {
    if (length($entry2) > $maxlen) {
      print $entry1,"\n";
      unshift(@sorted,$entry2);
    } else {
      printf "%-52s%s\n", $entry1, $entry2;
    }
  } else {
    if (length($entry2) > $maxlen) {
      print $entry2,"\n";
    } else {
      $entry3 = shift(@sorted);
      if (length($entry3) > $maxlen) {
        unshift(@sorted,$entry3);
        $entry3 ="";
      }
      write;
    }
  }
  if ($i++ > $maxlines)  {
    $titlecount++;
    print "\nFor more, see events Index-${titlecount}\n";
    print $separator;
    $header = 1;
    $i = 0;
  }
}
print $separator;
format STDOUT =           
@<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<
$entry1                   $entry2                   $entry3
.

