[comp.sys.hp] utility: printcap to lpadmin

hakanson@ogicse.ogi.edu (Marion Hakanson) (04/11/91)

Below find "pcap2lp" -- a Perl (3.0.44 or later) program which parses
a printcap file and spits out a shell script of lpadmin commands which
will recreate all the printers and aliases in the printcap file.

It is of limited use in that it only deals with remote printers (but
most of our machines have none attached directly, so this covers a lot
of cases), and in particular it assumes the printer models used by
HP-UX 7.0 for supporting remote printers.  It certainly saves a lot of
work in keeping our HP machines "in sync" with the BSD world they live
in here.  Comments welcome.

-- 
Marion Hakanson         Domain: hakanson@cse.ogi.edu
                        UUCP  : {hp-pcd,tektronix}!ogicse!hakanson

===================================================================

This script depends on a patched perl termcap.pl library file:

===================================================================
RCS file: RCS/termcap.pl,v
retrieving revision 1.1
diff -r1.1 termcap.pl
24c24
< 	if (index($TERMCAP,"|$TERM|") < $[) {
---
> 	if ($TERMCAP !~ /(^|\|)$TERM[:\|]/) {
36c36
< 		if (/\\|$TERM[:\\|]/) {
---
> 		if (/(^|\\|)$TERM[:\\|]/) {
===================================================================
#!/usr/bin/perl
;#
;# $Id: pcap2lp,v 1.3 91/03/21 17:07:11 hakanson Exp $
;#
;# Perl program to take BSD printcap entries for remote printers and
;# produce SysV-style lpadmin commands to configure the same printers.
;#   Marion Hakanson (hakanson@cse.ogi.edu)
;#   Oregon Graduate Institute of Science and Technology
;#
;# Note that this is designed for HP-UX 7.0, which supports the BSD-style
;# lpd protocol for remote printing, but via a Sys-V interface.  It expects
;# appropriate model files to be in place.

($ZERO = $0) =~ s#.*/##;	# Save for later
$[ = 0;

unless ( $#ARGV == 0 ) {
  print STDERR "usage: $ZERO <printcap-file>\n";
  exit 1;
}

$PRINTCAP = $ARGV[0];

;# Need a full path for TERMCAP env. variable.
if ( $PRINTCAP !~ m#^/# ) {
  chop($cwd = `pwd`) || die "Can't find current directory: $!, aborted";

  if ( $PRINTCAP =~ m#.*/# ) {
    chop($head = $&);		# remove trailing '/'
    $tail = $';
    chdir($head) || die "Can't chdir to $head: $!, aborted";
    chop($head = `pwd`) || die "Can't find path of $head: $!, aborted";
    chdir($cwd) || die "Can't chdir back to $cwd: $!, aborted";
  } else {
    $head = $cwd;
    $tail = $PRINTCAP;
  }
  $PRINTCAP = $head . '/' . $tail;  
}

    #print STDERR "$ARGV[0] -> $PRINTCAP\n";
open(PRINTCAP, "<$PRINTCAP") || die "Can't open $PRINTCAP: $!, aborted";

require 'termcap.pl';		# Load the library routine.
$ospeed = -1;			# termcap.pl wants this to be nonzero.

$ENV{'TERMCAP'} = $PRINTCAP;	# Tgetent() will read this file.

;# This outer loop is the same as that in Tgetent().
;# We want to find all the printcap entries in the file, at which
;# point we'll make multiple passes through, parsing each one.

print "#!/bin/sh\n";
print "\n";
print "# May want to remove all printers:\n";
print "#   cd /usr/spool/lp/member\n";
print "#   for i in *\n";
print "#     do /usr/lib/lpadmin -x\$i\n";
print "#   done\n";

while ( <PRINTCAP> ) {
  next if /^#/;		# comment
  next if /^\t/;	# continuation line

  chop;			# strip newline
  s/:.*$//;		# keep only first entry
    #print STDERR "\n$_\n";
  @printers = split(/\|/);

    #$, = ' '; print STDERR @printers; print STDERR "\n";

  # Throw away the last one if it contains whitespace.
  pop(@printers) if ( $printers[$#printers] =~ /\s/ );

    #$, = ' '; print STDERR @printers; print STDERR "\n";

  # But printcap(5) says the second name is the most commonly
  # used one, but we will use the first one instead.

  next if ( $#printers < 0 );	# Must be at least one name

  $primary = $printers[0];

  &Tgetent($primary);		# Sets %TC array

    #while ( ($key, $val) = each %TC ) {
    #  print STDERR "$key $val\n";
    #}

  next unless ( $TC{'rm'} );	# skip if not remote

  $TC{'rp'} = $primary unless ( $TC{'rp'} );

  foreach $printer ( @printers ) {
    $cmd  = '/usr/lib/lpadmin ';
    $cmd .= "-p$printer ";
    $cmd .= '-mrmodel -v/dev/null -orc -ocmrcmodel -osmrsmodel ';
    $cmd .= "-orm$TC{'rm'} ";
    $cmd .= "-orp$TC{'rp'} ";
    $cmd .= '-ob3 ';		# Don't want this if remote is HP!

    print "$cmd\n";
  }
}

print "# May want a default printer:\n";
print "#   /usr/lib/lpadmin -dlp\n";
print "# May want to enable all printers:\n";
print "#   cd /usr/spool/lp/member\n";
print "#   enable *\n";
print "#   /usr/lib/accept *\n";

exit(0);
===================================================================