#!/usr/local/bin/perl
#
#  ps2txt - PostScript to ASCII converter
#
#  Uses ps2txt version 2.1 (as posted to alt.sources) as the main 
#  PostScript conversion engine.  This Perl program acts as a pre- 
#  and post- processor to improve the quality of the ps2txt output.
#
$ps2txt_cmd = "ps2txt-2.1";
$tmpfile = "/tmp/ps2txt.$$";
$start = 0;
$dviflag = "";

#
#  Read the PostScript file and strip the Prolog.  Determine if the
#  PostScript was generated by dvips.
#
open(PSOUT, "> $tmpfile") || die "Cannot write $tmpfile";
while (<>) {
	$start = 1 if (/^%%EndProlog/o);
	$dviflag = "-dvi" if (/Creator: dvips/o);
	next if ($start == 0);
	print PSOUT;
}
close(PS);

#
#  Process the filtered PostScript with $ps2txt_cmd and clean up its output.
#  Substitute \ddd characters with correct combinations.
#
open(PS2TXT, "$ps2txt_cmd $dviflag < $tmpfile |") || die "Cannot run ps2txt";
while (<PS2TXT>) {
	next if (/^\n/o);
	chop;
	if (/^.*\\.*$/o) {
		s/\\214/fi/g;
		s/\\256/fi/g;
		s/\\257/fl/g;
		s/\\320//g;
	}
	foreach $word (split) {
		print "$word\n" if ($word ne "");
	}
}
close(PS2TXT);
unlink($tmpfile);
exit(0);
