This file is indexed.

/usr/bin/foo2zjs-wrapper is in printer-driver-foo2zjs-common 20170320dfsg0-4.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/bin/sh

#* Copyright (C) 2003-2006  Rick Richardson
#*
#* This program is free software; you can redistribute it and/or modify
#* it under the terms of the GNU General Public License as published by
#* the Free Software Foundation; either version 2 of the License, or
#* (at your option) any later version.
#*
#* This program is distributed in the hope that it will be useful,
#* but WITHOUT ANY WARRANTY; without even the implied warranty of
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#* GNU General Public License for more details.
#*
#* You should have received a copy of the GNU General Public License
#* along with this program; if not, write to the Free Software
#* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#*
#* Authors: Rick Richardson <rick.richardson@comcast.net>

VERSION='$Id: foo2zjs-wrapper.in,v 1.154 2012/02/24 01:25:15 rick Exp $'

#
# Printer Notes:
#
# Minolta/QMS 2300 DL	- Does not need clipping, -X16, or -P.  But it doesn't
#			  break anything if you use them.
#
# Minolta/QMS 2200 DL	- Requires -X16 (which is on by default)
#			- Requires clipping
#
# HP LaserJet 1000	- Same as 2200 DL, plus needs -P
# HP LaserJet 1005	- Same as 2200 DL, plus needs -P
#
# HP LaserJet 1018	- Same as 2200 DL, plus needs -P -z1 -L0
# HP LaserJet 1020	- Same as 2200 DL, plus needs -P -z1 -L0
# HP LaserJet 1022	- Same as 2200 DL, plus needs -P -z1 -L0
# HP LaserJet M1319 MFP	- Same as 2200 DL, plus needs -P -z1 -L0
# HP LaserJet P2035	- Same as 2200 DL, plus needs -P -z1 -L0
# HP LaserJet P2035n	- Same as 2200 DL, plus needs -P -z1 -L0
#
# HP LaserJet Pro P1102	- Same as 2200 DL, plus needs -P -z2 -L0
# HP LaserJet Pro P1102w- Same as 2200 DL, plus needs -P -z2 -L0
# HP LaserJet Pro P1566	- Same as 2200 DL, plus needs -P -z2 -L0
# HP LaserJet Pro P1606dn- Same as 2200 DL, plus needs -P -z2 -L0
#
# HP LaserJet Pro CP1025nw- Same as 2200 DL, plus needs -P -z3 -L0
#

PROGNAME="$0"
BASENAME=`basename $PROGNAME`
PREFIX=/usr
SHARE=$PREFIX/share/foo2zjs
PATH=$PATH:/sw/bin:/opt/local/bin

#
#	Log the command line, for debugging and problem reports
#
if [ -x /usr/bin/logger -o -x /bin/logger ]; then
    logger -t "$BASENAME" -p lpr.info -- "$BASENAME $@" </dev/null
fi

