#!/usr/bin/perl
#
#  finalfilter - do final filtering of a LaTeX document and its associated 
#                files.  This is usually run just prior to the last latex
#                execution.  
#
#  Given a base file name for a LaTeX document (e.g. adass98 for adass98.tex),
#  this script will look for special filter files to apply to the document
#  and selected auxillary files--i.e., those with the same base name but
#  with the extensions, .tex, toc, .and, .snd, and .ind.  The filter file
#  searched for a given extension file has a name of the form, BASE_EXT.fltpl
#  (e.g. for adass98.toc, the filter file is adass98_toc.fltpl).  This type
#  of filtering is a convenient way to "tweak" intermediate files
#  that are created "on-the-fly" as part of the LaTeXing process before they 
#  are applied to produce the final .dvi output.
#
#  The filter file is a perl script written as if it were run with the 
#  perl option -p; that is, the filter file is applied to each line in the 
#  input file.  (See adass98_toc.fltpl for an example filter file.)
#
#  Options:
#    -v       verbose mode
#    -q       quiet mode
#    -r       replace input file with filtered version
#
#  written by Raymond Plante for ADASS VIII
#
($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";

@exts = ("toc", "and", "snd", "ind");
$filtfile = '';

# process switches
&Getopts('qsvrCd:m:f:F:');
$verbose = $true if ($opt_v);
$quiet = $true if ($opt_q);
$silent = $quiet = $true if ($opt_s);
$NoComments = $true if ($opt_C);
$replace = $true if ($opt_r);
$modifier = $opt_m if ($opt_m);
$filtext = $opt_f if ($opt_f);
$filtfile = $opt_F if ($opt_F);

$modifier = "$$" if ($replace);
$writeToFile = ($opt_d || $opt_m || $replace);

&fatal("No document file given") if (@ARGV == 0);

$doc = shift @ARGV;
($base = $doc) =~ s/\.[^\.]*$//;
$doc .= ".tex" if ($doc !~ /\./);
&warn("$doc: file not found") if (! -f "$doc");

&filterFile($doc, $base, '');

foreach $ext (@exts) {
    &filterFile("$base.$ext", $base, $ext);
}

exit(0);

sub filterFile {
    local($file, $base, $ext) = @_;
    local($filters, $ifile, $oext, $ofile);
    local($btin, $btout, $stat);
    local($filterfile) = $base;
    $filterfile .= "_$ext" if ($ext ne '');
    $filterfile .= ".fltpl";
    if (! -f "$filterfile") {
	&inform("$filterfile: No filters found for $file; skipping...") 
	    if ($verbose);
	return;
    }

    &inform("Filtering $file...");

    open(FILT, "$filterfile") || &fatal("$filterfile: unable to read filter");
    $filters = join('', grep(!/^\s*\#/ && !/^\s*$/, <FILT>));
    close(FILT);

    ($ifile = $file) =~ s!^.*/!!;
    open(INTEX, "$file") || &fatal("$file: $!");

    if ($writeToFile) {
	&fatal("$opt_d: No such directory with write permission") 
	    if ($opt_d && (! -d $opt_d || ! -w $opt_d));
	($oext) = ($ifile =~ /(\.[^\.]+)$/);
	($ofile = $ifile) =~ s/\.[^\.]+$//;
	$ofile = "$opt_d/$ofile" if ($opt_d);
	$ofile .= "_$modifier" if ($opt_m || ! $opt_d);
	$ofile .= $oext;

	open(OUTTEX, ">$ofile") || &fatal("$ofile: $!");
	select(OUTTEX);
    }

    while (<INTEX>) {

	eval $filters;

	print;
    }

    close(INTEX);
    close(OUTTEX) if ($writeToFile);

    if ($replace) {
	$btin = "mv -f $ofile $file";
	inform($btin) if ($verbose);
	$btout = `$btin 2>&1`;  $stat = $?/256; chop $btout;
	&fatal("Failed to overwrite $file ($btout)\n  Result is in $ofile")
	    if ($stat);
#	$btin = "chmod a-w $file";
#	inform($btin) if ($verbose);
#	$btout = `$btin 2>&1`;  $stat = $?/256; chop $btout;
    }
}
