#!/usr/bin/perl
# 
# Copyright (C) Koji Nakamaru
#
# Author: Koji Nakamaru (nakamaru at gmail.com)
# Modified: Apr 30 2005
#   * changed the contact information.
# Modified: Oct  5 2004
#   * fixed a bug which incorrectly removes "translate" for some pdfs
#     in the landscape style.
#     (thanks: nide-san (http://oku.edu.mie-u.ac.jp/~okumura/texfaq/qa/31924.html))
# Created: Jul 30 2002
# Keywords: acrobat, acroread, linux, postscript, pdf
#
# 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, 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 GNU Emacs; see the file COPYING.  If not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#

$cmd = "/usr/bin/lpr";
$fname = "/tmp/acrolpr$$";

$usage = <<"EOF";
Usage: perl acrolpr.pl [options] filename
Options:
  --help:
        print usage.
  -f fname:
        store converted postscript into the specified file instead of
        printing it.
  (options for lpr command may also be specified)

Note:
  This command should be used only in the acroread print
  dialog. Please add the following line in your .Xresources:

    AcroRead*lprCommand: acrolpr.pl

EOF

sub error
{
    my ($msg) = @_;
    print STDERR "acrolpr: ", $msg, "\n";
    unlink $fname;
    exit 1;
}

sub interrupted
{
    error "interrupted";
}

$SIG{'INT'} = $SIG{'TERM'} = $SIG{'QUIT'} = $SIG{'HUP'} = 'interrupted';

if ($#ARGV < 0) {
    print STDERR $usage;
    exit 1;
} else {
    $opt_f = 0;
    for ($i = 0; $i <= $#ARGV; $i++) {
	if ($ARGV[$i] =~ /-help/) {
	    print STDERR $usage;
	    exit 1;
	} elsif ($ARGV[$i] eq "-f") {
	    if ($i < $#ARGV - 1) {
		$fname = $ARGV[$i + 1];
		$opt_f = 1;
	    } else {
		error "-f requires an argument";
	    }
	}
    }
}

open IFILE, "<$ARGV[$#ARGV]"
    or error "cannot open $ARGV[$#ARGV] for read";
binmode IFILE;
open OFILE, ">$fname"
    or error "cannot open $fname for write";
binmode OFILE;

$mode = 0;
while (<IFILE>) {
    if (/^%%BeginResource: procset Adobe_CoolType_Utility_MAKEOCF/) {
	$mode = 1;
    } elsif (/^%%BeginPageSetup/) {
	$mode = 2;
    } elsif ($mode == 1 and /^%%EndResource/ or $mode == 2 and /^%%.*/) {
	$mode = 0;
    }
    if ($mode == 1) {
	s/ ct_BadResourceImplementation\?/ false/;
    } elsif ($mode == 2) {
	s/^([0-9eE\.\-]+)\s+([0-9eE\.\-]+)\s+translate//;
    }
    print OFILE;
}
close OFILE;
close IFILE;

if (! $opt_f) {
    for ($i = 0; $i < $#ARGV; $i++) {
	$cmd .= " $ARGV[$i]";
    }
    $cmd .= " $fname";
    if (system($cmd) != 0) {
	error "$cmd is failed";
    }
    unlink $fname;
}