usage() {
	cat <<EOF
Usage:
	$BASENAME [options] [ps-file]

	Foomatic printer wrapper for the foo2zjs printer driver.
	This script reads a Postscript ps-file or standard input
	and converts it to Zenographics ZjStream printer format.

Normal Options:
-c                Print in color (else monochrome)
-C colormode      Colormode [$COLORMODE]
                    1=Photos, 2=Photo and text, 3=Graphics and text
                    10=ICM RGB color profile (using -G *.icm file)
                    gs9=New gs9+ ICM CMYK color profile (using -G *.icm file)
-d duplex         Duplex code to send to printer [$DUPLEX]
                    1=off, 2=longedge, 3=shortedge
-m media          Media code to send to printer [$MEDIA]
                  -z0:
                    1=standard, 2=transparency, 3=glossy, 257=envelope,
                    259=letterhead, 261=thickstock, 262=postcard, 263=labels
                  -z1,-z2,-z3: above plus
		    258=light, 261=cardstock, 263=rough, 265=labels,
                    267=envelope, 273=vellum, 512=color, 513=letterhead,
                    514=preprinted, 515=prepunched, 516=recycled
-p paper          Paper code [$PAPER]
                  -z0:
                    1=letter, 5=legal, 7=executive, 9=A4, 11=A5, 13=B5jis
                    20=env#10, 27=envDL 28=envC5 34=envB5 37=envMonarch
                  -z1: -z0 plus
		    257=16K 197x273, 258=fanfold german legal, 259=b5iso,
                    260=postcard, 261=doublepostcard, 262=A6
                  -z2: -z0 plus
		    43=postcard, 70=A6, 82=double postcard rotated,
		    257=16K 197x273, 263=16K 184x260, 264=16K 195x270,
                    258=fanfold german legal
                  -z3: -z0 plus
		    43=postcard, 70=A6, 82=double postcard rotated,
		    257=16K 197x273, 263=16K 184x260, 264=16K 195x270,
                    258=fanfold german legal, 268=photo4x6, 269=photo5x8,
		    270=photo10x15
-n copies         Number of copies [$COPIES]
-r <xres>x<yres>  Set device resolution in pixels/inch [$RES]
-s source         Source code to send to printer [$SOURCE]
                    1=upper, 2=lower, 4=manual, 7=auto
		    Code numbers may vary with printer model.
-t                Draft mode.  Every other pixel is white.
-T density        Print density (1-5) [$DENSITY]
-2/-3/-4/-6/-8/-10/-12/-14/-15/-16/-18
                  Print with N-up (requires psutils)
-o orient         For N-up: -op is portrait, -ol is landscape, -os is seascape.

Printer Tweaking Options:
-u <xoff>x<yoff>  Set offset of upper left printable in pixels [varies]
-l <xoff>x<yoff>  Set offset of lower right printable in pixels [varies]
-L mask           Send logical clipping values from -u/-l in ZjStream [3]
                  0=no, 1=Y, 2=X, 3=XY
-P                Do not output START_PLANE codes.  May be needed by some
                  monochrome-only printers.
-X padlen         Add extra zero padding to the end of BID segments [16]
-z model          Model: [0]
                    0=KM 2300DL / HP 1000 / HP 1005
                    1=HP 1018 / HP 1020 / HP 1022
                    2=HP Pro P1102 / P1566 / P1606dn
                    3=HP Pro CP1025nw

Color Tweaking Options:
-g gsopts         Additional options to pass to Ghostscript, such as
                  -dDITHERPPI=nnn, etc.  May appear more than once. []
-G profile.icm    Convert profile.icm to a Postscript CRD using icc2ps and
                  adjust colors using the setcolorrendering PS operator.
                  $SHARE/icm/ will be searched for profile.icm.
-I intent         Select profile intent from ICM file [$INTENT]
                  0=Perceptual, 1=Colorimetric, 2=Saturation, 3=Absolute
-G gamma-file.ps  Prepend gamma-file to the Postscript input to perform
                  color correction using the setcolortransfer PS operator.

Debugging Options:
-S plane          Output just a single color plane from a color print [all]
                  1=Cyan, 2=Magenta, 3=Yellow, 4=Black
-D lvl            Set Debug level [$DEBUG]
-V                $VERSION
EOF

	exit 1
}

#
#       Report an error and exit
#
error() {
	echo "$BASENAME: $1" >&2
	exit 1
}

dbgcmd() {
	if [ $DEBUG -ge 1 ]; then
	    echo "$@" >&2
	fi
	"$@"
}

#
#	N-up-ify the job.  Requires psnup from psutils package
#
nup() {
    case "$NUP" in
    [2368]|1[0458])
	tr '\r' '\n' | psnup $NUP_ORIENT -d2 -$NUP -m.3in -p$paper -q
	;;
    [49]|1[26])
	tr '\r' '\n' | psnup $NUP_ORIENT -d2 -$NUP -m.5in -p$paper -q
	;;
    *)
	error "Illegal call to nup()."
	;;
    esac
}

#
#       Process the options
#

# Try to use a local copy of GhostScript 8.54, if available.  Otherwise,
# fallback to whatever the Linux distro has installed (usually 7.07)
#
# N.B. := operator used here, when :- would be better, because "ash"
# doesn't have :-
if eval gs.foo -v >/dev/null 2>&1; then
        GSBIN=${GSBIN:-gs.foo}
else
        GSBIN=${GSBIN:-gs}
fi
GSVER=`$GSBIN --version`

CMDLINE="$*"
DEBUG=0
DUPLEX=1
COLOR=
COLORMODE=default
MODEL=0
QUALITY=1
QUALITY=wts
MEDIA=1
COPIES=1
test -r /etc/papersize && PAPER=$(cat /etc/papersize)
test "$PAPER" || PAPER=1
RES=1200x600
SOURCE=7
NUP=
CLIP_UL=
CLIP_LR=
CLIP_LOG=
BC=
AIB=
NOPLANES=
COLOR2MONO=
GAMMAFILE=
INTENT=0
GSOPTS=
EXTRAPAD=
SAVETONER=
DENSITY=3       #Medium
NUP_ORIENT=
GSDEV=-sDEVICE=pbmraw
# What mode to use if the user wants us to pick the "best" mode
case "$GSVER" in
7*)	DEFAULTCOLORMODE=10
	DEFAULTCOLORMODE=2
	;;
8.1*)
	DEFAULTCOLORMODE=2
	QUALITY=1
	;;
