#!/local/perl/bin/perl -w
#
#  Create a .num and .idx file for a list of forune quotes
#
#  Modify so that the number of fortunes is first
#  (Overwrite the first entry w/ a long containing the number of
#  quotes then modify the code so that if the first quote is choosen
#  it seeks to 0, which is always were the first quote will be)
#

($qtsfile = shift @ARGV) || die "Usage: stfile quotefile [idxfile]\n";
$idxfile = (shift @ARGV || $qtsfile . ".idx");

#$qtsfile = "/src/1/misc/fortune/qotd/fortunes";
#$idxfile = "fortunes.idx";

open(QTS,"<$qtsfile") || die "Can't open $qtsfile: $!\n";
open(IDX,">$idxfile") || die "Can't open $idxfile: $!\n";

binmode IDX;

$curr = 1;

print IDX pack("L",tell(QTS));

while (<QTS>) {
  if (m/^%/o) {
      print IDX pack("L", tell(QTS));
      $curr++;
  }
}

print STDERR "$curr quotes indexed\n";
seek (IDX, 0, 0);
print IDX pack("L",$curr);

close(IDX);
close(QTS);
