File: apsfilter.in

package info (click to toggle)
apsfilter 7.2.6-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,356 kB
  • sloc: sh: 452; ansic: 236; perl: 175; makefile: 124
file content (2074 lines) | stat: -rwxr-xr-x 57,022 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
#! @shell@
# -*- sh -*-

# $ApsCVS: src/apsfilter/bin/apsfilter.in,v 1.212.2.16 2003/05/11 07:58:41 andreas Exp $

##############################################################################
#
#   apsfilter @VERSION@
#   ---------
#
#   Copyright by Andreas Klemm <andreas@apsfilter.org>
#   Copyright 1993 - 2002
#
#   You are permitted to modify and distribute apsfilter in the
#   terms of the GNU Public License (GPL) see excerpt below.
#
#   For Unix Systems With BSD Alike Print Mechanism (lpd, /etc/printcap)
#
#   Supported filetypes
#
#	archives (print summary of various types), ASCII, BMP, data (PCL
#	etc.), DVI, FBM, FIG, FITS, GIF, Group 3 fax, HTML, IFF ILBM, JPEG,
#	Kodak Photo CD, MGR, MIFF, PAM/PBM/PGM/PNM/PPM, PDF, PNG, PostScript,
#	RLE, SGI, Sketch, Sun raster, Targa, TGIF, TIFF, troff, WMF,
#	WordPerfect graphics, XCF (gimp), X pixmap, X window dump
#
#   Supported compression types
#
#	bzip2, gzip, compress, freeze, pack, lzop
#
##############################################################################

##############################################################################
#
#                       C O P Y R I G H T
#
##############################################################################
#
# You are permitted to use this script in the terms of the
#
#                   GNU GENERAL PUBLIC LICENSE
#                      Version 2, June 1991
#
#  Copyright (C) 1989, 1991 Free Software Foundation, Inc.
#                           675 Mass Ave, Cambridge, MA 02139, USA
#  Everyone is permitted to copy and distribute verbatim copies
#  of this license document, but changing it is not allowed.
##############################################################################

##############################################################################
# Start Up
##############################################################################

#
# For easy debugging of apsfilter, please use the "-D" flag to "aps2file"
#

#-----------------------------------------------------------------------------
# Set a basic PATH; might have to be extended in
# @sysconfdir@/apsfilter/apsfilterrc
#-----------------------------------------------------------------------------

PATH="/usr/local/bin:/usr/X11R6/bin:/usr/bin:/bin"

#-----------------------------------------------------------------------------
# restrictive umask; needed because private tmp dir must be rwx--x--x
#-----------------------------------------------------------------------------

umask 077

#-----------------------------------------------------------------------------
# Ignore hang up and broken pipe signals.
#-----------------------------------------------------------------------------

trap '' 1 13

#-----------------------------------------------------------------------------
# Remove temporary files before terminating.
#-----------------------------------------------------------------------------

trap 'rc=`cat "$APS_TMPDIR/rc"`; rm -rf "$APS_TMPDIR"; exit $rc' 0

# return code 9: JSIGNAL (for LPRng)
# these are SIGINT SIGTRAP SIGBUS SIGUSR1 SIGUSR2 SIGTERM
trap 'echo 9 >"$APS_TMPDIR/rc"; exit' 1 5 7 10 12 15

##############################################################################
# Error Reporting Functions
#
# These are guaranteed to work no matter what.
##############################################################################

#=============================================================================
# fatal RETURN_CODE SUMMARY [LINE...]
#
# This is the generic interface for reporting fatal errors.
#
# RETURN_CODE is an integer number that gives the spooler a hint what kind
# of error has occured (mostly for LPRng; BSD-lpr doesn't seem to care,
# as long as the error code is >0)
#	1 (JFAIL):	transient error condition; retry might succeed
#	2 (JABORT):	fatal error; retry probably won't succeed
#
# SUMMARY is a concise one-line message describing the error.  The rest of the
# arguments offer a verbose explanation, and list possible solutions to the
# problem.
#
# Someday soon it will be possible to disable the verbose parts, e.g. for
# experienced users, or large sites, to keep the logs short.
#=============================================================================

fatal()
{
    local line

    echo $1 >"$APS_TMPDIR/rc"; shift

    for line; do echo "apsfilter: $line"; done >&2

    if [ -z "$APS2FILE_CONTEXT" ]; then
	{
	    echo "To: ${NOTIFY:-root}"
	    echo "Cc: $USER${HOST:+@$HOST}"
	    echo "Subject: apsfilter: $1"
	    echo
	    echo "apsfilter fatal error: $1"
	    echo
	    shift
	    for line; do echo "$line"; done
	    echo
	    echo
	    echo "-- apsfilter, your lpd input filter"
	} | "@sendmail@" -oem -t
    fi

    exit
}

#=============================================================================
# Error messages.
#
# All of them are in one place, here, to facilitate localization, and to make
# the code look nicer.
#=============================================================================

fatal_basedir()
{
    fatal 2 "can't find apsfilter basedir" \
	    "Please adjust $CONF_DIR/basedir or run SETUP."
}

fatal_config()
{
    fatal 2 "can't find configuration" \
	    "The configuration file $CONF_DIR/$QUEUE/apsfilterrc" \
	    "is missing. Please run SETUP."
}

fatal_driver_script()
{
    fatal 2 "driver script '$DRIVER_SCRIPT' for '$PRINTER' not found" \
	    "The driver script '$DRIVER_SCRIPT' which sets" \
	    "the options for the printer '$PRINTER'" \
	    "was not found. The file driver/MAPPING is corrupt."
}

fatal_file_type()
{
    fatal 2 "unsupported file type '${FILE_TYPE%%,*}'" \
	    "If you think you can help us to support files of this type," \
	    "please contact us on <apsfilter-hackers@apsfilter.org>."
}

fatal_filter()
{
    fatal 2 "missing $1; can't convert file type '${FILE_TYPE%%,*}'" \
	    "Please install $1 if you want to be able to print files of" \
	    "this type."
}

fatal_method()
{
    fatal 2 "invalid method '$METHOD'" \
	  "Please adjust $CONF_DIR/$QUEUE/apsfilterrc." \
	  "Method must be 'auto', 'ascii' or 'raw'."
}

fatal_spooldir()
{
    fatal 2 "can't determine the lpd spool directory" \
	  "Please fix your lpd or stop printing to remote printers" \
	  "directly."
}

fatal_tmpdir()
{
    fatal 1 "error creating directory for temporary files" \
	  "Clean up the '$TMPDIR' directory or change the value of" \
	  "TMPDIR."
}

fatal_unpacker()
{
    fatal 2 "missing $1; can't unpack file type '${FILE_TYPE%%,*}'" \
	  "Please install $1 if you want to be able to print files of" \
	  "this type."
}

##############################################################################
# Auxiliary Functions
#
# These are guaranteed not to overwrite your local variables.
##############################################################################

#=============================================================================
# find_filter FILTER
#
# Return false unless FILTER is found in the $PATH.
#=============================================================================

find_filter()
{
    # Unfortunately, FreeBSD and NetBSD ash doesn't dig `command -v'.
    type "$1" > /dev/null 2>&1
}

##############################################################################
# Functions For Printing
#
# These now all take their input from stdin (again); the previous approach
# (to provide commands as parameters that generate the proper input) would
# only save two (at most three) trivial cat commands, but it was very picky
# w.r.t quoting -- and also a pain to read and debug.
##############################################################################

#=============================================================================
# do_convert [FORMAT]
#
# Print all kinds of images.
#=============================================================================

do_convert()
{
    local opts

    case "$COLOR" in
    	gray)	opts="-colorspace GRAY" ;;
	mono)	opts="-monochrome" ;;
    esac
    eval convert -rotate '"-90>"' -page '"${WIDTH_POINTS}x${HEIGHT_POINTS}>"' \
	$opts ${1:+${1}:}- ps:- | print_ps
}

#=============================================================================
# do_nconvert [OPTION...]
#
# Print all kinds of images.
#=============================================================================

do_nconvert()
{
    local opts

    [ "$COLOR" = full ] && opts="-truecolors" || opts="-grey 256"
    eval nconvert -quiet $opts -out pnm "$@" -o /dev/stdout /dev/stdin \
	| print_pnm
}