*)	DEFAULTCOLORMODE=2
	;;
esac
while getopts "1:23456789o:b:cC:d:g:l:u:L:m:n:p:q:r:s:tT:z:ABS:D:G:I:PX:Vh?" opt
do
	case $opt in
	b)	GSBIN="$OPTARG";;
	c)	COLOR=-c;;
	d)	DUPLEX="$OPTARG";;
	g)	GSOPTS="$GSOPTS $OPTARG";;
	m)	MEDIA="$OPTARG";;
	n)	COPIES="$OPTARG";;
	p)	PAPER="$OPTARG";;
	q)	QUALITY="$OPTARG";;
	r)	RES="$OPTARG";;
	s)	SOURCE="$OPTARG";;
	t)	SAVETONER="-t";;
	T)      DENSITY="$OPTARG";;
	z)	MODEL="$OPTARG";;
	l)	CLIP_LR="-l $OPTARG";;
	u)	CLIP_UL="-u $OPTARG";;
	L)	CLIP_LOG="-L $OPTARG";;
	A)	AIB=-A;;
	B)	BC=-B;;
	C)	COLORMODE="$OPTARG";;
	S)	COLOR2MONO="-S$OPTARG";;
	D)	DEBUG="$OPTARG";;
	G)	GAMMAFILE="$OPTARG";;
	I)	INTENT="$OPTARG";;
	P)	NOPLANES=-P;;
	X)	EXTRAPAD="-X $OPTARG";;
	[234689])	NUP="$opt";;
	[57])	error "Can't find acceptable layout for $opt-up";;
	1)	case "$OPTARG" in
		[024568])	NUP="1$OPTARG";;
		*)	error "Can't find acceptable layout for 1$OPTARG-up";;
		esac
		;;
	o)	case "$OPTARG" in
		l*)	NUP_ORIENT=-l;;
		s*)	NUP_ORIENT=-r;;
		p*|*)	NUP_ORIENT=;;
		esac;;
	V)	echo "$VERSION"; foo2zjs -V; foo2zjs-pstops -V; exit 0;;
	h|\?)
		if [ "$CMDLINE" != "-?" -a "$CMDLINE" != -h ]; then
		    echo "Illegal command:"
		    echo "	$0 $CMDLINE"
		    echo
		fi
		usage;;
	esac
done
shift `expr $OPTIND - 1`

# Testing for gs9...
if [ `uname -n` = dual.rkkda.org -a $MODEL = 3 ]; then
    GSBIN=gs904
    GSBIN=gsgit	# gs 9.05+
fi

#
# If there is an argument left, take it as the file to print.
# Else, the input comes from stdin.
#
if [ $# -ge 1 ]; then
    if [ "$LPJOB" = "" ]; then
	: # LPJOB="$1"
    fi
    exec < $1
fi

#
case "$QUALITY" in
0)
    GSOPTS="-dCOLORSCREEN $GSOPTS"
    ;;
1)
    GSOPTS="-dCOLORSCREEN $GSOPTS"
    ;;
2)
    GSOPTS="-dMaxBitmap=500000000 $GSOPTS"
    ;;
as)
    GSOPTS="-dCOLORSCREEN -dMaxBitmap=500000000 $GSOPTS"
    ;;
wts)
    GSOPTS="-dCOLORSCREEN -dMaxBitmap=500000000 $GSOPTS"
    ;;
esac

#
#	Validate model code
#
case "$MODEL" in
0|1|2|3)	;;
*)	error "Unknown model code $MODEL";;
esac

#
#	Validate media code
#
case "$MEDIA" in
1|standard|plain)		MEDIA=1;;
2|transparency)			MEDIA=2;;
3|glossy)			MEDIA=3;;
257|envelope)			MEDIA=257;;	# -z0
258|light)			MEDIA=258;;	# -z1, -z2
259|letterhead)			MEDIA=259;;	# -z0
261|thickstock|cardstock)	MEDIA=261;;
262|postcard|heavy)		MEDIA=262;;
263|labels|rough)		MEDIA=263;;	# -z0 labels, -z[12] rough
265|labels)			MEDIA=265;;	# -z1, -z2
267|envelope)			MEDIA=267;;	# -z1, -z2
273|vellum)			MEDIA=267;;	# -z1, -z2
512|color)			MEDIA=512;;
513|letterhead)			MEDIA=513;;	# -z1, -z2
514|preprinted)			MEDIA=514;;	# -z1, -z2
515|prepunched)			MEDIA=515;;	# -z1, -z2
516|recycled)			MEDIA=516;;	# -z1, -z2
[0-9]*)				;;
*)				error "Unknown media code $MEDIA";;
esac

