#! /usr/bin/perl -w
# code_print uses TeX, eplain, and dvips to print sources
# Copyright (C) 1999 Ed Cashin
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# 
use strict;
use Getopt::Std;
use vars qw($opt_h $opt_p);

$main::max_line_length = 78;

getopts('hp');
if ($opt_h) {
    &usage;
    exit;
}

foreach (@ARGV) {
    if (-f $_) {
	&code_print($_);
    } else {
	print "$_ doesn't look like a regular file.\nskipping $_\n";
    }
}

sub code_print {
    die "Error: parameter missing" if @_ < 1;
    my $filename = $_[0];
    chomp(my $orig_cwd = `pwd`);

    my @lines		 = @{ &parse_file($filename) };
    chdir "/tmp";
    my $tmp_file	 = &tmp_file(\@lines);
    my $tmp_file_base	 = $tmp_file;
    $tmp_file_base =~ s/\.tex$//;
    system("tex $tmp_file_base") == 0 or die "problem executing tex";
    system("dvips -o $tmp_file_base.ps $tmp_file_base") == 0
      or die "problem executing dvips";
    unlink $tmp_file;
    unlink $tmp_file_base . ".dvi";
    if ($opt_p) {
	system("lpr " . $tmp_file_base . ".ps") == 0
	  or die "Error executing lpr.";
	unlink $tmp_file_base . ".ps";
    } else {
	print "Your ps file is in " . `pwd`;
	print "...and is named $tmp_file_base.ps\nthanks for using $0\n";
    }
    chdir $orig_cwd;
}

sub parse_file {
    die "Error: parameter missing" if @_ < 1;
    my $filename = $_[0];
    open CODE, $filename or die $!;

    my ($line, @processed_lines);
    while (defined($line = <CODE>)) {
	$line =~ s/\|/||/g;
	$line =~ s/\t/    /g;
	while (length($line) > $main::max_line_length) {
	    my $piece = substr($line, 0, $main::max_line_length);
	    $piece .= "\n";
	    $line = substr($line, $main::max_line_length);
	    push @processed_lines, $piece;
	}
	push @processed_lines, $line;
    }
    return \@processed_lines;
}

sub tmp_file {
    die "Error: parameter missing" if @_ < 1;
    my @lines = @{ $_[0] };
    my $tmpfilename = "code_print_" . $$ . ".tex";
    open TMPFILE, ">$tmpfilename"
      or die "Could not open $tmpfilename for writing: " . $!;
    print TMPFILE "% generated by $0\n";
    print TMPFILE "\\input eplain\n";
    print TMPFILE "\\verbatim\n";
    print TMPFILE @lines;
    print TMPFILE "|endverbatim\n\\bye";
    close TMPFILE;
    return $tmpfilename;
}

sub usage {
    print <<ENDORAMA;
usage:
	$0 [-h]
	$0 [-p] file_to_print

options:
	-h		print this message
	-p		actually print (and delete) the postscript file

Printing the .ps file is not the default, since I like to use gv to 
preview files and print from there.

ENDORAMA
}