#=============================================================================
# print_ar
#
# Print a summary of an AR archive.
#=============================================================================

print_ar()
{
    find_filter ar || fatal_filter ar
    cat > "$APS_TMPDIR/ar"
    ar -tv "$APS_TMPDIR/ar" | print_ascii
    rm -f "$APS_TMPDIR/ar"
}

#=============================================================================
# print_arc
#
# Print a summary of an ARC archive.
#=============================================================================

print_arc()
{
    if find_filter unarc; then
	unarc v /dev/stdin | print_ascii
    else
	find_filter arc || fatal_filter arc
	arc v /dev/stdin | print_ascii
    fi
}

#=============================================================================
# print_arj
#
# Print a summary of an ARJ archive.
#=============================================================================

print_arj()
{
    find_filter unarj || fatal_filter unarj
    cat > "$APS_TMPDIR/archive.arj"
    unarj l "$APS_TMPDIR/archive.arj" | print_ascii
    rm -f "$APS_TMPDIR/archive.arj"
}

#=============================================================================
# print_ascii
#
# Print ASCII files.
#=============================================================================

print_ascii()
{
    local features jobname lang

    jobname="$JOB"
    if [ `echo "$jobname" | wc -c` -gt 40 ]; then
	# try shortening excessively long job name
	jobname=`basename $jobname`
    fi

    case "$ASCII_FILTER" in
	mpage)
	    find_filter mpage || fatal_filter mpage

	    if [ "$MPAGE_OPTS" ]; then
		eval mpage $MPAGE_OPTS | print_ps
	    else
		: ${MPAGE_PAPERSIZE:=$PAPERSIZE}

		case $ASCII_PPS in
		    2|4|8)	features="-$ASCII_PPS" ;;
		    *)		features="-1" ;;
		esac
		unset LANDSCAPE PS_NUP

		eval mpage -b $MPAGE_PAPERSIZE -da \
		    ${MPAGE_BASIC:--CISO-Latin.1 -f} ${ASCII_BORDER:+-B} \
		    ${ASCII_HEADER:+-H} $features ${ASCII_LANDSCAPE:+-l} \
		    | print_ps
	    fi
	    ;;
	enscript)
	    find_filter enscript || fatal_filter enscript

	    if [ "$ENSCRIPT_OPTS" ]; then
		eval enscript $ENSCRIPT_OPTS -q -p - | print_ps
	    else
		if [ ! "$ENSCRIPT_PAPERSIZE" ]; then
		    case "$PAPERSIZE" in
			letter) ENSCRIPT_PAPERSIZE=Letter ;;
			legal)  ENSCRIPT_PAPERSIZE=Legal  ;;
			a3)     ENSCRIPT_PAPERSIZE=A3     ;;
			a4)     ENSCRIPT_PAPERSIZE=A4     ;;
			a5)     ENSCRIPT_PAPERSIZE=A5     ;;
			*)      ENSCRIPT_PAPERSIZE=Letter ;;
		    esac
		fi

		case "$PRINTER" in
		    cdesk*|cdj*|desk*|djet*|hpdj|pcl3)
			case "$ENSCRIPT_PAPERSIZE" in
			    Letter|A4)
				ENSCRIPT_PAPERSIZE="$ENSCRIPT_PAPERSIZE"dj ;;
			esac
			;;
		esac

		case $ASCII_PPS in
		    1|2|4|8)	features="-U $ASCII_PPS" ;;
		    *)		features="" ;;
		esac
		unset LANDSCAPE PS_NUP

		if [ "$PRETTY_PRINTING" != 0 ]; then
		    # enscript is not very smart about file magic
		    case "$FILE_TYPE" in
			*awk*)			lang=awk ;;
			*c\ *program*)		lang=c ;;
			*c++*)			lang=cpp ;;
			*python*)		lang=python ;;
			*postscript*)		lang=postscript ;;
			*diff*)			lang=diff ;;
			*html*)			lang=html ;;
			*mail*|*news*)		lang=mail ;;
			*perl*)			lang=perl ;;
			*emacs*lisp*)		lang=elisp ;;
			*lisp*|*scheme*)	lang=scheme ;;
			*shell*|*script*)	lang=sh ;;
		    esac
		fi

		: ${ENSCRIPT_BASIC:=-X 88591}
		if [ "$COLOR" = full ]; then
		    features="$features --color"
		else
		    features="$features --color=blackwhite"
		fi
		eval enscript -M $ENSCRIPT_PAPERSIZE ${ASCII_BORDER:+-j} \
		    $ENSCRIPT_BASIC ${lang:+-E$lang} -t '"$jobname"' \
		    ${ASCII_HEADER:+-b "'%D|\$n|Page \$% of \$='"} $features \
		    ${ASCII_LANDSCAPE:+-r} -q -p - | print_ps
	    fi
	    ;;
	recode)
	    find_filter recode || fatal_filter recode
	    {
		[ "$RECODE_PROLOGUE" ] && printf $RECODE_PROLOGUE
		eval recode -q ${RECODE_OPTS:-latin1..ibmpc}
		[ "$RECODE_EPILOGUE" ] && printf $RECODE_EPILOGUE
	    } | print_raw
	    ;;
	*)
	    find_filter a2ps || fatal_filter a2ps

	    if [ "$A2PS_OPTS" ]; then
		eval a2ps $A2PS_OPTS -q -o - | print_ps
	    else
		: ${A2PS_PAPERSIZE:=$PAPERSIZE}
		case "$PRINTER" in
		    cdesk*|cdj*|desk*|djet*|hpdj|pcl3)
			case "$A2PS_PAPERSIZE" in
			    Letter|letter|A4|a4)
				A2PS_PAPERSIZE="$A2PS_PAPERSIZE"dj ;;
			esac
			;;
		esac

		case $ASCII_PPS in
		    2|4|8)	features="-$ASCII_PPS" ;;
		    *)		features="-1" ;;
		esac
		unset LANDSCAPE PS_NUP

		# a2ps is not very smart about file magic, either
		case "$FILE_TYPE" in
		    *awk*)		lang=awk ;;
		    *c\ *program*)	lang=c ;;
		    *c++*)		lang=cxx ;;
		    *roff*)		lang=roff ;;
		    *python*)		lang=python ;;
		    *postscript*)	lang=ps ;;
		    *html*)		lang=html ;;
		    *mail*|*news*)	lang=mail ;;
		    *perl*)		lang=perl ;;
		    *scheme*)		lang=scheme ;;
		    *emacs*lisp*)	lang=elisp ;;
		    *lisp*)		lang=clisp ;;
		    *c[-\ ]shell*)	lang=csh ;;
		    *shell*|*script*)	lang=sh ;;
		esac

		: ${A2PS_BASIC:=--delegate=no -X iso1}
		[ "$ASCII_BORDER" ] || features="$features --borders=no"
		[ "$ASCII_HEADER" ] || features="$features --no-header"
		case "$COLOR" in
		    full)  features="$features --prologue=color" ;;
		    gray)  features="$features --prologue=gray" ;;
		    mono)  features="$features --prologue=bw" ;;
		esac
		case "$PRETTY_PRINTING" in
		    0)	features="$features --highlight-level=none" ;;
		    1)	features="$features --highlight-level=normal" ;;
		    2)	features="$features --highlight-level=heavy" ;;
		esac

		eval a2ps -M $A2PS_PAPERSIZE --center-title='"$jobname"' \
		    -b'"Printed by $USER from $HOST"' $A2PS_BASIC \
		    $features ${lang:+-E$lang} ${ASCII_LANDSCAPE:+-r} -q \
		    -o - | print_ps
	    fi
	    ;;
    esac
}

#=============================================================================
# print_auto
#
# Print input after using file to determine how.
#=============================================================================