#
#	Validate source (InputSlot) code
#
case "$SOURCE" in
1|upper)	SOURCE=1;;
2|lower)	SOURCE=2;;
4|manual)	SOURCE=4;;
7|auto)		SOURCE=7;;
[0-9]*)		;;
*)		error "Unknown source code $SOURCE";;
esac

#
#	Validate Duplex code
#
case "$DUPLEX" in
1|off|none)	DUPLEX=1;;
2|long*)	DUPLEX=2;;
3|short*)	DUPLEX=3;;
[0-9]*)		;;
*)		error "Unknown duplex code $DUPLEX";;
esac

#
#	Validate Resolution
#
case "$RES" in
600x600)	;;
1200x600)	;;
2400x600)	;;
*)		error "Illegal resolution $RES";;
esac

#
#	Validate Print Density
#
case "$DENSITY" in
1|2|3|4|5)	;;
*)		error "Illegal print density (-T) $DENSITY";;
esac

#
#	Figure out the paper dimensions in pixels/inch, and set the
#	default clipping region.  Unfortunately, this is a trouble
#	area for ZjStream printers.  Various versions of ZjS print
#	engines react differently when asked to print into their
#	unprintable regions.
#
#	The Minolta 2200 DL is sensitive to its unprintable regions,
#	and will pixel skew if you try to print there.
#
#	The HP1000 will print blank pages when asked to print into its
#	unprintable region.
#
#	The Minolta 2300 DL doesn't care if print into the unprintable
#	region.  It will do the clipping itself.  This is as it should be.
#	But it won't hurt it if we do the clipping here.
#
set_clipping() {
    ulx=$1; uly=$2
    lrx=$3; lry=$4

    # Set clipping region if it isn't already set
    if [ "$CLIP_UL" = "" ]; then
	case "$RES" in
	600x600)	ulx=`expr $ulx / 2`;;
	2400x600)	ulx=`expr $ulx \* 2`;;
	esac
	CLIP_UL="-u ${ulx}x${uly}"
    fi
    if [ "$CLIP_LR" = "" ]; then
	case "$RES" in
	600x600)	lrx=`expr $lrx / 2`;;
	2400x600)	lrx=`expr $lrx \* 2`;;
	esac
	CLIP_LR="-l ${lrx}x${lry}"
    fi
}

case "$PAPER" in
Custom*x*)
		# Command line only
		XDIM=`echo "$PAPER" | sed -e "s/Custom.//" -e "s/x.*//" `
		YDIM=`echo "$PAPER" | sed -e "s/.*x//" `
		if [ "$XDIM" = "" -o "$YDIM" = "" ]; then
		    error "Custom page size '$PAPER' != 1-99999"
		fi
		if [ "$XDIM" -eq 0 -o "$YDIM" -eq 0 ]; then
		    error "Custom page size '$PAPER' != 1-99999"
		fi
		XDIM=`dc -e "$XDIM 1200* 72/p"`
		YDIM=`dc -e "$YDIM 600* 72/p"`
		PAPER=256;        paper=letter;
		MEDIA=261
                set_clipping 2 100     2 100
		;;
Custom*)
		#%%BeginFeature: *CustomPageSize True
		#216
		#360
		#0
		#0
		#0
		#pop pop pop pop pop

		#%%BeginFeature: *CustomPageSize True
		#792.000000 612.000000 1 0.000000 0.000000
		#pop pop pop pop pop

		if [ $DEBUG = 0 ]; then
		    TMPFILE=/tmp/cus$$
		else
		    TMPFILE=/tmp/custom.ps
		fi
		cat >$TMPFILE
		exec <$TMPFILE

		tmp=`head -n 10000 $TMPFILE \
		    | sed -n '/CustomPageSize/{n;p;n;p;}' \
		    | tr '\n' ' '`
		case "$tmp" in
		[0-9]*\ [0-9]*)
		    XDIM=`echo "$tmp" | sed 's/ .*//'`
		    YDIM=`echo "$tmp" | sed -e 's/^[^ ]* //' -e 's/ .*//'`
		    ;;
		*)
		    if [ $DEBUG = 0 ]; then rm -f $TMPFILE; fi
		    error "Custom page size [XY]DIM != 1-99999"
		    ;;
		esac
		XDIM=`dc -e "$XDIM 1200* 72/p"`
		YDIM=`dc -e "$YDIM 600* 72/p"`
		PAPER=256;        paper=letter;
		MEDIA=261
                set_clipping 2 100     2 100
		;;
1|letter)	PAPER=1;	paper=letter;    XDIM="10200"; YDIM="6600"
		case "$MODEL" in
		0)	set_clipping 204 102	204 106;;
		1)	set_clipping 192 96	192 96;;
		2)	set_clipping 192 96	192 96;;
		3)	set_clipping 192 96	192 96;;
		# 1)	set_clipping 96 96	288 96;;
		esac
		;;
