Wednesday, April 15, 2009

Convert lpd printcap to CUPS printers.conf - Rough draft.

It's perl, rather than bash. Some things need a bit more oomph.

#!/usr/local/bin/perl -w 
use strict;

my $Printcap = "/hub/share/etc/printcap";

my $i = 0;
my $j = 0;
open (PRINTCAP, "$Printcap") or die "can't open input file";

#Run through the printcap recording the queue and the server in separate arrays.  The separate arrays allow us to recall the data
#later asynchronously.  This looks like it could have been accomplished with a associative array, but there is no unique index, 
#since two print queues on separate servers can have the same name as far as I know - Adam 

while () {
 if ( /^\n$/ ) {
 if ( /:lp\=:/ ) {
  ($a,$b,$Server[$i]) = split(/=/,$_);
  chomp $Server[$i];
  $Server[$i] =~ s/(\:|\\)//g;
 if ( /:rp\=/ ) {
  ($c, $PrintQueue[$i]) = split(/=/,$_);
  $PrintQueue[$i] =~  s/(\:|\\)//g;
  chomp $PrintQueue[$i];
# printcap ends on a new line so there will be no elements in the arrays for the last value of $i


# Now having collected the data from printcap we dump the stanza for printers.conf
while ( $j <= $i ){
 print "\n";
 print "Info\n";
 print "Location\n";
 print "DeviceURI lpd://$Server[$j]$PrintQueue[$j]\n";
 print "State Idle\n";
 print "Accepting Yes\n";
 print "JobSheets none none\n";
 print "QuotaPeriod 0\n";
 print "PageLimit 0\n";
 print "KLimit 0\n";
 print "\n";