1#!/usr/bin/perl -w 2 3use strict; 4no strict "vars"; 5 6my $printcap="/etc/printcap"; 7my $filterdir="@filterdir@"; 8my $force=0; 9my $m; 10 11$m=0; 12 13if ($#ARGV >= 0) { 14 $force=($ARGV[0] eq "--force"); if ($force) { $m++; } 15} 16 17if ($m <= $#ARGV) { $printcap=$ARGV[$m]; } 18 19sub input 20{ 21 my($message)=$_[0]; 22 my($defanswer)=$_[1]; 23 my($reply); 24 25 printf "%-45s [%-15s]: ",$message,$defanswer; 26 $reply=<STDIN>; chop($reply); if ($reply eq "") { $reply=$defanswer; } 27 28 return $reply; 29} 30 31sub yninput 32{ 33 my($message)=$_[0]; 34 my($defanswer)=$_[1]; 35 my($reply); 36 37 printf "%s? (y/n) [%s]: ",$message,$defanswer; 38 $reply=<STDIN>; chop($reply); if ($reply eq "") { $reply=$defanswer; } 39 40 return $reply; 41} 42 43sub readfilters 44{ 45 my($i); 46 47 if ( ! -e $filterdir ) 48 { 49 print<<EOT; 50 51The magicfilter package is wrongly installed; can't find $filterdir. 52 53EOT 54 55 die "'$filterdir' missing, stopped"; 56 } 57 58 open(FILTERS, "ls $filterdir|sort|") || die "Can't find $filters."; 59 $i=0; 60 while (<FILTERS>) 61 { 62 chop; 63 s/$filterdir\/(.*)-filter/$1/g; 64 $filters[$i]=$_; $i++; 65 } 66 close(FILTERS); 67 68 foreach $entry (@filters) { $entry =~ s/-filter//g; } 69 return sort(@filters); 70} 71 72if ( -e $printcap && !$force) 73{ 74 print<<EOT; 75 76You already have an $printcap file installed. 77I assume that it is already correctly configured and won't overwrite it. 78(if $printcap is not correctly configured, delete the file and re-run 79 $0 or run '$0 --force'). 80 81EOT 82 exit 0; 83} 84 85@filters=&readfilters; 86 87$ok="no"; $m=-1; $d=0; 88while ($ok !~ /y(es)?/) 89{ 90 print<<EOT; 91 92You will have to supply the following items for each printer to be installed 93(example values in parentheses): 94 95 - its short name and its full name (hpjlet4, HP LaserJet 4) 96 - the device where the printer is attached (/dev/lp1) 97 - and the input filter you want to use (ljet4l) 98 99EOT 100 101 $p=-1; 102 do 103 { 104 $p++; $m++; $full[$m+1]='done'; 105 106 if ($p == 0) { $full[$p]='HP Laserjet 4L'; } 107 else { $full[$p]='done'; } 108 109 $short[$p]='hplj4l'; 110 $dev[$p]=sprintf("/dev/lp%d",$p+1); $ifilter[$p]='ljet4l'; 111 $remflags[$p]=""; $serflags[$p]=""; 112 113 printf "Printer #%d:\n", $p+1; 114 printf "------------\n"; 115 116 $full[$p]=&input("Full name, `done' when done", $full[$p]); 117 118 if ($full[$p] !~ /done\s*/) 119 { 120 $short[$p]=&input("Short name, name of the spool directory",$short[$p]); 121 $dev[$p] =&input("Full device path (or remote machine name)", 122 $dev[$p]); 123 124 if ($#filters == 0) 125 { 126 print "There aren't any filters."; $full[$p]='done'; 127 } 128 else 129 { 130 print<<EOT; 131 132The following filters are available in $filterdir. You can read 133the comments at the top of each file to help decide which filter would 134best suit your needs. 135 136EOT 137 $l=0; 138 foreach $filter (@filters) 139 { 140 $l += (length($filter)+1); 141 if ($l > 72-length($filter)) { print "\n"; $l=0; } 142 print "$filter "; 143 } 144 print "\n\n"; 145 146 $ifilter[$p]=&input("The input filter to be used", $ifilter[$p]); 147 148 if ($dev[$p] !~ /\/dev\//) { 149 $remflags[$p]=":rm=$dev[$p]:rp=$short[$p]:lpr_bounce"; 150 $dev[$p]="/dev/null"; 151 } elsif ($dev[$p] =~ /\/dev\/ttyS/) { 152 $serflags[$p]=":bf=2400:fc=0:fs=1"; 153 } 154 155 $spooldir[$p]="/var/spool/lpd/$short[$p]"; 156 157 print "\n"; 158 } 159 } 160 } 161 while ($full[$p] !~ /done\s*/); 162 163 if ($p > 0) 164 { 165 if ($p == 1) { $d=0; } 166 do 167 { 168 format top= 169 170 Printer configuration 171 172Default printer (alias `lp') is marked with an asterisk. 173 174Printer name Short name Spool dir Device Input filter 175------------------------------------------------------------------------------- 176. 177 178 format STDOUT= 179@<<<<<<<<<<<< @<<<<<<<<< @<<<<<<<<<<<<<<<<<<< @<<<<<<<<< @<<<<<<<<<<<<<<<<<<@<< 180$full, $short, $spooldir, $dev, $ifilter, $a 181. 182 183 for($i=0;$i<$p;$i++) 184 { 185 $full=$full[$i]; $short=$short[$i]; 186 $spooldir=$spooldir[$i]; $dev=$dev[$i]; $ifilter=$ifilter[$i]; 187 $a= ($remflags[$i] ne "") ? "R" : ""; 188 $a= $a . (($i == $d) ? "*" : ""); 189 190 write; 191 } 192 $ans="n"; $-=0; 193 194 print "\n"; 195 if ($p > 1) 196 { 197 $ans=&yninput("Do you want to change the default printer","n"); 198 if ($ans =~ /y(es)?/) 199 { 200 $def=&input("New default printer (type its short name)",""); 201 $d=0; 202 while (($d < $p) && ($short[$d] !~ /$def/)) { $d++; } 203 if ($d == $p) { $d = 0; } 204 $short[$d]=$def; 205 } 206 } 207 } 208 while ($ans =~ /y(es)?/); 209 $ok=&yninput("Is this ok","y"); 210 } 211 else 212 { 213 $ok="yes"; 214 } 215} 216 217print "\n"; 218if ($p > 0) 219{ 220 if ( -x "/usr/bin/paperconf" ) 221 { 222 open(PAPERSIZE, "/usr/bin/paperconf -N|"); 223 $papersize=<PAPERSIZE>; chop($papersize); 224 close(PAPERSIZE); 225 } 226 227 $pagelength=66; 228 if ($papersize =~ /A4/) { $pagelength=72; } 229 print "Paper size is $papersize, page length is $pagelength lines.\n"; 230 231 # Stop running daemons: 232 if ($> == 0) { 233 if ( -x "/etc/init.d/lpd" ) { system("/etc/init.d/lpd stop"); } 234 if ( -x "/etc/init.d/lprng" ) { system("/etc/init.d/lprng stop"); } 235 } 236 237 open(PRINTCAP,">$printcap") || die "Can't write $printcap: $!"; 238 print PRINTCAP <<EOH; 239# 240# LPD printer configuration. 241# 242# This file was generated by $0. 243# 244EOH 245 246 $short[$d]="lp|" . $short[$d]; 247 for($i=0;$i<$p;$i++) 248 { 249 print PRINTCAP <<"EOE"; 250$short[$i]|$full[$i]:\\ 251\t:lp=$dev[$i]:sd=$spooldir[$i]$remflags[$i]$serflags[$i]:\\ 252\t:sh:pw#80:pl#${pagelength}:px#1440:mx#0:\\ 253\t:if=$filterdir/$ifilter[$i]:\\ 254\t:af=/var/log/lp-acct:lf=/var/log/lp-errs: 255EOE 256 } 257 close(PRINTCAP); 258 259 for($i=0;$i<$p;$i++) 260 { 261 $lpgrp=(getpwnam("lp"))[2]; 262 if ( ! -e $spooldir[$i] ) 263 { 264 umask 002; mkdir($spooldir[$i],02775); chown(0,$lpgrp,$spooldir[$i]); 265 } 266 } 267 268 print<<EOT; 269$printcap generated. 270 271You'll probably want to finetune your newly created $printcap file. 272Read the printcap(5), lpr(1), lpq(1), lprm(1) and lpc(1) manual pages 273and the PRINTING-HOWTO before doing this. 274 275EOT 276 277 if ($> == 0) { 278 if ( -x "/etc/init.d/lpd" ) { system("/etc/init.d/lpd start"); } 279 if ( -x "/etc/init.d/lprng" ) { system("/etc/init.d/lprng start"); } 280 } 281} 282else 283{ 284 print<<EOT; 285 286Nothing to install. 287You're going to write your $printcap file yourself. 288 289EOT 290} 291 292my $display=$ENV{"DISPLAY"} || ""; 293 294if ( $display ne "" ) { 295 $dummy=""; 296 print "Press <ENTER> to continue."; $dummy=<STDIN>; 297} 298 299exit 0; 300