5|legal)	PAPER=5;	paper=legal;     XDIM="10200"; YDIM="8400"
		case "$MODEL" in
		0)	set_clipping 204 102	204 106;;
		1)	set_clipping 192 96	192 96;;
		2)	set_clipping 192 96	192 96;;
		3)	set_clipping 192 96	192 96;;
		esac
		;;
7|executive)	PAPER=7;	paper=executive; XDIM="8700";  YDIM="6300"
		case "$MODEL" in
		0)	set_clipping 206 102	206 102;;
		1)	set_clipping 192 96	192 96;;
		2)	set_clipping 192 96	192 96;;
		3)	set_clipping 192 96	192 96;;
		esac
		;;
9|a4|A4)	PAPER=9;	paper=a4;        XDIM="9920";  YDIM="7016"
		case "$MODEL" in
		0)
		    if [ "$NOPLANES" != "" -a "$RES" = 600x600 ]; then
			# Hack fix for LJ1000
			set_clipping 224 100	224 100
		    else
			# 2200DL
			set_clipping 200 100	200 100
		    fi
		    ;;
		1)	set_clipping 192 96	192 96;;
		2)	set_clipping 192 96	192 96;;
		3)	set_clipping 192 96	192 96;;
		esac
		;;
11|a5|A5)	PAPER=11;	paper=a5;        XDIM="6992";  YDIM="4960"
		case "$MODEL" in
		0)	set_clipping 200 100	200 100;;
		1)	set_clipping 192 96	192 96;;
		2)	set_clipping 192 96	192 96;;
		3)	set_clipping 192 96	192 96;;
		esac
		;;
13|b5|B5)	PAPER=13;	paper=b5;        XDIM="8598";  YDIM="6070"
		case "$MODEL" in
		0)	set_clipping 207 107	207 107;;
		1)	set_clipping 192 96	192 96;;
		2)	set_clipping 192 96	192 96;;
		3)	set_clipping 192 96	192 96;;
		esac
		;;
259|b5iso|B5ISO) PAPER=259;	paper=b5;        XDIM="8316";  YDIM="5904"
		case "$MODEL" in
		1)	set_clipping 192 96	192 96;;
		*)	error "Unimplemented paper code $PAPER";;
		esac
		;;
20|"env#10")	PAPER=20;	paper=env10;     XDIM="4950";  YDIM="5700"
		case "$MODEL" in
		0)	set_clipping 171 78	171 78;;
		1)	set_clipping 171 78	171 78;;
		2)	set_clipping 171 78	171 78;;
		3)	set_clipping 171 78	171 78;;
		esac
		;;
27|envDL)	PAPER=27;	paper=envDL;     XDIM="5200";  YDIM="5200"
		case "$MODEL" in
		0)	set_clipping 176 84	176 84;;
		1)	set_clipping 176 84	176 84;;
		2)	set_clipping 176 84	176 84;;
		3)	set_clipping 176 84	176 84;;
		esac
		;;
28|envC5)	PAPER=28;	paper=envC5;     XDIM="7650";  YDIM="5408"
		case "$MODEL" in
		0)	set_clipping 170 80	169 80;;
		1)	set_clipping 170 80	169 80;;
		2)	set_clipping 170 80	169 80;;
		3)	set_clipping 170 80	169 80;;
		esac
		;;
34|envB5)	PAPER=34;	paper=envB5;     XDIM="8316";  YDIM="5892"
		case "$MODEL" in
		0)	set_clipping 174 74	174 74;;
		1)	set_clipping 174 74	174 74;;
		2)	set_clipping 174 74	174 74;;
		3)	set_clipping 174 74	174 74;;
		esac
		;;
37|envMonarch)	PAPER=37;	paper=envMonarch;XDIM="4650";  YDIM="4500"
		case "$MODEL" in
		0)	set_clipping 174 78	173 78;;
		1)	set_clipping 174 78	173 78;;
		2)	set_clipping 174 78	173 78;;
		3)	set_clipping 174 78	173 78;;
		esac
		;;
70|262|A6)			paper=A6;	XDIM="4956";  YDIM="3498"
		case "$MODEL" in
		1)	PAPER=262; set_clipping 174 78	173 78;;
		2)	PAPER=70; set_clipping 174 78	173 78;;
		3)	PAPER=70; set_clipping 174 78	173 78;;
		*)	error "Unimplemented paper code $PAPER";;
		esac
		;;
