#!/usr/bin/perl
#
# Copyright (c) 2002 Steve Slaven, All Rights Reserved.
#
# 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
#
# Used to replace lpr on systems where the printers may change
# a lot (e.g. a laptop) and there are several irritating apps
# (e.g. mozilla) that don't let you choose an alternate printer
#
# Rename /usr/bin/lpr to /usr/bin/lpr.real and put this in
# /usr/bin/lpr
# Prompt if no printer specified
@opts = @ARGV; # This is to copy the args we'll be using in the open() call
# Try and read printcap, to get a list of printers
%printers = ();
open( IN, "/etc/printcap" );
while( $line = ) {
$line =~ s/#.*//;
if( $line =~ /^[a-zA-Z]/ ) {
# Assume it's set up like
# name|Real name:
( $pn ) = $line =~ /^([^ ].*?):/;
next unless $pn;
@names = split( /\|/, $pn );
$realname = pop( @names );
for( @names ) {
$printers{ $_ } = $realname;
}
}
}
close( IN );
# Find default
( $printer ) = grep { /^-P/ } @opts;
$printer =~ s/^-P//;
@opts = grep { $_ !~ /^-P/ } @opts;
$printer = $printer || 'lp';
# Make a hoopy-doopy dialog
@cmd = ( '--radiolist',
'Select Printer',
'400',
'600',
'10' );
for( keys( %printers ) ) {
push( @cmd,
$_,
$printers{ $_ },
$_ eq $printer ? 'on' : 'off' );
}
push( @cmd,
'CANCEL',
'Do not print',
'off' );
# Generate cmdline
$cmdline = join( " ", map { s/(["'\\;])/\\$1/g; '"' . $_ . '"' } @cmd );
$ans = '';
while( ! $ans ) {
$ans = `gdialog $cmdline 2>&1`;
}
chomp( $ans );
if( $ans eq "CANCEL" ) {
system( "gdialog --infobox 'Print job canceled' 300 500" );
exit;
}
unshift( @opts, "-P$ans" );
$opts = join( " ", map { s/(["'\\;])/\\$1/g; '"' . $_ . '"' } @opts );
# Handle sigchld, e.g. if lpr.real dies we should too
$SIG{ CHLD } = sub { exit(); };
open( OUT, "|/usr/bin/lpr.real $opts" );
print OUT $_ while ;
close( OUT );