print_auto()
{
    # these patterns are based on the magic database of file-3.37,
    # with some additions

    case "$FILE_TYPE" in
	arc\ archive*)
	    print_arc		;;
	arj\ archive*)
	    print_arj		;;
	fbm\ image*)
	    print_fbm		;;
	fig\ image*)
	    print_fig		;;
	fits\ image*)
	    print_fits		;;
	gif\ image*)
	    print_gif		;;
	gimp\ xcf\ image*)
	    print_xcf		;;
	group\ 3\ fax*|raw\ g3\ data*)
	    print_g3		;;
	html\ document*|exported\ sgml*|\
	iso-8859\ html\ document*|iso-8859\ exported\ sgml*)
	    print_html		;;
	iff*ilbm*)
	    print_ilbm		;;
	jpeg\ image*)
	    print_jpeg		;;
	kodak\ photo\ cd*)
	    print_pcd		;;
	lha*archive*)
	    print_lha		;;
	mgr\ bitmap*)
	    print_mgr		;;
	microsoft\ cabinet*)
	    print_cab		;;
	miff\ image*)
	    print_miff		;;
	ms-windows*wmf*)
	    print_wmf		;;
	pam*image*|netpbm\ pam*image*)
	    print_pam		;;
	pbm*image*|netpbm\ pbm*image*|\
	pnm*image*|netpbm\ pnm*image*|\
	ppm*image*|netpbm\ ppm*image*)
	    print_pnm		;;
	pc\ bitmap*)
	    print_bmp		;;
	pdf\ document*)
	    print_pdf		;;
	pgm*image*|netpbm\ pgm*image*)
	    print_pgm		;;
	png\ image*)
	    print_png		;;
	posix\ tar\ archive*|gnu\ tar\ archive*)
	    print_tar		;;
	postscript\ document*)
	    print_ps		;;
	rar\ archive*)
	    print_rar		;;
	rle\ image*)
	    print_rle		;;
	rpm*)
	    print_rpm		;;
	sgi\ image*)
	    print_sgi		;;
	sketch\document*)
	    print_sketch   	;;
	sun\ raster\ image*|rasterfile*)
	    print_ras		;;
	targa\ image*)
	    print_tga		;;
	tex\ dvi*)
	    print_dvi		;;
	tiff\ image*)
	    print_tiff		;;
	tgif\ file*)
	    print_tgif		;;
	troff*)
	    print_troff		;;
	wordperfect\ graphic*)
	    print_wpg		;;
	x\ pixmap\ image*)
	    print_xpm		;;
	xwd*image*)
	    print_xwd		;;
	zip\ archive*)
	    print_zip		;;
	zoo\ archive*)
	    print_zoo		;;

	# patterns which might cause a mismatch (mostly via strings in
	# file(1)'s output that appear in free form)
	*ar\ archive*)
	    print_ar		;;
	*cpio\ archive*)
	    print_cpio		;;

	# generic patterns
	*mail*|*news*|*ascii*|*text*|*english*|*script*)
	    print_ascii		;;
	*data*|*escape*|*pcl*|*pjl*|*printer*job*language*|*ms*windows*)
	    print_data		;;
	*)
	    fatal_file_type	;;
    esac
}

#=============================================================================
# print_bmp
#
# Print (Windows, OS/2) BMP images.
#=============================================================================

print_bmp()
{
    if find_filter convert; then
	do_convert bmp
    elif find_filter nconvert; then
	do_nconvert -in bmp
    else
	find_filter bmptoppm || fatal_filter bmptoppm
	bmptoppm | print_pnm
    fi
}

#=============================================================================
# print_cab
#
# Print a summary of a CAB archive.
#=============================================================================

print_cab()
{
    find_filter cabextract || fatal_filter cabextract
    cat > "$APS_TMPDIR/cab"
    cabextract -l "$APS_TMPDIR/cab" | print_ascii
    rm -f "$APS_TMPDIR/cab"
}

#=============================================================================
# print_cpio
#
# Print a summary of a CPIO archive.
#=============================================================================

print_cpio()
{
    find_filter cpio || fatal_filter cpio
    cpio -tv --quiet | print_ascii
}

#=============================================================================
# print_data
#
# Print data.
#=============================================================================

print_data()
{
    local c

    print_copy()
    {
	local rc=0 ncp_print

	if [ "$APS2FILE_CONTEXT" ]; then
	    # we were called by aps2file -> output to stdout
	    cat
	elif [ -f "$CONF_DIR/$QUEUE/smbclient.conf" ]; then
	    find_filter smbclient || fatal_filter smbclient
	    . "$CONF_DIR/$QUEUE/smbclient.conf"
	    [ "$SMB_PASSWD" ] && export PASSWD="$SMB_PASSWD"
	    eval smbclient "'//$SMB_SERVER/$SMB_PRINTER'" \
		${SMB_IP:+-I"$SMB_IP"} ${SMB_USER:+-U"$SMB_USER"} \
		${SMB_WORKGROUP:+-W"$SMB_WORKGROUP"} -b"${SMB_BUFFER:-1400}" \
		${SMB_FLAGS:--N} -c "'print -'"
	    rc=$?
	elif [ -f "$CONF_DIR/$QUEUE/netware.conf" ]; then
	    . "$CONF_DIR/$QUEUE/netware.conf"
	    if find_filter ncprint; then
		# BSD systems have "ncprint"
		ncp_print=ncprint
		# create temporary .nwfsrc file (for security)
		{
		    echo "[${NCP_SERVER}:${NCP_USER}]"
		    echo "password=$NCP_PASSWD"
		} > "$APS_TMPDIR/.nwfsrc"
		chmod 600 "$APS_TMPDIR/.nwfsrc"
	    else
		find_filter nprint || fatal_filter ncprint/nprint
		# Linux systems have "nprint"
		ncp_print=nprint
		# create temporary .nwclient file (for security)
		echo "$NCP_SERVER/$NCP_USER ${NCP_PASSWD:--}" \
		    > "$APS_TMPDIR/.nwclient"
		chmod 600 "$APS_TMPDIR/.nwclient"
	    fi
	    # fake $HOME to make .nwfsrc and .nwclient visible
	    HOME="$APS_TMPDIR" $ncp_print -S "$NCP_SERVER" -U "$NCP_USER"\
		-q "$NCP_PRINTER" -c $COPIES
	    rc=$?
	elif [ -f "$CONF_DIR/$QUEUE/lpr.conf" ]; then
	    . "$CONF_DIR/$QUEUE/lpr.conf"
	    lpr -P"$REMOTE_NAME" -\#"$COPIES"
	    rc=$?
	elif [ -f "$CONF_DIR/$QUEUE/pap.conf" ]; then
	    find_filter pap || fatal_filter pap
	    . "$CONF_DIR/$QUEUE/pap.conf"
	    pap -e -p "$PAP_NBPNAME" -s "$SPOOLDIR/pap.status"
	    rc=$?
	else
	    cat
	fi
	if [ $rc -ne 0 ]; then
	    echo 2 >"$APS_TMPDIR/rc"; exit
	fi
    }

    if [ "$SAVE_DUPLEX_COPY" ]; then
	# print_ps_duplex() does the manual copying
	# we append here to include the (optional) blank page
	tee -a "$APS_TMPDIR/copy-$SAVE_DUPLEX_COPY" | print_copy
    else
	if [ "$COPIES" = 1 -o -n "$REMOTE_COPIES" ]; then
	    print_copy
	else
	    tee "$APS_TMPDIR/copy" | print_copy
	    for c in $(seq 2 "$COPIES"); do
		print_copy < "$APS_TMPDIR/copy"
	    done
	    rm -f "$APS_TMPDIR/copy"
	fi
    fi
}

#=============================================================================
# print_dvi
#
# Print DVI files.
#=============================================================================

print_dvi()
{
    local resolution_x options

    export TEXINPUTS="$TEXINPUTS:$HOMEDIR:/tmp:/var/tmp"

    cat > "$APS_TMPDIR/dvi"
    if [ "$PRINT_DVI" ]; then
	eval $PRINT_DVI < "$APS_TMPDIR/dvi" | print_data
    else
	find_filter dvips || fatal_filter dvips

	[ "$HAVE_MAKETEXPK" ] || options="-M"

	resolution_x="${RESOLUTION%%[!0-9]*}"
	[ "$resolution_x" -gt 400 ] && options="$options -Z"
	[ "$LANDSCAPE" ] && options="$options -t landscape"
	PSNUP_ROTATE="-r"
	: ${DVIPS_PAPERSIZE:=$PAPERSIZE}

	eval dvips ${DVIPS_RES_DorP:--D} $resolution_x -R -f -q \
	    -t "$DVIPS_PAPERSIZE" $options < "$APS_TMPDIR/dvi" | print_ps
    fi
    rm -f "$APS_TMPDIR/dvi"
}