257|16k197x273) PAPER=257;      paper=executive;XDIM="9306";  YDIM="6448"
		case "$MODEL" in
		1)	set_clipping 173 84	173 84;;
		2)	set_clipping 173 84	173 84;;
		3)	set_clipping 173 84	173 84;;
		*)	error "Unimplemented paper code $PAPER";;
		esac
                ;;
263|16k184x260) PAPER=263;      paper=executive;XDIM="8692";  YDIM="6142"
		case "$MODEL" in
		2)	set_clipping 122 87	122 87;;
		3)	set_clipping 122 87	122 87;;
		*)	error "Unimplemented paper code $PAPER";;
		esac
                ;;
264|16k195x270) PAPER=264;      paper=executive;XDIM="9212";  YDIM="6378"
		case "$MODEL" in
		2)	set_clipping 126 87	126 87;;
		3)	set_clipping 126 87	126 87;;
		*)	error "Unimplemented paper code $PAPER";;
		esac
                ;;
43|260|postcard)		paper=executive;XDIM="4728";  YDIM="3498"
		case "$MODEL" in
		1)	PAPER=260; set_clipping 126 87	126 87;;
		2)	PAPER=43; set_clipping 126 87	126 87;;
		3)	PAPER=43; set_clipping 126 87	126 87;;
		*)	error "Unimplemented paper code $PAPER";;
		esac
                ;;
82|261|doublepostcardrotated)
				paper=executive;XDIM="6996";  YDIM="4722"
		case "$MODEL" in
		1)	PAPER=261; set_clipping 126 87	126 87;;
		2)	PAPER=82; set_clipping 126 87	126 87;;
		3)	PAPER=82; set_clipping 126 87	126 87;;
		*)	error "Unimplemented paper code $PAPER";;
		esac
                ;;
258|fanfoldgermanlegal)
		PAPER=258;      paper=legal;	XDIM="10200";  YDIM="7800"
		case "$MODEL" in
		1)	set_clipping 192 96	192 96;;
		2)	set_clipping 192 96	192 96;;
		3)	set_clipping 192 96	192 96;;
		*)	error "Unimplemented paper code $PAPER";;
		esac
                ;;
268|photo4x6)
		PAPER=268;      paper=a6;	XDIM="4800";  YDIM="3600"
		case "$MODEL" in
		3)	set_clipping 192 96	192 96;;
		*)	error "Unimplemented paper code $PAPER";;
		esac
                ;;
269|photo5x8)
		PAPER=269;      paper=statement; XDIM="6000";  YDIM="4800"
		case "$MODEL" in
		3)	set_clipping 192 96	192 96;;
		*)	error "Unimplemented paper code $PAPER";;
		esac
                ;;
270|photo10x15)
		PAPER=270;      paper=postcard; XDIM="4818";  YDIM="3590"
		case "$MODEL" in
		3)	set_clipping 192 96	192 96;;
		*)	error "Unimplemented paper code $PAPER";;
		esac
                ;;
*)		error "Unimplemented paper code $PAPER";;
esac
# e.g. /usr/share/ghostscript/7.07/lib/gs_statd.ps
PAPERSIZE="-sPAPERSIZE=$paper";

case "$RES" in
600x600)	XDIM=`expr $XDIM / 2`;;
1200x600)	;;
2400x600)	XDIM=`expr $XDIM \* 2`;;
esac
DIM="${XDIM}x${YDIM}"

#
# Filter thru psnup if N-up printing has been requested
#
case $NUP in
[234689]|1[024568])	PREFILTER="nup";;
*)			PREFILTER=cat;;
esac
if [ "$DEBUG" -ge 9 ]; then
    PREFILTER="tee /tmp/$BASENAME.ps"
fi

#
#	Overload -G.  If the file name ends with ".icm" or ".ICM"
#	then convert the ICC color profile to a Postscript CRD,
#	then prepend it to the users job.  Select the intent
#	using the -I option.
#

