#!/usr/bin/perl
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#NAME
#  addpdf - Add a .pdf entry to .ps makefile entries
#
#SYNOPSIS
#  addpdf [file]..
#
#DESCRIPTION
#  This is a bit of a kludge. It reads from stdin and copies most of the data
#  to stdout.  While doing this, it looks for makefile lines of the form:
#     foo.suf: ... foo.ps
#  When it finds one, it rewrites the line as:
#     foo.suf: ... foo.ps foo.pdf
#  Also, at the end of the makefile chunk, it adds the lines:
#     foo.pdf: foo.ps
#      ps2pdf foo.ps
#  The result is that makefile chunks that create .ps files now  also  create
#  .pdf files, using the ps2pdf command.
#
#OPTIONS
#
#EXAMPLES
#
#FILES
#
#BUGS
#
#SEE ALSO
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;
$exitstat = 0;
($P = $0) =~ s".*/"";
$V = $ENV{"V_$P"} || $ENV{"D_$P"} || 1;	# Verbose level.

$inchunk = 0;	# True while within a chunk
$gotps   = 0;	# True if chunk contains target line

while ($line = <>) {
	$line =~ s/[\r\s]+$//;		# Trim the lines of any white stuff
	if ($line eq '') {
		if ($gotps) {
			print "$base.pdf: $base.ps\n";
			print "\tps2pdf $base.ps\n";
			$gotps = 0;
			$base = $suff = '';
		}
		$gotps = $inchunk = 0;
		$label = '';
	} elsif ($line =~ /^[^.:]+:.*\s([-+._\w]+)\.ps\s*$/) {
		$base = $1;
		$gotps = 1;
		$inchunk = 1;
		$line .= " $base.pdf";
	} elsif ($line =~ /^([^:]+)\.ps:/) {
		$label = uc(substr($1,0,8));
		print "# label='$label'\n" if $V>2;
	} elsif ($line =~ s/	\|PSftr /	|PShdr "$label" "" "$label" |PSftr /) {
	} else {
		$inchunk = 1;
	}
	print "$line\n";
}
if ($inchunk && $gotps) {
	print "$base.pdf: $base.ps\n";
	print "\tps2pdf $base.ps\n";
#	$gotps = 0;
#	$base = $suff = '';
}

print "$P: Exit with status $exitstat.\n" if $V>1;
exit $exitstat;

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