#=============================================================================
# print_fbm
#
# Print Fuzzy Bitmap images.
#=============================================================================

print_fbm()
{
    find_filter nconvert || fatal_filter nconvert
    cat > "$APS_TMPDIR/fbm"
    do_nconvert -in fbm < "$APS_TMPDIR/fbm"
    rm -f "$APS_TMPDIR/fbm"
}

#=============================================================================
# print_fig
#
# Print xfig files.
#=============================================================================

print_fig()
{
    if find_filter fig2dev; then
	fig2dev -Lps -c -z "$PAPERSIZE" | print_ps
    else
	find_filter sk2ps || fatal_filter sk2ps
	sk2ps /dev/stdin | print_ps
    fi
}

#=============================================================================
# print_fits
#
# Print FITS images.
#
# Note: Should 3-axes-files be handled as three grayscale images (convert) or
#	as RGB input, resulting in one color image (nconvert, fitstopnm)?
#=============================================================================

print_fits()
{
    if find_filter nconvert; then
	cat > "$APS_TMPDIR/fits"
	do_nconvert -in fits < "$APS_TMPDIR/fits"
	rm -f "$APS_TMPDIR/fits"
    elif find_filter fitstopnm; then
	# fitstopnm output "may" need to be flipped top for bottom, according
	# to the man page -- you gotta be kidding...
	fitstopnm | print_pnm
    else
	find_filter convert || fatal_filter convert
	do_convert fits
    fi
}

#=============================================================================
# print_g3
#
# Print Group 3 faxes.
#=============================================================================

print_g3()
{
    local options

    if find_filter convert; then
	do_convert fax
    elif find_filter nconvert; then
	do_nconvert -in fax
    else
	find_filter pbmtolps || fatal_filter pbmtolps
	case "$FILE_TYPE" in
	    *normal*resolution*)  options=-s ;;
	esac
	if find_filter g32pbm; then
	    g32pbm $options
	else
	    find_filter g3topbm || fatal_filter g3topbm
	    g3topbm $options
	fi | pbmtolps -dpi 204x196 | print_ps
    fi
}

#=============================================================================
# print_gif
#
# Print GIF images.
#=============================================================================

print_gif()
{
    if find_filter convert; then
	do_convert gif
    elif find_filter nconvert; then
	do_nconvert -in gif
    else
	find_filter giftopnm || fatal_filter giftopnm
	giftopnm | print_pnm
    fi
}

#=============================================================================
# print_html
#
# Print HTML files.
#=============================================================================

print_html()
{
    local opts paper

    if find_filter htmldoc; then
	[ "$COLOR" = full ] && opts="--color" || opts="--gray"
	eval htmldoc ${HTMLDOC_OPTS:---webpage} $opts --format ps \
	    ${LANDSCAPE:+--landscape} ${PORTRAIT:+--portrait} \
	    --size ${WIDTH_POINTS}x${HEIGHT_POINTS}pt - | print_ps
    else
	find_filter html2ps || fatal_filter html2ps
	if ! find_filter gs; then
	    echo >&2 "apsfilter warning: html2ps needs gs for DSC compliance"
	fi

	[ "$COLOR" = full ] && opts="-U" || opts="-g"
	[ "$PAPERSIZE" = tabloid ] && paper=11x17 || paper="$PAPERSIZE"
	eval html2ps ${HTML2PS_OPTS:--e ISO-8859-1 -u -H} $opts \
	    ${LANDSCAPE:+-L} -D -S '"paper { type: $paper; }"' | print_ps
    fi
}

#=============================================================================
# print_ilbm
#
# Print IFF ILBM images.
#=============================================================================

print_ilbm()
{
    if find_filter nconvert; then
	cat > "$APS_TMPDIR/ilbm"
	do_nconvert -in lbm < "$APS_TMPDIR/ilbm"
	rm -f "$APS_TMPDIR/ilbm"
    else
	find_filter ilbmtoppm || fatal_filter ilbmtoppm
	ilbmtoppm | print_pnm
    fi
}

#=============================================================================
# print_jpeg
#
# Print JPEG images.
#=============================================================================

print_jpeg()
{
    if find_filter convert; then
	do_convert jpeg
    elif find_filter nconvert; then
	do_nconvert -in jpeg
    elif find_filter djpeg; then
	if [ "$COLOR" = full ]; then
	    djpeg | print_pnm
	else
	    djpeg -grayscale | print_pgm
	fi
    else
	find_filter jpegtopnm || fatal_filter jpegtopnm
	jpegtopnm | print_pnm
    fi
}

#=============================================================================
# print_lha
#
# Print a summary of a LHa(rc) archive.
#=============================================================================

print_lha()
{
    find_filter lha || fatal_filter lha
    lha v - | print_ascii
}

#=============================================================================
# print_mgr
#
# Print MGR images.
#=============================================================================

print_mgr()
{
    if find_filter mgrtopbm; then
	mgrtopbm | print_pnm
    else
	find_filter nconvert || fatal_filter nconvert
	cat > "$APS_TMPDIR/mgr"
	do_nconvert -in mgr < "$APS_TMPDIR/mgr"
	rm -f "$APS_TMPDIR/mgr"
    fi
}

#=============================================================================
# print_miff
#
# Print MIFF images.
#=============================================================================

print_miff()
{
    if find_filter convert; then
	# convert doesn't seem to like an explicit "miff" here, at least for
	# images with an alpha (matte) channel -- weird
	do_convert
    else
	find_filter nconvert || fatal_filter nconvert
	do_nconvert -in miff
    fi
}

#=============================================================================
# print_pam
#
# Print PAM (xv thumbnail) images.
#=============================================================================

print_pam()
{
    if find_filter convert; then
	do_convert p7
    elif find_filter xvminitoppm; then
	xvminitoppm | print_pnm
    elif find_filter xvpictoppm; then
	xvpictoppm | print_pnm
    else
	find_filter nconvert || fatal_filter nconvert
	cat > "$APS_TMPDIR/pam"
	do_nconvert -in p7 < "$APS_TMPDIR/pam"
	rm -f "$APS_TMPDIR/pam"
    fi
}

#=============================================================================
# print_pcd
#
# Print Kodak Photo CD images.
#=============================================================================

print_pcd()
{
    if find_filter convert; then
	do_convert pcd
    elif find_filter nconvert; then
	cat > "$APS_TMPDIR/pcd"
	do_nconvert -in pcd < "$APS_TMPDIR/pcd"
	rm -f "$APS_TMPDIR/pcd"
    elif find_filter pcdtoppm; then
	cat > "$APS_TMPDIR/pcd"
	if [ "$COLOR" = full ]; then
	    pcdtoppm "$APS_TMPDIR/pcd" - | print_pnm
	else
	    pcdtoppm --gray "$APS_TMPDIR/pcd" - | print_pgm
	fi
	rm -f "$APS_TMPDIR/pcd"
    else
	find_filter hpcdtoppm || fatal_filter hpcdtoppm
	cat > "$APS_TMPDIR/pcd"
	hpcdtoppm "$APS_TMPDIR/pcd" | print_pnm
	rm -f "$APS_TMPDIR/pcd"
    fi
}

#=============================================================================
# print_pdf
#
# Print PDF files.
#=============================================================================

print_pdf()
{
    local paper

    ACROREAD_OPTS="-toPostScript -size ${WIDTH_POINTS}x${HEIGHT_POINTS} \
		${ACROREAD_OPTS:--level2 -fast}"

    cat > "$APS_TMPDIR/pdf"
    if find_filter acroread5; then
	eval acroread5 $ACROREAD_OPTS -pairs "$APS_TMPDIR/pdf" /dev/stdout
    elif find_filter acroread4; then
	eval acroread4 $ACROREAD_OPTS -pairs "$APS_TMPDIR/pdf" /dev/stdout
    elif find_filter acroread; then
	eval acroread $ACROREAD_OPTS -pairs "$APS_TMPDIR/pdf" /dev/stdout
    elif find_filter pdftops; then
	pdftops ${PDFTOPS_OPTS:--q} -paperw $WIDTH_POINTS \
	    -paperh $HEIGHT_POINTS "$APS_TMPDIR/pdf" -
    else
	find_filter pdf2ps || fatal_filter pdf2ps
	[ "$PAPERSIZE" = tabloid ] && paper=11x17 || paper="$PAPERSIZE"
	pdf2ps -sPAPERSIZE="$paper" "$APS_TMPDIR/pdf" -
    fi | print_ps
    rm -f "$APS_TMPDIR/pdf"
}