create_crd() {
    #
    # Create a Postscript CRD
    #
    ICC2PS=$PREFIX/bin/foo2zjs-icc2ps
    if [ -x $ICC2PS ]; then
	case "$GAMMAFILE" in
	none | none.icm | */none.icm)
	    ;;
	*)
	    if [ -x /usr/bin/logger ]; then
		logger -t "$BASENAME" -p lpr.info -- \
		"`basename $ICC2PS` -o $GAMMAFILE -t$INTENT > $ICCTMP.crd.ps"
	    fi
	    $ICC2PS -o $GAMMAFILE -t$INTENT > $ICCTMP.crd.ps 2>$ICCTMP.log \
		|| error "Problem converting .ICM file to Postscript"
	    ;;
	esac

	PSTOPS_OPTS="$PSTOPS_OPTS -c"
	cat > $ICCTMP.usecie.ps <<-EOF
		%!PS-Adobe-3.0
		<</UseCIEColor true>>setpagedevice
	EOF
	if [ "$QUALITY" = wts -o "$QUALITY" = as ]; then
	    if [ "$QUALITY" = wts ]; then
		cat >> $ICCTMP.usecie.ps <<-EOF
		    << /UseWTS true >> setuserparams
		EOF
	    else
		cat >> $ICCTMP.usecie.ps <<-EOF
		    << /UseWTS false >> setuserparams
		EOF
	    fi
	    cat >> $ICCTMP.usecie.ps <<-EOF
		<<
		    /AccurateScreens true
		    /HalftoneType 1
		    /HalftoneName (Round Dot Screen) cvn
		    /SpotFunction { 180 mul cos exch 180 mul cos add 2 div}
		    /Frequency 137
		    /Angle 37
		>> sethalftone
		EOF
	fi
	cat > $ICCTMP.selcrd.ps <<-EOF
		/Current /ColorRendering findresource setcolorrendering
	EOF
	case "$GAMMAFILE" in
	none | none.icm | */none.icm) GAMMAFILE="$ICCTMP.usecie.ps";;
	*)	GAMMAFILE="$ICCTMP.usecie.ps $ICCTMP.crd.ps $ICCTMP.selcrd.ps";;
	esac
    else
	GAMMAFILE=
    fi
}

#
#	Print the color space from an ICC file using "dd"
#
colorspace() {
    if [ -r "$1" ]; then
	dd if=$1 bs=4c skip=4 count=1 2>/dev/null
    elif [ -r "$SHARE/icm/$1" ]; then
	dd if=$1 bs=4c skip=4 count=1 2>/dev/null
    else
	echo none
    fi
}

if [ $DEBUG -gt 0 ]; then
    ICCTMP=/tmp/icc
else
    ICCTMP=/tmp/icc$$
fi

PSTOPS_OPTS="-n"

if [ "" = "$COLOR" ]; then
    COLORMODE=
    GAMMAFILE=
else
    case "$COLORMODE" in
    default)	COLORMODE=$DEFAULTCOLORMODE;;
    esac

    case "$GAMMAFILE" in
    default)
	    case "$MODEL" in
	    3)	GAMMAFILE=hp-cp1025-rgb-392-bpp1.icm;;
	    *)	GAMMAFILE=DL2312.icm;;
	    esac
	    ;;
    esac

    case "$GSVER" in
    9.*)
	    cs=`colorspace $GAMMAFILE`
	    if [ "$cs" = "CMYK" ]; then
		COLORMODE=gs9
		QUALITY=
		PSTOPS_OPTS="$PSTOPS_OPTS -9 -a"
	    fi
	    ;;
    esac
fi

CRDBASE="$PREFIX/share/foo2zjs/crd"
case "$RES" in
    600x600)	SCREEN=screen1200.ps;;
    1200x600)	SCREEN=screen1200.ps;;
    2400x600)	SCREEN=screen2400.ps;;
esac

case "$COLORMODE" in
0|"")
    # Monochrome
    ;;
gs9)
    # Use new icc method in gs 9.*
    AIB=
    BC=
    if [ -r "$GAMMAFILE" ]; then
	GAMMAFILE="-sOutputICCProfile=$GAMMAFILE"
    elif [ -r "$SHARE/icm/$GAMMAFILE" ]; then
	GAMMAFILE="-sOutputICCProfile=$SHARE/icm/$GAMMAFILE"
    else
	GAMMAFILE=
    fi
    ;;
10|icm)
    # Use old ICM method
    AIB=-A
    BC=-B
    case "$GAMMAFILE" in
    none | none.icm | */none.icm)
	create_crd
	;;
    *.icm|*.ICM|*.icc|*.ICC)
	#
	# Its really an .ICM file, not a gamma file.
	#
	# The file can be a full path name, or the name of a file in $SHARE/icm/
	#
	if [ -r "$GAMMAFILE" ]; then
	    create_crd
	elif [ -r "$SHARE/icm/$GAMMAFILE" ]; then
	    GAMMAFILE="$SHARE/icm/$GAMMAFILE"
	    create_crd
	else
	    GAMMAFILE=
	fi
	;;
    esac
    ;;
1|photo)
    # Photo
    GAMMAFILE="$CRDBASE/prolog.ps"
    GAMMAFILE="$GAMMAFILE $CRDBASE/2300w-1200@150-l250-kx,ucr125,75-per.crd"
    GAMMAFILE="$GAMMAFILE $CRDBASE/$SCREEN"
    ;;
