#!/usr/athena/bin/perl -lw

use strict;
use IO::File;

my $head = uc shift or die "no leading series specified";
my $tail = uc shift or die "no trailing series specified";

my $start = $head;
my $stop = $tail . chr(127);  # so if $stop is "FOO" then we *include* FOOXYZ

my $punt = join "|",
  map { $_ . '\b' }
  grep { $_ ge $start and $_ le $stop and print "punting $_ series" }
  ("STAR TREK", "STAR WARS", "DOCTOR WHO", "PERRY RHODAN", "PHENWICK");
$punt = qr/^($punt)/ if $punt;


do "./dexmaster-current";
undef $@;

print "Loading in datadex...";
loaddex ('datadex', 'main') || die "Cannot open datadex"; 

print "Hacking at dex...";
use vars qw(%dex);
my @hackdex;
for my $bk (@{$dex{main}}) {
  my @keepers = grep { 
    my $ser = $_; 
    $ser =~ tr/A-Z //cd; 
    $ser ge $start and $ser le $stop and not ($punt and $ser =~ /$punt/);
  } split /\|/, $$bk{series};
  next unless @keepers;
  my $hack = { %$bk };
  $hack->{series} = join '|', @keepers;
  push @hackdex, $hack;
}

prettydex (-books => \@hackdex, -by => "series",
	   -tex => "gap-seriesdex:$head-$tail.tex",
	   -supple => "Gapdexing Supplement, \\today");
exit 0;