#=============================================================================
# print_pgm
#
# Print PGM (greyscale) images.
#=============================================================================

print_pgm()
{
    if find_filter convert; then
	do_convert pgm
    else
	find_filter pnmdepth || fatal_filter pnmdepth
	find_filter pnmtops || fatal_filter pnmtops
	pnmdepth 255 | pnmtops -width $WIDTH_INCHES -height $HEIGHT_INCHES \
	    2> /dev/null | print_ps
    fi
}

#=============================================================================
# print_png
#
# Print PNG images.
#=============================================================================

print_png()
{
    case "$FILE_TYPE" in
	*rgba*|*alpha*)
	    # Only pngtopnm can handle the alpha channel.
	    find_filter pngtopnm || fatal_filter pngtopnm
	    pngtopnm -mix -background "#fff" | print_pnm
	    ;;
	*)
	    if find_filter convert; then
		do_convert png
	    elif find_filter nconvert; then
		do_nconvert -in png
	    else
		find_filter pngtopnm || fatal_filter pngtopnm
		pngtopnm | print_pnm
	    fi
	    ;;
    esac
}

#=============================================================================
# print_pnm
#
# Print PNM/PBM/PPM images.
#=============================================================================

print_pnm()
{
    if find_filter convert; then
	do_convert pnm
    else
	if [ "$COLOR" = full ]; then
	    find_filter pnmdepth || fatal_filter pnmdepth
	    find_filter pnmtops || fatal_filter pnmtops
	    pnmdepth 255 | pnmtops -width $WIDTH_INCHES \
		-height $HEIGHT_INCHES 2> /dev/null | print_ps
	else
	    find_filter ppmtopgm || fatal_filter ppmtopgm
	    ppmtopgm | print_pgm
	fi
    fi
}

#=============================================================================
# print_ps
#
# Print PostScript files.
#=============================================================================

print_ps()
{
    local paper gs_cmd

    if [ "$DUPLEX" ] && [ "$PRINTER" != PS -o -z "$HARDWARE_DUPLEX" ]; then
	unset DUPLEX
	print_ps_duplex
	return
    elif [ "$PRINTER" = PS ]; then
	ps_postprocessing | print_data
	return
    fi

    find_filter gs || fatal_filter gs

    : ${GS_FONTPATH:=/usr/X11R6/lib/X11/fonts/Type1:/var/X11R6/lib/X11/fonts/Type1}
    : ${GS_LIB:=$GS_FONTPATH}
    export GS_FONTPATH GS_LIB

    [ "$PAPERSIZE" = tabloid ] && paper=11x17 || paper="$PAPERSIZE"
    case "$PRINTER" in
	*.upp)
	    gs_cmd="gs -q -dNOPAUSE -dSAFER -dPARANOIDSAFER -sstdout=%stderr \
		@'$PRINTER' -sPAPERSIZE='$paper' -sOutputFile=- \
		${PS_INIT:+'$PS_INIT'} - ${PS_EXIT:+'$PS_EXIT'}" ;;
	*)
	    gs_cmd="gs -q -dNOPAUSE -dSAFER -dPARANOIDSAFER -sstdout=%stderr \
		-sDEVICE='$PRINTER' -sPAPERSIZE='$paper' \
		${RESOLUTION:+-r'$RESOLUTION'} -sOutputFile=- $GS_FEATURES \
		${PS_INIT:+'$PS_INIT'} - ${PS_EXIT:+'$PS_EXIT'}" ;;
    esac
    if [ "$POST_FILTER" ]; then
	find_filter "$POST_FILTER" || fatal_filter "$POST_FILTER"
	gs_cmd="$gs_cmd | '$POST_FILTER' $POST_FILTER_OPTS"
    fi
    ps_postprocessing | eval $gs_cmd | print_data
}

print_ps_duplex()
{
    local copies c is_odd

    print_odd_pages()
    {
	[ "$copies" != 1 ] && SAVE_DUPLEX_COPY=odd
	psselect -q -o ${DUPLEX_REVERSE_ODD:+-r} < "$APS_TMPDIR/duplex.ps" | \
	    print_ps
    }
    print_even_pages()
    {
	[ "$copies" != 1 ] && SAVE_DUPLEX_COPY=even
	[ -n "$is_odd" -a -n "$DUPLEX_REVERSE_EVEN" ] && \
	    printf "$BLANK_PAGE" | print_data
	psselect -q -e ${DUPLEX_REVERSE_EVEN:+-r} < "$APS_TMPDIR/duplex.ps" | \
	    print_ps
	[ -n "$is_odd" -a -z "$DUPLEX_REVERSE_EVEN" ] && \
	    printf "$BLANK_PAGE" | print_data
    }

    # try to set up a special named pipe (FIFO) for communication; has to be
    # world writable
    mkfifo -m 622 "$APS_TMPDIR/duplex-key"

    if [ -p "$APS_TMPDIR/duplex-key" ]; then
	find_filter psselect || fatal_filter psselect

	# make page count even by adding a blank page if needed
	case $(ps_postprocessing | tee "$APS_TMPDIR/duplex.ps" | \
		psselect -p_1 2>&1 >/dev/null | tr "[]" "0\n" | head -1) in
	    01)		# don't bother to print 1 page in duplex mode
			print_ps < "$APS_TMPDIR/duplex.ps"
			rm -f "$APS_TMPDIR/duplex.ps" "$APS_TMPDIR/duplex-key"
			return ;;
	    *[13579])	is_odd=set ;;
	    *)		unset is_odd ;;
	esac

	# don't process PostScript with the pstools again
	SKIP_POSTPROCESSING=set

	copies="$COPIES"; COPIES=1
	if [ "$DUPLEX_ODD_FIRST" ]; then
	    print_odd_pages;  duplex_notification; print_even_pages
	else
	    print_even_pages; duplex_notification; print_odd_pages
	fi
	rm -f "$APS_TMPDIR/duplex.ps"

	# turn on the copy machine!
	if [ "$copies" != 1 ]; then
	    unset SAVE_DUPLEX_COPY
	    if [ "$DUPLEX_ODD_FIRST" ]; then
		for c in $(seq 2 "$copies"); do
		    print_data < "$APS_TMPDIR/copy-odd"
		    duplex_notification
		    print_data < "$APS_TMPDIR/copy-even"
		done
	    else
		for c in $(seq 2 "$copies"); do
		    print_data < "$APS_TMPDIR/copy-even"
		    duplex_notification
		    print_data < "$APS_TMPDIR/copy-odd"
		done
	    fi
	fi
    else
	# the FIFO couldn't be set up, so print the file simplex style
	echo >&2 "apsfilter warning: duplex fifo couldn't be created"
	print_ps
    fi

    rm -f "$APS_TMPDIR/duplex-key"
}

ps_postprocessing()
{
    if [ "$SKIP_POSTPROCESSING" ]; then
	cat
    else
	eval ${PS_BOOK:+psbook -q |} \
	    ${PS_NUP:+psnup -q -$PS_NUP ${LANDSCAPE:+$PSNUP_ROTATE} |} \
	    ps_set_duplex ${PS_UTILS:+| $PS_UTILS}
    fi
}

ps_set_duplex()
{
    local tray

    find_filter psset || fatal_filter psset

    [ "$PAPERTRAY" ] && tray="-Spapertray:$PAPERTRAY"

    if [ "$DUPLEX" ]; then
	if [ "$BINDING" = short ]; then
	    psset -n -q -t $tray
	else
	    psset -n -q -d $tray
	fi
    else
	psset -n -q -s $tray
    fi
}

