#!/usr/athena/bin/perl

$max_errors = 5;

%lines = ();

$| = 1;
undef $/;

$input = <>;

# Put separators in between the records, and then use split to split
# based on the separators.  This is the clearest way to do things,
# since split deletes the strings matching the delimiters.
$input =~ s/\n([^ \t])/\n--\n$1/g;

$* = 1;
@records = split(/^--\n/, $input);
$* = 0;

foreach $record (@records) {
    $recnum++;
    if ($record !~ /^[^ \t]+\t.*\n([ \t]+.*\n)*\t([^ ,]+,)*[^ ,]+$/) {
	warn "Badly formatted index file $ARGV, record number $recnum, containing:\n$record";
	$errors++;
	die "Too many errors ($max_errors) in $ARGV; aborting.\n"
	    if ($errors == $max_errors);
    }
    ($archive_name, $rest) = split(/\t/, $record, 2);
    $archive_name =~ s/\s+$//;
    $archive_name =~ s/\.Z$//;
    $archive_name =~ tr/A-Z/a-z/;
    $lines{$archive_name} = $record;
}

foreach $name (sort keys %lines) {
    print "$lines{$name}" || die;
}

die "Errors ($errors) in $ARGV; aborting.\n" if ($errors);
