#!/usr/bin/perl
#
#  mkabstracts - create abstracts in ADS input format
#
($prog = $0) =~ s!.*/!!;
$ENV{'PERL_LIB'} = "$ENV{'HOME'}/usr/lib/perl" 
    if ($ENV{'PERL_LIB'} eq '');

unshift(@INC, ".", $ENV{'PERL_LIB'});
require "getopts.pl";
require "texscan.pl";
require "error.pl";
require "readadb.pl";
require "readabstract.pl";

$warn_sub = "warn";
@exclude_papers = ();
@include_papers = ();
$includeDir = "filtered";
%prevsect = ();
%partsect = ();
$delim = "><";
$extra_cmds = "\\paspreprint";

# process switches
&Getopts('qsvXD:d:E:');
$verbose = $true if ($opt_v);
$quiet = $true if ($opt_q);
$silent = $quiet = $true if ($opt_s);
$exclude = $true if ($opt_X);
$includeDir = $opt_D if ($opt_D);
$dbfile = $opt_d;
$extra_cmds = $opt_E if ($opt_E);
@files = @ARGV;

&fatal("No .adb file given") if ($dbfile eq '');
$dbfile .= ".adb" if ($dbfile !~ /\.[^\.]*$/);

# read the db file once through to pick up all the labels
open(INDB, "$dbfile") || &fatal("$dbfile: $!");
while (&readNextSect("INDB")) { }
close(INDB);

# read it a second time to write out the abstracts
open(INDB, "$dbfile") || &fatal("$dbfile: $!");
while (&readNextSect("INDB")) {
    next if ($data{"set"} eq '');

    if ($data{"section"} eq "article") {

        # remove LaTeX hard breaks from author list
        $author = $data{'author'};
        $author =~ s/\\nobreakspace//g;
        $data{'author'} = $author;

        $data{"abstract"} = &readAbstract( $data{'filename'}.'.tex' );
        &writedata(*data);
    }

}

exit(0);

sub dowrite {
    local(*dat) = @_;

    return $false if ($dat{"section"} ne "article");
    return (grep(/^$dat{"filename"}$/, @files) == (! $exclude)) 
	if (@files > 0);

    return $true;
}

sub writedata {
    local(*dat) = @_;
    local($tmp);
    local($filename) = $dat{"filename"};
    #$filename =~ s/\.[^\.]*$//;
    #$filename .= ".abs" if ($filename !~ /\.[^\.]*$/);
    $filename .= ".abs";

    &inform("Writing $filename...") if ($verbose);

    local(@extlabels) = split(/$delim/, $dat{"extlabels"});

    open(AUX, ">$filename") || &fatal("$filename: $!");
    print AUX "%T $dat{'title'}\n";
    print AUX "%A $author\n";
    print AUX "%P $dat{'page'}\n";
    print AUX "%B $dat{'abstract'}";
    close(AUX);
}