duplex_notification()
{
    local duplex_passwd key

    # send an instruction mail to the user saying to flip the sheets,
    # put them back in and echo a special key into the FIFO to resume
    # the operation
    duplex_passwd=`"$AWK" 'BEGIN { srand(); for (i=0; i<8; i++) \
	printf "%02x", int(256*rand()) }'`
    {
	cat <<EOF
To: $USER${HOST:+@$HOST}
Subject: duplex notification

You requested to print a file in duplex mode. The even pages have been
sent to the printer "$QUEUE".
Wait for all pages to be ejected, then flip the sheets and put them back
into the printer. Please insert them so that the binding is on the
$BINDING edge of the _virtual_ page.

To print the odd pages, enter the following command:

echo '$duplex_passwd' > '$APS_TMPDIR/duplex-key'

Note: This command must be entered on a single line. Use cut-and-paste
whenever possible -- switch off auto-wrapping of lines in your mail
reader if needed.


Do not reply to this message. It was generated by apsfilter. In case
of an error, please contact your system administator.
EOF
    } | "@sendmail@" -oem -t

    # now wait for the secret key (busy read from a FIFO)
    key=dummy
    while [ "$key" != "$duplex_passwd" ]; do
	read $read_r key < "$APS_TMPDIR/duplex-key"
    done
}

#=============================================================================
# print_rar
#
# Print a summary of a RAR archive.
#=============================================================================

print_rar()
{
    cat > "$APS_TMPDIR/rar"
    if find_filter unrar; then
	unrar v "$APS_TMPDIR/rar" | print_ascii
    else
	find_filter rar || fatal_filter rar
	rar v "$APS_TMPDIR/rar" | print_ascii
    fi
    rm -f "$APS_TMPDIR/rar"
}

#=============================================================================
# print_ras
#
# Print Sun RasterFiles.
#=============================================================================

print_ras()
{
    local opts

    if find_filter convert; then
	do_convert ras
    elif find_filter ras2ps; then
	[ "$COLOR" = full ] && opts="-C"
	ras2ps $opts | print_ps
    elif find_filter nconvert; then
	do_nconvert -in ras
    else
	find_filter rasttopnm || fatal_filter rasttopnm
	rasttopnm | print_pnm
    fi
}

#=============================================================================
# print_raw
#
# Print raw data.
#=============================================================================

print_raw()
{
    {
	[ "$RAW_PROLOGUE" ] && printf $RAW_PROLOGUE
	cat
	[ "$RAW_EPILOGUE" ] && printf $RAW_EPILOGUE
    } | print_data
}

#=============================================================================
# print_rle
#
# Print RLE images.
#=============================================================================

print_rle()
{
    if find_filter convert; then
	do_convert rle
    elif find_filter nconvert; then
	do_nconvert -in rle
    else
	find_filter rletopnm || fatal_filter rletopnm
	rletopnm | print_pnm
    fi
}

#=============================================================================
# print_rpm
#
# Print a summary of an RPM package.
#=============================================================================

print_rpm()
{
    local queryformat

    find_filter rpm || fatal_filter rpm

    queryformat="Name        : %-27{NAME} Distribution: %{DISTRIBUTION}
Version     : %-27{VERSION}       Vendor: %{VENDOR}
Release     : %-27{RELEASE}   Build Date: %{BUILDTIME:date}
Install date: %-27{INSTALLTIME:date}   Build Host: %{BUILDHOST}
Group       : %-27{GROUP}   Source RPM: %{SOURCERPM}
Size        : %{SIZE}
Summary     : %{SUMMARY}
Description :\n%{DESCRIPTION}

Files:
[        %{FILENAMES}\n]\

Requires:
[        %{REQUIRENAME} %{REQUIREFLAGS:depflags} %{REQUIREVERSION}\n]\

Provides:
[        %{PROVIDES}\n]"

    # this silly contruction avoids a "broken pipe" error
    { rpm --qf "$queryformat" -qp -- -; cat > /dev/null; } | print_ascii
}

#=============================================================================
# print_sgi
#
# Print SGI images.
#=============================================================================

print_sgi()
{
    if find_filter convert; then
	# convert doesn't seem to like an explicit "sgi" here
	do_convert
    elif find_filter nconvert; then
	do_nconvert -in sgi
    else
	find_filter sgitopnm || fatal_filter sgitopnm
	sgitopnm | print_pnm
    fi
}

#=============================================================================
# print_sketch
#
# Print Sketch files.
#
# Note: If your document contains inline images (bitmap, EPS, etc.), it's
#       probably better to convert it to PostScript by hand first, otherwise
#       the relative paths will cause sk2ps to barf.
#=============================================================================

print_sketch()
{
    find_filter sk2ps || fatal_filter sk2ps
    sk2ps /dev/stdin | print_ps
}

#=============================================================================
# print_tar
#
# Print a summary of a TAR archive.
#=============================================================================

print_tar()
{
    find_filter tar || fatal_filter tar
    tar tvf - | print_ascii
}

#=============================================================================
# print_tga
#
# Print Targa images.
#=============================================================================

print_tga()
{
    if find_filter convert; then
	do_convert tga
    elif find_filter nconvert; then
	do_nconvert -in tga
    else
	find_filter tgatoppm || fatal_filter tgatoppm
	tgatoppm | print_pnm
    fi
}

#=============================================================================
# print_tgif
#
# Print TGIF drawings.
#=============================================================================

print_tgif()
{
    local opts

    find_filter tgif || fatal_filter tgif
    cat > "$APS_TMPDIR/tgif.obj"
    [ "$COLOR" = full ] && opts="-color" || opts="-gray"
    tgif -print -adobe -ps -stdout $opts "$APS_TMPDIR/tgif.obj" 2> /dev/null \
	| print_ps
    rm -f "$APS_TMPDIR/tgif.obj"
}

#=============================================================================
# print_tiff
#
# Print TIFF images.
#=============================================================================

print_tiff()
{
    cat > "$APS_TMPDIR/tiff"
    if find_filter tiff2ps; then
	tiff2ps -a2 -w $WIDTH_INCHES -h $HEIGHT_INCHES "$APS_TMPDIR/tiff" | \
	    print_ps
    elif find_filter convert; then
	do_convert tiff < "$APS_TMPDIR/tiff"
    elif find_filter nconvert; then
	do_nconvert -in tiff < "$APS_TMPDIR/tiff"
    else
	find_filter tifftopnm || fatal_filter tifftopnm
	tifftopnm "$APS_TMPDIR/tiff" | print_pnm
    fi
    rm -f "$APS_TMPDIR/tiff"
}

#=============================================================================
# print_troff
#
# Print troff files.
#=============================================================================

print_troff()
{
    find_filter grog || fatal_filter grog
    find_filter groff || fatal_filter groff
    eval `grog -S -Tps - < "$HEADER"` | print_ps
}

#=============================================================================
# print_wmf
#
# Print Windows Metafiles.
#=============================================================================

print_wmf()
{
    cat > "$APS_TMPDIR/wmf"
    if find_filter wmf2eps; then
	wmf2eps --ps ${LANDSCAPE:+--landscape}${PORTRAIT:+--portrait} \
	    --centre "$APS_TMPDIR/wmf" | print_ps
    else
	find_filter sk2ps || fatal_filter sk2ps
	sk2ps "$APS_TMPDIR/wmf" | print_ps
    fi
    rm -f "$APS_TMPDIR/wmf"
}

#=============================================================================
# print_wpg
#
# Print WordPerfect graphics.
#=============================================================================

print_wpg()
{
    if find_filter convert; then
	do_convert wpg
    else
	find_filter nconvert || fatal_filter nconvert
	do_nconvert -in wpg
    fi
}

#=============================================================================
# print_xcf
#
# Print GIMP XCF files.
#=============================================================================

print_xcf()
{
    find_filter nconvert || fatal_filter nconvert
    cat > "$APS_TMPDIR/xcf"
    do_nconvert -in xcf < "$APS_TMPDIR/xcf"
    rm -f "$APS_TMPDIR/xcf"
}

#=============================================================================
# print_xpm
#
# Print X pixmaps.
#=============================================================================

print_xpm()
{
    if find_filter convert; then
	do_convert xpm
    elif find_filter nconvert; then
	do_nconvert -in xpm
    else
	find_filter xpmtoppm || fatal_filter xpmtoppm
	xpmtoppm | print_pnm
    fi
}

#=============================================================================
# print_xwd
#
# Print X window dump files.
#=============================================================================

print_xwd()
{
    if find_filter convert; then
	do_convert xwd
    elif find_filter nconvert; then
	do_nconvert -in xwd
    else
	find_filter xwdtopnm || fatal_filter xwdtopnm
	xwdtopnm | print_pnm
    fi
}

#=============================================================================
# print_zip
#
# Print a summary of a ZIP archive.
#=============================================================================

