#!/usr/local/bin/perl
# Require Perl5
#
# pdfprint -- a perl script to print PDF files,
#
# by Fabrizio Pivari <pivari@geocities.com> 5 April 1998
#
# Copy, use, and redistribute freely, but don't take my name off it and
# clearly mark an altered version.  Fixes and enhancements cheerfully 
# accepted.
#
# This is version 2.1.
#
use strict;
use Getopt::Long;

my $version="2.1";
my $configure="pdfprint.cfg";
my $match="";
my $recursive="";
my $verbose="";
my $help="";
my @elem; my %option; my $elem="";

do GetOptions("configure=s" => \$configure,
              "recursive=s" => \$recursive,
              "match=s"     => \$match,
              "help"        => \$help,
              "verbose"     => \$verbose) || printusage() ;
@elem=("converter","print");
%option=(converter             => 'pdftops',
         print              => 'lpr');

$help and printusage();

open (CNF, "$configure") || die "pdfprint: couldn't open configuration file $configure\n";
while (<CNF>) {
  s/\t/ /g;        #replace tabs by space
  next if /^ *\#/; #ignore comment lines
  next if /^ *$/;  #ignore empty lines
  foreach $elem (@elem) {if (/ *$elem *: *(.*)/i) {$option{$elem}=$1;}}
  }
close(CNF);

sub wanted {
  if ($File::Find::name=~/$match/) {
    push @ARGV,$File::Find::name;
    }
  }


if ($match && !$recursive) {
   print "You can use -match option only with -recursive option\n";
   exit;
   }

if ($recursive) { 
  $match=~s/\./\\./g;
  $match=~s/\*/.*/g;
  $match=~s/\?/./g;
  $match=~s/$/\$/;
  find (\&wanted,"$recursive");
  }


my @args;
my $x=""; my $file="";
if (@ARGV) {
  foreach $x (@ARGV) {
    if ($x=~/\.pdf$/i) {
      $file="printpdf$$.ps";
      $verbose and print "Using converter tool with $x file\n";
      `$option{'converter'} $x $file`;
      $verbose and print "Printing the $x file\n";
      `$option{'print'} $file`;
      unlink $file;
      } else {print "Warning: the extension of the file isn't .pdf or .PDF\n";}
    }
  }
else {printusage();}

sub printusage {
    print <<USAGEDESC;

usage:
        pdfprint [-options ...] files

where options include:
    -help                        print out this message
    -verbose                     verbose
    -recursive directory         scan recursively the directory
    -match     files             match different files ex. *.pdf, a?.*
    -configure file              default pdfprint.cfg

files:
    with files you can use metacharacters and relative and absolute path name
    
example:
    pdfprint *.pdf
    pdfprint -v -c tests/test.cfg */*.pdf
    pdfprint -m a* -r pdfdocs

If you want to know more about this tool, you might want
to read the docs. They came together with pdfprint!

Home: http://www.geocities.com/CapeCanaveral/Lab/3469/pdfprint.html

USAGEDESC
    exit(1);
}
