"SfR Fresh" - the SfR Freeware/Shareware Archive 
Member "susefilter-1.5/apsfilter.setup.script" of archive susefilter-1.5.tar.gz:
As a special service "SfR Fresh" has tried to format the requested source page into HTML format using source code syntax highlighting with prefixed line numbers.
Alternatively you can here view or download the uninterpreted source code file.
That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
1 #! /bin/bash
2 #
3 # apsfilter.setup for configuration of apsfilter
4 #
5 # Copyright 1996 S.u.S.E. GmbH
6 # Author: Werner Fink <werner@suse.de>
7 #
8 # Version: @@version@@
9 #
10 # Note: it _is_ complicated
11 #
12
13 # Global environment
14 #
15 APSFILTER=@@/var/lib/apsfilter@@
16 APSRC=/etc/apsfilterrc
17 SPOOL=/var/spool/lpd
18 LP_OWNER="lp"
19 LP_GROUP="lp"
20 METHODS="ascii auto raw"
21 PRINTCAP="/etc/printcap"
22 OLDCAP="${PRINTCAP}.old"
23 TMPPCAP="/tmp/printcap.aps$$"
24 LABEL=""
25 SLABEL="### BEGIN apsfilter:"
26 WLABEL="# Warning: Configured for apsfilter, do not edit the labels!"
27 DLABEL="# apsfilter setup `date`"
28 ELABEL="### END apsfilter:"
29 OLABEL="LABEL apsfilter"
30 PNAME=""
31 TODO="add"
32 CLEAR=""
33 HOST=""
34 DEVICE=""
35 SERIAL=""
36 PRINTER=""
37 PAPER=""
38 COLOR="mono"
39 DPI=""
40 DVIPS=""
41 NO_FF_SEP=""
42 declare -i lp=0
43 declare -i ascii=0
44 declare -i raw=0
45 declare -i auto=0
46 declare -i remote=0
47
48 #
49 # Check first
50 #
51 rm -f $TMPPCAP
52 if [ -e $TMPPCAP ] ; then
53 echo "Our temporary file $TMPPCAP already exists" 1>&2
54 exit 1
55 fi
56
57 #
58 # On an interrupt we want exit cleanly
59 #
60 trap "rm -f $TMPPCAP; exit" 0 1 3 5 9 10 13 15
61 trap "exit 1" 2
62
63 #
64 # We want be called in a simple way
65 #
66 usage ()
67 {
68 echo -e "$0: Usage:
69 For help:
70 $0 --help
71
72 Overwrite|Add local printer entry in printcap:
73 $0 [-O|-A] -d <dev> -G <gsmode> [-s <baud>] \\
74 -t <paper> (-c|-m) -r <res> [-nf] [-M <dvipsmode>] [-N <name>]
75
76 Delete local printer entry in printcap:
77 $0 -D -G <gsmode> -t <paper> (-c|-m) -r <res> [-N <name>]
78
79 Overwrite|Add a remote default printer of a host in local printcap:
80 $0 [-O|-A] -h <hostname> -R <printer> [-nf] [-N <name>]
81
82 Delete a remote default printer of a host in local printcap:
83 $0 -D -h <hostname> -R <printer> [-N <name>]
84
85 List apsfilter printer entries in printcap:
86 $0 -L" 1>&2
87 exit 1
88 }
89
90 apshelp ()
91 {
92 echo "$0: Help and Usage of $0
93 Operations:
94 -A Add an new entry in /etc/printcap.
95 -O Overwrite an existing entry in /etc/printcap.
96 -O Delete an existing entry in /etc/printcap.
97 -L List all existing entries in /etc/printcap done by $0.
98
99 Specifications:
100 -G <gsmode> The name of the ghostscript driver for the printer.
101 -d <dev> The full path to an existing device/interface.
102 -s <baud> The optional baud rate of a parallel interface.
103 -t <paper> The paper size of the printer.
104 -c Colorfull printer or
105 -m Monochrome printer.
106 -r <res> The resolution in DPI (i.e.: 300, 300x600, ...)
107 -nf Set \`sf' in printcap entry (suppress form feeds between
108 two print jobs).
109 -M <dvipsmode> The name of an optional dvips printer, should be configured
110 (e.g.: with texconfig under the tetex package).
111 -h <hostname> The hostname a remote printer server.
112 -R <printer> The name of the remote printer.
113 -N <name> The optional starting name of a printer.
114 (a|b) \`a' or \`b' not both settings
115 [a] \`a' is an optional setting
116
117 Examples:
118
119 Overwrite|Add local printer entry in printcap:
120 $0 (-O|-A [--clear]) -d <dev> -G <gsmode> [-s <baud>] \\
121 -t <paper> (-c|-m) -r <res> [-nf] [-M <dvipsmode>] [-N <name>]
122
123 Delete local printer entry in printcap:
124 $0 -D -G <gsmode> -t <paper> (-c|-m) -r <res> [-N <name>]
125
126 Overwrite|Add a remote default printer of a host in local printcap:
127 $0 (-O|-A [--clear]) -h <hostname> -R <printer> [-nf] [-N <name>]
128
129 Delete a remote default printer of a host in local printcap:
130 $0 -D -h <hostname> -R <printer> [-N <name>]
131
132 List apsfilter printer entries in printcap:
133 $0 -L
134 " 1>&2
135 exit 0
136 }
137
138 ##
139 ## If we write to the /etc/printcap
140 ##
141 printpc ()
142 {
143 echo -e "$*" >> $PRINTCAP
144 }
145
146 ##
147 ## Uh .. do it verbosely
148 ##
149 printlg ()
150 {
151 echo -e "$*" 1>&2
152 }
153
154 ##
155 ## Read options
156 ##
157 [ -z "$1" ] && usage
158 while [ -n "$1" ] ; do
159 case $1 in
160 -h) HOST=${2} ; shift ; shift ;;
161 -A) TODO="add" ; shift ;;
162 -O) TODO="ovw" ; shift ;;
163 -D) TODO="del" ; shift ;;
164 -L) TODO="shw" ; shift ;;
165 -nf) NO_FF_SEP="yes" ; shift ;;
166 -d) DEVICE=${2} ; shift ; shift ;;
167 -s) SERIAL=${2} ; shift ; shift ;;
168 -G) PRINTER=${2} ; shift ; shift ;;
169 -R) RPRINTER=${2} ; shift ; shift ;;
170 -N) PNAME=${2} ; shift ; shift ;;
171 -t) PAPER=${2} ; shift ; shift ;;
172 -c) COLOR="color" ; shift ;;
173 -m) COLOR="mono" ; shift ;;
174 -r) DPI=${2} ; shift ; shift ;;
175 -M) DVIPS=${2} ; shift ; shift ;;
176 --clear) CLEAR="yes" ; shift ;;
177 --help) apshelp ;;
178 *) usage ;;
179 esac
180 done
181 [ "$CLEAR" = "yes" -a "$TODO" != "add" ] && usage
182 [ -z "$DPI" ] && DPI="default"
183
184 ##
185 ## List all printer enties of apsfilter in /etc/printcap
186 ##
187 if [ "$TODO" = "shw" ] ; then
188 sed "/^${SLABEL}\$/,/^${ELABEL}\$/d" < $PRINTCAP | \
189 grep -E "^($SLABEL|[a-z0-9A-Z\-]+\|)" | \
190 sed "s/^${SLABEL}//g;\
191 s/\:\\\\//g" 1>&2
192 #
193 # Ok .. done
194 #
195 exit 0
196 fi
197
198 ##
199 ## Check options, set defaults and lables
200 ##
201 if [ -n "$HOST" ] ; then
202 #
203 # Paranoid
204 #
205 if [ "${HOST%%.*}" = "lp" ] ; then
206 echo "$0: Conflicting names of host and default printer queue" 1>&2
207 exit 1
208 fi
209 # SHOST="${HOST%%.*}"
210 SHOST="${HOST}"
211 #
212 # These things belongs to the remote printer server
213 #
214 DEVICE="/dev/null"
215 PRINTER="remote"
216 [ -z "$RPRINTER" ] && usage
217 PAPER="unknown"
218 DPI="unknown"
219 METHODS="remote"
220 #
221 # LABEL for identification of the remote printer
222 #
223 if [ -n "$PNAME" ]; then
224 LABEL="### $PRINTER $SHOST $RPRINTER $PNAME ###"
225 else
226 LABEL="### $PRINTER $SHOST $RPRINTER ###"
227 fi
228 else
229 #
230 # if we want to delete an entry we don't look one the device
231 # but on the printer name
232 #
233 [ "$TODO" = "del" ] && { DEVICE="unknown"; }
234 #
235 # We need it
236 #
237 [ -z "$DEVICE" -o -z "$PRINTER" -o \
238 -z "$PAPER" -o -z "$COLOR" -o -z "$DPI" ] && usage
239 case $PAPER in
240 letter|legal|ledger) ;;
241 11x17) ;;
242 a[0123456789]) ;;
243 a10) ;;
244 b[012345]) ;;
245 arch[EDCBA]) ;;
246 fls[ae]) ;;
247 halfletter|note) ;;
248 *) usage ;;
249 esac
250 [ `echo $DPI|grep -cE '^(default|[0-9]+|[0-9]+x[0-9]+)$'` -eq 0 ] && usage
251 #
252 # Shorten the resolution if horizontal resolution
253 # is equal to vertical resolution
254 #
255 set -- `echo ${DPI}| tr 'x' ' '`
256 [ -n "$2" ] && [ $1 -eq $2 ] && DPI="$1"
257 #
258 # LABEL for identification for local printers
259 #
260 if [ -n "$PNAME" ]; then
261 LABEL="### $PRINTER $PAPER $COLOR $DPI $PNAME ###"
262 else
263 LABEL="### $PRINTER $PAPER $COLOR $DPI ###"
264 fi
265 fi
266
267 ##
268 ## Color methods: on color we want mono too
269 ##
270 second=""
271 if [ "$COLOR" = "color" ] ; then
272 COLOR="$COLOR mono"
273 # If we have a color Printer
274 # we have a `only mono' device too
275 second="mono"
276 fi
277 #
278 # Save original /etc/printcap
279 # and clean example entry of /etc/printcap
280 # out of the packages lprold/plp
281 #
282 touch $PRINTCAP
283 #
284 # Save original /etc/printcap to /etc/printcap.orig
285 #
286 [ ! -f ${PRINTCAP}.orig ] && cp $PRINTCAP ${PRINTCAP}.orig
287 #
288 # Clean the stupid example
289 #
290 sed 's@^\(lp:lp=/dev/lp1:sd=/usr/spool/lp1:sh\)@#\1@g' < $PRINTCAP > $OLDCAP
291 #
292 # Our working files: changes going to /etc/printcap
293 # refrence is /etc/printcap.old
294 #
295 cp $OLDCAP $PRINTCAP
296
297 ##
298 ## Special for YaST: the old overwrite of apsfilter removes all
299 ## aps entries. Therefore, if we are called with --clear
300 ## we will clear /etc/printcap
301 ##
302 if [ "$CLEAR" = "yes" -a "$TODO" = "add" ] ; then
303 #
304 # Clear /etc/printcap ... means remove all
305 # entries of this script or an old aps-SETUP
306 #
307 cp $PRINTCAP $TMPPCAP
308 lines=$(cat $TMPPCAP| wc -l)
309 sed "/${OLABEL}$\|^${SLABEL}/,${lines}d" < $TMPPCAP > $PRINTCAP
310 #
311 fi
312
313 ##
314 ## Now set extended Start and End label of our special entry
315 ##
316 SLABEL="$SLABEL $LABEL"
317 ELABEL="$ELABEL $LABEL"
318
319 ##
320 ## Befor adding an entry we will check lp names
321 ##
322 if [ "$TODO" = "add" ] ; then
323 #
324 # Paranoid ... maybe there is a hand written entry
325 # ... we have to count lp1 .. lpN names correctly !!
326 #
327 cp $PRINTCAP $TMPPCAP
328 cat $TMPPCAP | awk 'BEGIN { n=0 } \
329 { \
330 if (/lp([0-9]+)\|/) { \
331 n++ ; gsub(/lp([0-9]+)\|/,"lp" n "|" ) \
332 } \
333 print $0 \
334 }' > $PRINTCAP
335 #
336 # We add a printer only if lines not zero
337 # ... means there is no equal entry
338 #
339 lines=$(sed -n "/^${SLABEL}\$/,/^${ELABEL}\$/p" < $PRINTCAP | wc -l)
340 #
341 # lines > 0 ... oops there IS an entry
342 #
343 if [ $lines -gt 0 ] ; then
344 echo "$0: Entry $LABEL exists" 1>&2
345 exit 1
346 fi
347 #
348 fi
349
350 ##
351 ## Overwrite, delete, or append existing apsfilter entry in /etc/printcap
352 ##
353 if [ "$TODO" = "ovw" -o "$TODO" = "del" ] ; then
354 #
355 # Check
356 #
357 check=$(grep -cE "^${SLABEL}" $PRINTCAP)
358 [ $check -eq 0 ] && { echo "$0: No entry $LABEL" 1>&2 ; exit 1; }
359 #
360 # Maybe we have to remove some links and spool directories
361 #
362 if [ "$TODO" = "del" ] ; then
363 remove=$(sed -n "/^${SLABEL}\$/,/^${ELABEL}\$/p" < $PRINTCAP | \
364 grep -E ':sd=|:if=' | sed 's/:\|\\/ /g')
365 fi
366 #
367 # Remove an existing apsfilter entry out of /etc/printcap
368 #
369 sed "/^${SLABEL}\$/,/^${ELABEL}\$/d" < $PRINTCAP > $TMPPCAP
370 #
371 # If we overwrite or delete an entry we have to
372 # update the lp1 ... lpN names in the remaining entries
373 #
374 cat $TMPPCAP | awk 'BEGIN { n=0 } \
375 { \
376 if (/lp([0-9]+)\|/) { \
377 n++ ; gsub(/lp([0-9]+)\|/,"lp" n "|" ) \
378 } \
379 print $0 \
380 }' > $PRINTCAP
381 #
382 # On delete action our work is done
383 #
384 if [ "$TODO" = "del" ] ; then
385 #
386 # Remove if and only if links and spool directories
387 # not used by another entry
388 #
389 for d in $remove ; do
390 check=$(grep -c ":${d}:" $PRINTCAP)
391 if [ $check -eq 0 ] ; then
392 case $d in
393 sd=*) rm -rf ${d#*=} ;;
394 if=*) rm -f ${d#*=} ;;
395 esac
396 fi
397 done
398 exit 0
399 fi
400 #
401 # Puh, now we can act like `adding a new entry'
402 #
403 TODO="add"
404 #
405 fi
406
407 ##
408 ## Never needed any more
409 ##
410 rm -f $TMPPCAP
411
412 ##
413 ## Now we want to add an new entry ...
414 ##
415 [ "$TODO" != "add" ] && exit 0
416
417 #
418 # Let's count some printer entries
419 #
420 lp=0
421 ascii=0
422 raw=0
423 auto=0
424 remote=0
425
426 #
427 # We count all lp1 ... lpN entries
428 #
429 lp=$(cat $PRINTCAP | awk 'BEGIN {n=0} {if (/lp([0-9]+)\|/) n++} END {print n}')
430 lp=$((lp + 1))
431
432 #
433 # These are all entries of the default printer
434 #
435 ascii=`grep -cE "^ascii\|" $PRINTCAP`
436 raw=`grep -cE "^raw\|" $PRINTCAP`
437 auto=`grep -cE "^lp(-mono|)\|" $PRINTCAP`
438 remote=`grep -cE "^remote\|" $PRINTCAP`
439 #
440 # mono printer: one entry counts like 2 auto entries
441 # of a color one ...
442 #
443 [ $auto -eq 1 ] && auto=2
444
445 #
446 # Now built all possible printer entries
447 # for our printer
448 #
449 name=""
450 for m in $METHODS ; do
451 #
452 # local APS-conform printer spool
453 # name := ${PRINTER}-${PAPER}-${m}[-${c}-${DPI}]
454 # PRINTER=<gs_device_name>
455 # PAPER=<paper_type>
456 # m=ascii,auto,raw
457 # c=mono,color
458 # DPI=<resolution>
459 #
460 case $m in
461 ascii)
462 #
463 # if ascii=0 we have the default entry belonging to `lp'
464 main="${PRINTER}-ascii"
465 [ -n "$PNAME" ] && main="${PNAME}-ascii"
466 [ $ascii -eq 0 ] && main="ascii"
467 ascii=$((ascii + 1))
468 lpN="lp${lp}" ; lp=$((lp + 1))
469 comment="${PRINTER}~${PAPER}~${m}~mono~${DPI}"
470 spool="${PRINTER}-${PAPER}-${m}-mono-${DPI}"
471 name="$name ${main}|${lpN}|${spool}|${comment}"
472 ;;
473 auto)
474 #
475 # if auto=0 we have the default entry `lp'
476 # if auto=1 we have the mono entry belonging to `lp'
477 # ... therefore we have to count a mono `lp' twice
478 for c in $COLOR ; do
479 main="${PRINTER}"
480 [ -n "$PNAME" ] && main="$PNAME"
481 [ "$c" = "$second" ] && main="${main}-${c}"
482 [ $auto -eq 0 ] && main="lp"
483 [ $auto -eq 1 ] && main="lp-${c}"
484 auto=$((auto + 1))
485 lpN="lp${lp}" ; lp=$((lp + 1))
486 comment="${PRINTER}~${PAPER}~${m}~${c}~${DPI}"
487 spool="${PRINTER}-${PAPER}-${m}-${c}-${DPI}"
488 name="$name ${main}|${lpN}|${spool}|${comment}"
489 done
490 ;;
491 raw)
492 #
493 # if raw=0 we have the default entry belonging to `lp'
494 main="${PRINTER}-raw"
495 [ -n "$PNAME" ] && main="${PNAME}-raw"
496 [ $raw -eq 0 ] && main="raw"
497 raw=$((raw + 1))
498 lpN="lp${lp}" ; lp=$((lp + 1))
499 comment="${PRINTER}~${PAPER}~${m}"
500 spool="${PRINTER}-${PAPER}-${m}"
501 name="$name ${main}|${lpN}|${spool}|${comment}"
502 ;;
503 remote)
504 main="${SHOST}-${RPRINTER}"
505 [ -n "$PNAME" ] && main="$PNAME"
506 [ $remote -eq 0 ] && main="remote"
507 remote=$((remote + 1))
508 lpN="lp${lp}" ; lp=$((lp + 1))
509 comment="${SHOST}~${RPRINTER}"
510 spool="${SHOST}-${RPRINTER}"
511 name="$name ${main}|${lpN}|${spool}|${comment}"
512 ;;
513 esac
514 done
515
516 #
517 # Now append the new entry
518 #
519 printpc "${SLABEL}"
520 printlg " ${LABEL}"
521 printpc "${WLABEL}"
522 printpc "${DLABEL}"
523 printpc "#"
524 for d in $name ; do
525 # comment : last name in printer names is a comment
526 # SPOOLDIR : the spool direcrory for any possibility
527 main="$(echo $d|tr '~' ' ')"
528 FILTER="$(echo $d|cut -d'|' -f3)"
529 SPOOLDIR="${SPOOL}/${FILTER}"
530 if [ -n "$HOST" ] ; then
531 RP="$(echo $FILTER|sed "s/${SHOST}\-//")"
532 fi
533 #
534 #
535 # write main entry
536 printlg "${main}"
537 printpc "${main}:\\"
538 #
539 # the rest
540 if [ -n "$HOST" ] ; then
541 # lp : lp=NULL ... there is no local printer device
542 # rm : the remote printer server
543 # rp : the remote printer queue (apsfilter conform)
544 printpc "\t:lp=:\\"
545 printpc "\t:rm=${HOST}:\\"
546 printpc "\t:rp=${RP}:\\"
547 else
548 # lp : we need a parallel or serial printer device
549 printpc "\t:lp=${DEVICE}:\\"
550 if [ -n "$SERIAL" ] ; then
551 # br : baud rate for a serial printer device
552 # ty : some settings on a serial device (man stty)
553 printpc "\t:br#${SERIAL}:\\"
554 printpc "\t:ty=ixon -imaxbel -ixany -ixoff -crtscts:\\"
555 fi
556 fi
557 # sd : our spool directory
558 # lf : loging goes to log file
559 # af : the accouting file
560 # lo : we should lock for printer job control
561 printpc "\t:sd=${SPOOLDIR}:\\"
562 printpc "\t:lf=${SPOOLDIR}/log:\\"
563 printpc "\t:af=${SPOOLDIR}/acct:\\"
564 #
565 # This should be done automatically by lpd
566 # Note: bsd-lpd only want lock without fully path
567 #
568 #printpc "\t:lo=${SPOOLDIR}/lock:\\"
569 if [ -n "$HOST" ] ; then
570 # ar : write remote accounting informations
571 # bk : plp should be bsd lpd compatible
572 # mx : No limit on mx=0
573 printpc "\t:ar:bk:mx#0:\\"
574 else
575 # if : our filter
576 # la : @ means do not write local accounting informations
577 # mx : No limit on mx=0
578 printpc "\t:if=${APSFILTER}/bin/${FILTER}:\\"
579 printpc "\t:la@:mx#0:\\"
580 fi
581 # sh : no banner pages
582 # sf : no page feeds between jobs
583 if [ "$NO_FF_SEP" = "yes" ] ; then
584 printpc "\t:sh:sf:"
585 else
586 printpc "\t:sh:"
587 fi
588 printpc "#"
589 #
590 # Now should initialize the hole queue
591 #
592 if [ ! -d ${SPOOLDIR} ] ; then
593 mkdir -p ${SPOOLDIR}
594 : > ${SPOOLDIR}/log
595 : > ${SPOOLDIR}/acct
596 : > ${SPOOLDIR}/lock
597 : > ${SPOOLDIR}/status
598 chown -R ${LP_OWNER}:${LP_GROUP} ${SPOOLDIR} > /dev/null 2>&1
599 chmod 0755 ${SPOOLDIR} > /dev/null 2>&1
600 chmod 0640 ${SPOOLDIR}/log > /dev/null 2>&1
601 chmod 0640 ${SPOOLDIR}/acct > /dev/null 2>&1
602 chmod 0640 ${SPOOLDIR}/lock > /dev/null 2>&1
603 chmod 0640 ${SPOOLDIR}/status > /dev/null 2>&1
604 fi
605 #
606 # The name of the game :-)
607 #
608 if [ -z "$HOST" ] ; then
609 [ -d ${APSFILTER}/bin/ ] || mkdir -p ${APSFILTER}/bin/
610 rm -f ${APSFILTER}/bin/${FILTER}
611 [ -e ${APSFILTER}/bin/${FILTER} ] || \
612 ln -sf ../apsfilter ${APSFILTER}/bin/${FILTER}
613 fi
614 done
615 printpc "${ELABEL}"
616
617 #
618 # Global resource file
619 #
620 [ -f "$APSRC" ] || cp $APSFILTER/template/apsfilterrc $APSRC
621
622 #
623 # Remote printer
624 #
625 [ -n "$HOST" ] && exit 0
626
627 #
628 # A local printer needs its own resource file
629 #
630 TEMPLATE="$APSFILTER/template/apsfilterrc.gs_device_name"
631 if [ ! -f "${APSRC}.${PRINTER}" ] ; then
632 if [ -n "$DVIPS" ] ; then
633 sed "s/^#DVIPS_MODE=/DVIPS_MODE=-P${DVIPS}/;\
634 s/<gs_device_name>/${PRINTER}/g" < ${TEMPLATE} > ${APSRC}.${PRINTER}
635 else
636 sed "s/<gs_device_name>/${PRINTER}/g" < ${TEMPLATE} > ${APSRC}.${PRINTER}
637 fi
638 else
639 if [ -n "$DVIPS" ] ; then
640 cp ${APSRC}.${PRINTER} ${APSRC}.${PRINTER}.old
641 sed "s/^\(\|#\)\(DVIPS_MODE=\)\([^#]*\)/\2-P${DVIPS}/" \
642 < ${APSRC}.${PRINTER}.old > ${APSRC}.${PRINTER}
643 fi
644 fi
645 #
646 # Puh ... done
647 #
648 exit 0