print_zip()
{
    find_filter unzip || fatal_filter unzip
    cat > "$APS_TMPDIR/zip"
    unzip -v "$APS_TMPDIR/zip" | print_ascii
    rm -f "$APS_TMPDIR/zip"
}

#=============================================================================
# print_zoo
#
# Print a summary of a Zoo archive.
#=============================================================================

print_zoo()
{
    find_filter zoo || fatal_filter zoo
    cat > "$APS_TMPDIR/zoo"
    zoo l "$APS_TMPDIR/zoo" | print_ascii
    rm -f "$APS_TMPDIR/zoo"
}

#=============================================================================
# unpack HANDLER
#
# Unpack input and pass it on to HANDLER.
#=============================================================================

unpack()
{
    local HEADER FILE_TYPE unpacker unpacker_opts

    DEPTH=$(($DEPTH + 1))
    HEADER="$APS_TMPDIR/header$DEPTH"
    dd bs=1k count=16 > "$HEADER" 2> /dev/null
    FILE_TYPE=`file "$HEADER"`
    FILE_TYPE=`echo ${FILE_TYPE#$HEADER: } | tr A-Z a-z`

    unpacker=
    case "$FILE_TYPE" in
	bzip*)
	    unpacker=bzip2 ; unpacker_opts=-d	;;
	gzip*|packed*|old\ packed*)
	    unpacker=gzip  ; unpacker_opts=-d	;;
	compress*)
	    unpacker=zcat  ; unpacker_opts=	;;
	frozen*)
	    unpacker=fcat  ; unpacker_opts=	;;
	lzop*)
	    unpacker=lzop  ; unpacker_opts=-d	;;
	empty)
	    rm -f "$HEADER"; return ;;
    esac
    if [ "$unpacker" ]; then
	find_filter $unpacker || fatal_unpacker $unpacker
	cat "$HEADER" - | $unpacker $unpacker_opts | unpack "$@"
    else
	cat "$HEADER" - | eval "$@"
    fi

    rm -f "$HEADER"
}

##############################################################################
# Main Body
##############################################################################

#-----------------------------------------------------------------------------
# Process command line arguments.
#
# Recognized options:
#     -c
#     -h HOST
#     -H HOST (LPRng)
#     -n USER
#     -L USER (LPRng)
#     -C CLASS
#     -Z Z_OPTS (LPRng)
#     -f JOB (LPRng)
#
# Options and their arguments can also be concatenated (e.g. `-nroot').
#
# Unrecognized options are ignored.
#-----------------------------------------------------------------------------

while [ $# -gt 0 ]; do
    variable=
    case "$1" in
	-c)      LPD_METHOD=raw    ;;
	-h*|-H*) variable=HOST     ;;
	-n*|-L*) variable=USER     ;;
	-C*)     variable=CLASS    ;;
	-Z*)     variable=Z_OPTS   ;;
	-f*)     variable=JOB      ;;
	-*)      variable=variable ;;
	*)       ACCT_FILE="$1"    ;;
    esac
    if [ "$variable" ]; then
	value="${1#-?}"
	if [ ! "$value" ]; then
	    shift
	    value="$1"
	fi
	eval $variable='"$value"'
    fi
    shift
done
unset variable value

#-----------------------------------------------------------------------------
# Directories.
#
# CONF_DIR: The global apsfilter configuration directory.
#
# HOMEDIR: User's home directory (extracted from /etc/passwd).
#
# SPOOLDIR: The lpd spool directory.
#
# TMPDIR: Directory used for storing temporary files.
#
# APS_TMPDIR: Directory used for storing temporary files securely.
#-----------------------------------------------------------------------------

# check if we must use the "-r" flag for "read" operations
if echo dummy | read -r dummy 2>/dev/null; then
    read_r="-r"
else
    unset read_r
fi

CONF_DIR=@sysconfdir@/apsfilter

[ -d "$CONF_DIR/basedir" ] || fatal_basedir

HOMEDIR="`eval echo ~$USER`"
case "$HOMEDIR" in
    [!/]*)
	# the shell doesn't know tilde expansion
	HOMEDIR=`
	    if find_filter ypcat; then
		# we're on a NIS (aka yp) client
		ypcat passwd | grep "^$USER:"
	    else
		grep "^$USER:" /etc/passwd
	    fi | \
	    if [ $? -eq 0 ]; then
		# grep found something
		IFS=: read $read_r user passwd uid gid gecos home shell
		echo "$home"
	    fi
	`
	;;
esac

SPOOLDIR="${SPOOL_DIR:-${ACCT_FILE%/*}}"
[ "$SPOOLDIR" ] || fatal_spooldir

# get the printer queue name from the spool dir name
QUEUE=`basename $SPOOLDIR`

#-----------------------------------------------------------------------------
# Process configuration files.
#-----------------------------------------------------------------------------

[ -f "$CONF_DIR/apsfilterrc" ] && . "$CONF_DIR/apsfilterrc"

[ -f "$CONF_DIR/$QUEUE/apsfilterrc" ] || fatal_config
# this file does the basic configuration for each printer (driver name,
# paper size, method, resolution), so it must be present
. "$CONF_DIR/$QUEUE/apsfilterrc"

[ -f "$CONF_DIR/$QUEUE/apsfilterrc.$USER" ] && \
    . "$CONF_DIR/$QUEUE/apsfilterrc.$USER"

# aps2file runs in userland, so we can safely use user-specific configuration
[ "$APS2FILE_CONTEXT" ] && USE_USER_CODE=set

[ -n "$USE_USER_CODE" -a -f "$HOMEDIR/.apsfilter/apsfilterrc.$QUEUE" ] && \
    . "$HOMEDIR/.apsfilter/apsfilterrc.$QUEUE"

export PATH

# our private temporary directory has mode 711 so that the user can see our
# fifo "$APS_TMPDIR/duplex-key" (in case it's needed)
: ${TMPDIR:=/tmp}
APS_TMPDIR="$TMPDIR/apsfilter$$"
rm -rf "$APS_TMPDIR"
mkdir -m 711 "$APS_TMPDIR"
if [ $? -ne 0 ]; then
    # maybe someone tried a denial-of-service attack
    find_filter mktemp || fatal_tmpdir
    APS_TMPDIR=`mktemp -q -d "$TMPDIR/apsfilter.XXXXXX"` || fatal_tmpdir
    chmod 711 "$APS_TMPDIR"
fi
echo 0 >"$APS_TMPDIR/rc"

# gs doesn't always remove its temp files; let it use our private
# temp directory, so that everything will be removed later on
export TEMP="$APS_TMPDIR" TMPDIR="$APS_TMPDIR"

# default value for AWK (you can change it in apsfilterrc)
: ${AWK:=@awk@}

# various quality setting defaults
: ${QUALITY:=medium} ${SWEEP:=bi} ${MEDIA:=plain} ${COLOR:=full}

# if raw printing was requested on the lpr command line ("-l" or "-b" switch),
# it has higher priority than the value in apsfilterrc, but can be set to be
# ignored anyway (to please buggy remote spoolers)
[ -n "$LPD_METHOD" -a -z "$IGNORE_LPD_RAW" ] && METHOD=raw

#-----------------------------------------------------------------------------
# Process the control file.
#
# We don't do this when LPRng is used since it gives us all the information we
# need on the command line.
#-----------------------------------------------------------------------------

# LPRng sets $CONTROL, and BSD lpr doesn't.
if [ ! "$CONTROL" ]; then
    {
	read pid
	read $read_r control_file
    } < "$SPOOLDIR/lock"
    while read $read_r line; do
	value="${line#?}"
	case "$line" in
	    C*) CLASS="$value" ;;
	    H*) HOST="$value"  ;;
	    J*) JOB="$value"   ;;
	    N*) FNAME="$value" ;;
	    P*) USER="$value"  ;;
	esac
    done < "$SPOOLDIR/$control_file"
    : ${JOB:=$FNAME}

    unset pid control_file line value
fi

#-----------------------------------------------------------------------------
# Don't show a job title if it's coming from standard input.
#
# BSD lpr uses `stdin' for this case, and LPRng uses `(stdin)' or `(STDIN)'.
#-----------------------------------------------------------------------------

[ "$JOB" = stdin -o "$JOB" = '(stdin)' -o "$JOB" = '(STDIN)' ] && unset JOB

