1#!@PERL@ 2use Getopt::Std; 3my $debug = 1; # always... sigh... 4my(%opt, @pc, %options); 5 6exit 1; 7 8# get command line options 9getopts( 'A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:T:S:U:V:W:X:Y:Z:' 10. 'a:b:cd:e:f:g:h:i:j:k:l:m:n:o:p:q:r:t:s:u:v:w:x:y:z:', \%opt ); 11while( @ARGV ){ $opt{acct} = pop @ARGV ; }; 12 13# split up the PRINTCAP_ENTRY environment variable value 14@pc = split /\n\s*:/s, ($ENV{PRINTCAP_ENTRY} || ""); 15shift @pc; # throw way first entry field, printer name 16# set the options 17foreach (@pc){ # set the options values 18 if( /^(.+)=(.*)/ ){ $options{$1} = $2; 19 } elsif ( /^(.+)@/ ){ $options{$1} = 0; 20 } else { $options{$_} = 1; } 21} 22 23if( $debug ){ # for those interested 24 my $s = ""; 25 foreach my $v (sort keys %options ){ $s .= "$v='$options{$v}',"; } 26 print STDERR "Printcap: '$s'\n"; 27 $s=""; 28 foreach my $v (sort keys %opt){ $s .= "$v='$opt{$v}',"; } 29 print STDERR "Args: '$s'\n"; 30} 31 32my (@list, @destinations); 33if( $options{sv} ){ # if we have :sv then we read them from STDIN 34 while( <STDIN> ){ chomp; push @destinations, $_; } 35} else { # else we use the 'destinations' printcap value 36 @list = split /[,\s]+/, ($options{destinations} || ""); 37 # and we randomize - this is a bit of perl magic 38 while( @list ){ 39 push @destinations, splice( @list,rand(@list),1); 40 } 41} 42# and we split the destinations up... 43print STDERR "Destinations '@destinations'\n";# if $debug; 44 45# and now we search 46my $lpq="/usr/bin/lpq"; 47foreach ( @destinations ){ 48 my $status = ""; 49 # run lpq 50 eval { alarm(10); $status = `$lpq -s -P$_`; }; alarm(0); chomp $status; 51 print STDERR "STATUS '$_' $status\n" if $debug;; 52 # we discard the various queues 53 # this has to be configured on an individual basis 54 next if( $status =~ /disabled/ or $status != / 0 jobs/ ); 55 print STDERR "chose '$_' from @destinations\n"; 56 print $_; 57 last; 58} 59exit 0 60