2|graphics)
    # Photo and Text
    GAMMAFILE="$CRDBASE/prolog.ps"
    #GAMMAFILE="$GAMMAFILE $CRDBASE/2300w-1200@150-l250-kx,ucr100,75-per.crd"
    GAMMAFILE="$GAMMAFILE $CRDBASE/kh.crd"
    GAMMAFILE="$GAMMAFILE $CRDBASE/$SCREEN"
    ;;
3|text)
    # Graphic and Text
    GAMMAFILE="$CRDBASE/prolog.ps"
    #GAMMAFILE="$GAMMAFILE $CRDBASE/2300w-1200@150-l250-kx,ucr100,50-per.crd"
    GAMMAFILE="$GAMMAFILE $CRDBASE/kx.crd"
    GAMMAFILE="$GAMMAFILE $CRDBASE/$SCREEN"
    ;;
4|tonersave)
    # Reduced toner
    GAMMAFILE="$CRDBASE/prolog.ps"
    GAMMAFILE="$GAMMAFILE $CRDBASE/2300w-1200@150-l250-kx,ucr100,0-per.crd"
    GAMMAFILE="$GAMMAFILE $CRDBASE/$SCREEN"
    ;;
*.crd)
    GAMMAFILE="$CRDBASE/prolog.ps"
    if [ -f $COLORMODE ]; then
	GAMMAFILE="$GAMMAFILE $COLORMODE"
    elif [ -f $CRDBASE/$COLORMODE ]; then
	GAMMAFILE="$GAMMAFILE $CRDBASE/$COLORMODE"
    else
	error "Can't find CRD '$COLORMODE' in . or in $CRDBASE"
    fi
    GAMMAFILE="$GAMMAFILE $CRDBASE/$SCREEN"
    ;;
*)
    error "Unknown color method '$COLORMODE'"
    ;;
esac

if [ "$COLOR" != "" -a "$QUALITY" = wts ]; then
    PSTOPS_OPTS="$PSTOPS_OPTS -w"
elif [ "$COLOR" != "" -a "$QUALITY" = as ]; then
    PSTOPS_OPTS="$PSTOPS_OPTS -a"
fi

if [ "" != "$COLOR" ]; then
    if [ "" = "$AIB" -a "" = "$BC" ]; then
	# Faster, but can't handle AllIsBlack or BlackClears
	GSDEV=-sDEVICE=pksmraw
    else
	# Can't handle different size pages
	GSDEV=-sDEVICE=bitcmyk
    fi
fi

#
#	Figure out USERNAME
#
if [ "$LPUSER" != "" ]; then
    USER="$LPUSER@$LPHOST"
else
    USER=""
fi

#
#	Main Program, just cobble together the pipeline and run it
#
#	The malarky with file descriptors 1 and 3 is to avoid a bug in
#	(some versions?) of Ghostscript where Postscript's stdout gets
#	intermingled with the printer drivers output, resulting in
#	corrupted image data.
#
GS="$GSBIN -q -dBATCH -dSAFER -dQUIET -dNOPAUSE -dNOINTERPOLATE"

foo2zjs-pstops $PSTOPS_OPTS | \
$PREFILTER \
| ($GS $PAPERSIZE -g$DIM -r$RES $GSDEV $GSOPTS \
    -sOutputFile="|cat 1>&3" $GAMMAFILE -_ >/dev/null) 3>&1 \
| foo2zjs -r$RES -g$DIM -p$PAPER -m$MEDIA -n$COPIES -d$DUPLEX -s$SOURCE \
	    -z$MODEL $COLOR $CLIP_UL $CLIP_LR $CLIP_LOG $SAVETONER -T$DENSITY \
	    -J "$LPJOB" -U "$USER" \
	    $BC $AIB $COLOR2MONO $NOPLANES $EXTRAPAD -D$DEBUG

#
#	Log the command line, for debugging and problem reports
#
if [ -x /usr/bin/logger ]; then
    logger -t "$BASENAME" -p lpr.info -- \
	"$GSBIN $PAPERSIZE -g$DIM -r$RES $GSDEV $GSOPTS $GAMMAFILE"
    logger -t "$BASENAME" -p lpr.info -- \
	"foo2zjs -r$RES -g$DIM -p$PAPER -m$MEDIA \
-n$COPIES -d$DUPLEX -s$SOURCE -z$MODEL $COLOR $CLIP_UL $CLIP_LR $CLIP_LOG \
$SAVETONER -T$DENSITY $BC $AIB $COLOR2MONO $NOPLANES $EXTRAPAD"
fi

#
#	Remove cruft
#
if [ $DEBUG -eq 0 ]; then
    for i in crd.ps log usecie.ps selcrd.ps
    do
	file="$ICCTMP.$i"
	[ -f $file ] && rm -f $file
    done
    [ -f "$TMPFILE" ] && rm -f $TMPFILE
fi

exit 0