#-----------------------------------------------------------------------------
# Parse the command line options (via -C, -Z and -o).
#-----------------------------------------------------------------------------

if [ "$CLASS$Z_OPTS" ]; then
  old_ifs="$IFS"
  IFS=:,
  set -- $CLASS $Z_OPTS
  IFS="$old_ifs"
  unset old_ifs

  for option; do
    case "$option" in
      lo|low)				# printing quality
        QUALITY=low ;;
      hi|high)
	QUALITY=high ;;
      med|medium)
	QUALITY=medium ;;
      draft|photo)
        QUALITY=$option ;;

      uni|bi)				# uni/bi-directional printing
	SWEEP=$option ;;

      plain|coated|glossy|premium|trans)
	MEDIA=$option ;;		# paper type

      color|colour)			# color printing
	COLOR=full ;;
      gray|grey)
	COLOR=gray ;;
      mono)
	COLOR=mono ;;

      a3|a4|legal|letter|ledger|tabloid)
	PAPERSIZE=$option ;;		# paper size
      ascii|auto|raw)			# force printing method
	METHOD=$option ;;
      a2ps|mpage|enscript|recode)	# choose filter for ASCII files
	ASCII_FILTER=$option ;;
      1pps|2pps|4pps|8pps)		# pages per sheet
	PS_NUP=${option%pps}; ASCII_PPS=$PS_NUP ;;
      header|noheader)			# use ASCII headers?
	ASCII_HEADER=${option#noheader} ;;
      border|noborder)			# use ASCII borders?
	ASCII_BORDER=${option#noborder} ;;
      pretty=[012])			# pretty-printing mode
	PRETTY_PRINTING=${option#pretty=} ;;
      duplex|simplex)			# use duplex printing?
        DUPLEX=${option#simplex} ;;
      shortbind|longbind)		# binding edge for hardware duplex
	BINDING=${option%bind} ;;
      tray[0-9])			# paper tray selection
	PAPERTRAY=${option#tray} ;;
      book)				# "book" printing mode
	PS_BOOK=set PS_NUP=2 ASCII_PPS=2 DUPLEX=set BINDING=short ;;
      land*|port*)			# force paper orientation
	LANDSCAPE=${option##port*}; ASCII_LANDSCAPE=$LANDSCAPE ;;
      copies=*)				# no of copies
        COPIES=${option#copies=} ;;

      [A-Z])				# class specification
	;;
      *)				# unknown option
        echo >&2 "apsfilter warning: unknown option '$option'" ;;
    esac
  done
  unset option
fi

#-----------------------------------------------------------------------------
# Fix the options.
#-----------------------------------------------------------------------------

: ${MAXCOPIES:=10}
[ "${COPIES##*[^0-9]*}" ] || COPIES=1  # now $COPIES is an integer number
[ "$COPIES" -gt "$MAXCOPIES" ] && COPIES="$MAXCOPIES"
[ "$COPIES" = 0 ] && exit  # printing 0 copies is _really_ fast :)

[ "$LANDSCAPE" ] && PORTRAIT= || LANDSCAPE= PORTRAIT=set
[ "$BINDING" = short ] || BINDING=long
[ "$DISABLE_DUPLEX" ] && unset DUPLEX
: ${ASCII_LANDSCAPE=$LANDSCAPE} ${ASCII_PPS=$PS_NUP} ${BLANK_PAGE:='\012\014'}
: ${PRETTY_PRINTING:=1}

case "$PS_NUP" in
    2|4|8)	;;
    *)		unset PS_NUP ;;
esac
PSNUP_ROTATE="-l"

# if the admin decides to restrict certain options for the printer(s),
# it will be done in the following scripts
[ -e "$CONF_DIR/restrictions" ] && . "$CONF_DIR/restrictions"
[ -e "$CONF_DIR/$QUEUE/restrictions" ] && . "$CONF_DIR/$QUEUE/restrictions"


case "$PAPERSIZE" in
    a4)		WIDTH_POINTS=595;   HEIGHT_POINTS=842
		WIDTH_INCHES=8.26;  HEIGHT_INCHES=11.69 ;;
    a3)		WIDTH_POINTS=842;   HEIGHT_POINTS=1190
		WIDTH_INCHES=11.69; HEIGHT_INCHES=16.53 ;;
    letter)	WIDTH_POINTS=612;   HEIGHT_POINTS=792
		WIDTH_INCHES=8.5;   HEIGHT_INCHES=11    ;;
    legal)	WIDTH_POINTS=612;   HEIGHT_POINTS=1008
		WIDTH_INCHES=8.5;   HEIGHT_INCHES=14    ;;
    ledger)	WIDTH_POINTS=1224;  HEIGHT_POINTS=792
		WIDTH_INCHES=17;    HEIGHT_INCHES=11    ;;
    tabloid)	WIDTH_POINTS=792;   HEIGHT_POINTS=1224
		WIDTH_INCHES=11;    HEIGHT_INCHES=17    ;;
    *)		# use safe default values (small enough)
		WIDTH_POINTS=595;   HEIGHT_POINTS=792
		WIDTH_INCHES=8.26;  HEIGHT_INCHES=11    ;;
esac


# if we print to a NetWare or remote lpd printer, rather let them do copies
# (for lower network impact); otherwise we will do it locally
if [ "${REMOTE_COPIES=set}" ]; then
    if [ -f "$CONF_DIR/$QUEUE/netware.conf" -o \
	 -f "$CONF_DIR/$QUEUE/lpr.conf" ]; then
	unset HARDWARE_COPIES
    else
	unset REMOTE_COPIES
    fi
fi

# now we call the driver script which assigns proper values to RESOLUTION
# and GS_FEATURES, hopefully :)

case "$PRINTER" in
    *.upp)
	# uniprint profile -- everything is hard-coded
	;;

    *)
	DRIVER_SCRIPT="$PRINTER"
	if [ ! -e "$CONF_DIR/basedir/driver/$DRIVER_SCRIPT" ]; then
	    # get the script name from the mapping file
	    DRIVER_SCRIPT=$(grep ":$PRINTER:" \
		"$CONF_DIR/basedir/driver/MAPPING" | sed "s/:.*//")
	fi
	if [ -z "$DRIVER_SCRIPT" ]; then
	    echo >&2 "apsfilter: driver script '$PRINTER' not yet available"
	elif [ -e "$CONF_DIR/basedir/driver/$DRIVER_SCRIPT" ]; then
	    . "$CONF_DIR/basedir/driver/$DRIVER_SCRIPT"
	else
	    fatal_driver_script
	fi

	# correct some printer/model settings
	case "$PRINTER" in
	    omni/*|hpijs/*)
		GS_FEATURES="$GS_FEATURES -sDeviceName=${PRINTER#*/}"
		PRINTER=${PRINTER%%/*} ;;
	    stp/*|hpdj/*)
		GS_FEATURES="$GS_FEATURES -sModel=${PRINTER#*/}"
		PRINTER=${PRINTER%%/*} ;;
	    pcl3/*)
		GS_FEATURES="$GS_FEATURES -sSubdevice=${PRINTER#pcl3/}"
		PRINTER=pcl3 ;;
	    gimp/*)
		GS_FEATURES="$GS_FEATURES -sDeviceModel='${PRINTER#gimp/}' \
		    -sIjsServer=ijsgimpprint -dIjsUseOutputFD"
		PRINTER=ijs ;;
	    ijs/*)
		GS_FEATURES="$GS_FEATURES -sDeviceModel='${PRINTER#ijs/}' \
		    -sIjsServer=hpijs -dIjsUseOutputFD"
		PRINTER=ijs ;;
	esac
	;;
esac

# if the driver does copying/duplex by itself, we won't do it anymore
# (hopefully the correct options have been added in the printer script)
if [ "$PRINTER" != PS ]; then
    [ "$HARDWARE_COPIES" ] && COPIES=1
    [ "$HARDWARE_DUPLEX" ] && unset DUPLEX
fi

#-----------------------------------------------------------------------------
# Call the appropriate printing function.
#-----------------------------------------------------------------------------

case "$METHOD" in
    auto)  unpack print_auto	;;
    ascii) unpack print_ascii	;;
    raw)   print_raw		;;
    *)     fatal_method		;;
esac