#! /bin/sh /usr/share/dpatch/dpatch-run
## 60_pxl-cups-driver-pdf.dpatch by  <till.kamppeter@gmail.com>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: No description.

@DPATCH@
diff -urNad ghostscript-8.63.dfsg.1~/cups/pstopxl.in ghostscript-8.63.dfsg.1/cups/pstopxl.in
--- ghostscript-8.63.dfsg.1~/cups/pstopxl.in	2007-12-05 01:08:50.000000000 +0100
+++ ghostscript-8.63.dfsg.1/cups/pstopxl.in	2008-11-26 11:21:02.000000000 +0100
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# "$Id: pstopxl.in,v 1.1.2.2 2004/06/29 13:15:10 mike Exp $"
+# "$Id: pstopxl.in 9239 2008-11-24 19:15:04Z till $"
 #
 # CUPS PCL XL/PCL 6 filter script for Ghostscript.
 #
@@ -45,23 +45,144 @@
     device="pxlmono"
 fi
 
+# Apply PPD settings.
+
+ps_code=
+ppd_opts=
+
+resolution=
+eval "$(printf "%s" "$5" | sed -nre 's/.*(^|\s)Resolution=([0-9.]+(x[0-9.]+)?).*/resolution="${resolution:-\2}"/p')"
+if test -e "$PPD"; then
+  eval "$(sed -nre 's/^\*DefaultResolution:\s+([0-9.]+(x[0-9.]+)?).*/resolution="${resolution:-\1}"/p' "$PPD")"
+fi
+echo "DEBUG: Resolution: $resolution" >&2
+if test -n "$resolution"; then
+  ppd_opts="${ppd_opts:+$ppd_opts }-r$resolution"
+fi
+
+pagesize=
+eval "$(printf "%s" "$5" | sed -nre 's/.*(^|\s)(media|PageSize)=(\S+).*/pagesize="${pagesize:-\3}"/p')"
+if test -e "$PPD"; then
+  eval "$(sed -nre 's/^\*DefaultPageSize:\s+(\S+).*/pagesize="${pagesize:-\1}"/p' "$PPD")"
+fi
+echo "DEBUG: Page size: $pagesize" >&2
+width=
+height=
+if test -n "$pagesize" && test -e "$PPD"; then
+  eval "$(sed -nre 's|^\*PaperDimension\s+'"$pagesize"'/[^:]+:\s+"(\S+)\s+(\S+)".*|width="\1"; height="\2"|p' "$PPD")"
+fi
+echo "DEBUG: Width: $width, height: $height" >&2
+if test -n "$width"; then
+  ppd_opts="${ppd_opts:+$ppd_opts }-dDEVICEWIDTHPOINTS=$width"
+fi
+if test -n "$height"; then
+  ppd_opts="${ppd_opts:+$ppd_opts }-dDEVICEHEIGHTPOINTS=$height"
+fi
+
+bl_x=
+bl_y=
+tr_x=
+tr_y=
+if test -n "$pagesize" && test -e "$PPD"; then
+  eval "$(sed -nre 's|^\*ImageableArea\s+'"$pagesize"'/[^:]+:\s+"(\S+)\s+(\S+)\s+(\S+)\s+(\S+)".*|bl_x="\1"; bl_y="\2"; tr_x="\3"; tr_y="\4"|p' "$PPD")"
+fi
+echo "DEBUG: Absolute margins: $bl_x, $bl_y, $tr_x, $tr_y" >&2
+margin_l=
+margin_b=
+margin_r=
+margin_t=
+if test -n "$width" && test -n "$height" && \
+   test -n "$bl_x" && test -n "$bl_y" && \
+   test -n "$tr_x" && test -n "$tr_y"; then
+  margin_l="$bl_x"
+  margin_b="$bl_y"
+  margin_r="$(printf "scale=8; (%s)-(%s)\n" "$width" "$tr_x" | bc)"
+  margin_t="$(printf "scale=8; (%s)-(%s)\n" "$height" "$tr_y" | bc)"
+fi
+echo "DEBUG: Relative margins: $margin_l, $margin_b, $margin_r, $margin_t" >&2
+if test -n "$margin_l" && test -n "$margin_b" && \
+   test -n "$margin_r" && test -n "$margin_t"; then
+  ps_code="${ps_code:+$ps_code }<</.HWMargins[$margin_l $margin_b $margin_r $margin_t] /Margins[0 0]>>setpagedevice"
+fi
+
+inputslot=
+eval "$(printf "%s" "$5" | sed -nre 's/.*(^|\s)(InputSlot)=(\S+).*/inputslot="${inputslot:-\3}"/p')"
+if test -e "$PPD"; then
+  eval "$(sed -nre 's/^\*DefaultInputSlot:\s+(\S+).*/inputslot="${inputslot:-\1}"/p' "$PPD")"
+fi
+echo "DEBUG: InputSlot: $inputslot" >&2
+inputslot_val=
+if test -n "$inputslot" && test -e "$PPD"; then
+  eval "$(sed -nre 's|^\*InputSlot\s+'"$inputslot"'/[^:]+:\s+"\s*<\s*<\s*/MediaPosition\s*([0-9]+)\s*>\s*>\s*setpagedevice\s*".*|inputslot_val="\1"|p' "$PPD")"
+fi
+echo "DEBUG: Value for MediaPosition: $inputslot_val" >&2
+if test -n "$inputslot_val"; then
+  ppd_opts="${ppd_opts:+$ppd_opts }-dMediaPosition=$inputslot_val"
+fi
+
+colormodel=
+eval "$(printf "%s" "$5" | sed -nre 's/.*(^|\s)(ColorModel)=(\S+).*/colormodel="${colormodel:-\3}"/p')"
+if test -e "$PPD"; then
+  eval "$(sed -nre 's/^\*DefaultColorModel:\s+(\S+).*/colormodel="${colormodel:-\1}"/p' "$PPD")"
+fi
+echo "DEBUG: ColorModel: $colormodel" >&2
+colormodel_val=
+if test -n "$colormodel" && test -e "$PPD"; then
+  eval "$(sed -nre 's|^\*ColorModel\s+'"$colormodel"'/[^:]+:\s+"\s*<\s*<\s*/BitsPerPixel\s*([0-9]*)\s*>\s*>\s*setpagedevice\s*".*|colormodel_val="\1"|p' "$PPD")"
+fi
+echo "DEBUG: Value for BitsPerPixel: $colormodel_val" >&2
+if test -n "$colormodel_val"; then
+  ppd_opts="${ppd_opts:+$ppd_opts }-dBitsPerPixel=$colormodel_val"
+fi
+
+duplex=
+eval "$(printf "%s" "$5" | sed -nre 's/.*(^|\s)(Duplex)=(\S+).*/duplex="${duplex:-\3}"/p')"
+if test -e "$PPD"; then
+  eval "$(sed -nre 's/^\*DefaultDuplex:\s+(\S+).*/duplex="${duplex:-\1}"/p' "$PPD")"
+fi
+echo "DEBUG: Duplex: $duplex" >&2
+duplex_val=
+tumble_val=
+if test -n "$duplex" && test -e "$PPD"; then
+  eval "$(sed -nre 's|^\*Duplex\s+'"$duplex"'/[^:]+:\s+"\s*<\s*<\s*/Duplex\s*(\S*)\s*(/Tumble\s*(\S*)\s*)?>\s*>\s*setpagedevice\s*".*|duplex_val="\1"; tumble_val="\3"|p' "$PPD")"
+fi
+echo "DEBUG: Value for Duplex: $duplex_val; Value for Tumble: $tumble_val" >&2
+if test -n "$duplex_val"; then
+  ppd_opts="${ppd_opts:+$ppd_opts }-dDuplex=$duplex_val"
+fi
+if test -n "$tumble_val"; then
+  ppd_opts="${ppd_opts:+$ppd_opts }-dTumble=$tumble_val"
+fi
+
+
+echo "DEBUG: PPD options: $ppd_opts" >&2
+echo "DEBUG: PostScript code from options: $ps_code" >&2
+
 # Options we use with Ghostscript...
-gsopts="-dQUIET -dDEBUG -dPARANOIDSAFER -dNOPAUSE -dBATCH"
-gsopts="$gsopts -dNOMEDIAATTRS -sDEVICE=$device -sstdout=%stderr"
+gsopts="-dQUIET -dPARANOIDSAFER -dNOPAUSE -dBATCH"
+gsopts="$gsopts -sDEVICE=$device -sstdout=%stderr"
 
 # See if we have a filename on the command-line...
-if test -z "$6"; then
-	ifile="-_"
-else
-	ifile="$6"
+if [ -n "$6" ]; then
+  exec <"$6"
 fi
 
+# We read the data into a temporary file as Ghostscript needs this to be
+# able to also work with PDF input
+tempfiles=
+trap 'rm -f $tempfiles' 0 1 2 13 15
+
+ifile=$(mktemp -t pstopxl.XXXXXX)
+tempfiles="$tempfiles $ifile"
+
+cat >"$ifile"
+
 echo INFO: Starting GPL Ghostscript `$bindir/@GS@ --version`... 1>&2
-echo DEBUG: Running $bindir/@GS@ $gsopts -sOUTPUTFILE="%stdout" "$ifile" 1>&2
+echo DEBUG: Running $bindir/@GS@ $gsopts $ppd_opts -sOUTPUTFILE="%stdout" -c "$ps_code" -f "$ifile" 1>&2
 
 # Now run Ghostscript...
-exec $bindir/@GS@ $gsopts -sOUTPUTFILE="%stdout" "$ifile"
+$bindir/@GS@ $gsopts $ppd_opts -sOUTPUTFILE="%stdout" -c "$ps_code" -f "$ifile"
 
 #
-# End of "$Id: pstopxl.in,v 1.1.2.2 2004/06/29 13:15:10 mike Exp $".
+# End of "$Id: pstopxl.in 9239 2008-11-24 19:15:04Z till $".
 #
diff -urNad ghostscript-8.63.dfsg.1~/cups/pxlcolor.ppd ghostscript-8.63.dfsg.1/cups/pxlcolor.ppd
--- ghostscript-8.63.dfsg.1~/cups/pxlcolor.ppd	2007-09-08 19:05:59.000000000 +0200
+++ ghostscript-8.63.dfsg.1/cups/pxlcolor.ppd	2008-11-26 11:20:54.000000000 +0100
@@ -1,6 +1,6 @@
 *PPD-Adobe: "4.3"
 *%
-*% "$Id: pxlcolor.ppd,v 1.1.2.1 2003/07/20 22:54:45 mike Exp $"
+*% "$Id: pxlcolor.ppd 9239 2008-11-24 19:15:04Z till $"
 *%
 *%   Sample color PCL XL/PCL 6 driver PPD file for the Common UNIX
 *%   Printing System (CUPS).
@@ -32,7 +32,8 @@
 *Product:	"(GPL Ghostscript)"
 *cupsVersion:	1.1
 *cupsManualCopies: False
-*cupsFilter:	"application/vnd.cups-postscript 0 pstopxl"
+*cupsFilter:	"application/vnd.cups-postscript 100 pstopxl"
+*cupsFilter:	"application/vnd.cups-pdf 0 pstopxl"
 *ModelName:     "HP Color LaserJet Series PCL 6"
 *ShortNickName: "HP Color LaserJet Series PCL 6"
 *NickName:      "HP Color LaserJet Series PCL 6 CUPS"
@@ -54,6 +55,15 @@
 *UIConstraints: *PageSize A5 *InputSlot Envelope
 *UIConstraints: *PageSize B5 *InputSlot Envelope
 *UIConstraints:	*Duplex *OptionDuplex False
+*UIConstraints: *InputSlot Envelope *PageSize Executive
+*UIConstraints: *InputSlot Envelope *PageSize Letter
+*UIConstraints: *InputSlot Envelope *PageSize Legal
+*UIConstraints: *InputSlot Envelope *PageSize Tabloid
+*UIConstraints: *InputSlot Envelope *PageSize A3
+*UIConstraints: *InputSlot Envelope *PageSize A4
+*UIConstraints: *InputSlot Envelope *PageSize A5
+*UIConstraints: *InputSlot Envelope *PageSize B5
+*UIConstraints:	*OptionDuplex False *Duplex
 
 *OpenUI *PageSize/Media Size: PickOne
 *OrderDependency: 10 AnySetup *PageSize
@@ -143,10 +153,11 @@
 
 *OpenUI *Resolution/Output Resolution: PickOne
 *OrderDependency: 20 AnySetup *Resolution
-*DefaultResolution: 300dpi
+*DefaultResolution: 600dpi
 *Resolution 150dpi/150 DPI: "<</HWResolution[150 150]>>setpagedevice"
 *Resolution 300dpi/300 DPI: "<</HWResolution[300 300]>>setpagedevice"
 *Resolution 600dpi/600 DPI: "<</HWResolution[600 600]>>setpagedevice"
+*Resolution 1200dpi/1200 DPI: "<</HWResolution[1200 1200]>>setpagedevice"
 *CloseUI: *Resolution
 
 *OpenUI *Duplex/Double-Sided Printing: PickOne
@@ -202,5 +213,5 @@
 *Font ZapfChancery-MediumItalic: Standard "(001.007S)" Standard ROM
 *Font ZapfDingbats: Special "(001.004S)" Standard ROM
 *%
-*% End of "$Id: pxlcolor.ppd,v 1.1.2.1 2003/07/20 22:54:45 mike Exp $".
+*% End of "$Id: pxlcolor.ppd 9239 2008-11-24 19:15:04Z till $".
 *%
diff -urNad ghostscript-8.63.dfsg.1~/cups/pxlmono.ppd ghostscript-8.63.dfsg.1/cups/pxlmono.ppd
--- ghostscript-8.63.dfsg.1~/cups/pxlmono.ppd	2007-09-08 19:05:59.000000000 +0200
+++ ghostscript-8.63.dfsg.1/cups/pxlmono.ppd	2008-11-26 11:20:54.000000000 +0100
@@ -1,6 +1,6 @@
 *PPD-Adobe: "4.3"
 *%
-*% "$Id: pxlmono.ppd,v 1.1.2.1 2003/07/20 22:54:45 mike Exp $"
+*% "$Id: pxlmono.ppd 9239 2008-11-24 19:15:04Z till $"
 *%
 *%   Sample monochrome PCL XL/PCL 6 driver PPD file for the Common UNIX
 *%   Printing System (CUPS).
@@ -32,7 +32,8 @@
 *Product:	"(GPL Ghostscript)"
 *cupsVersion:	1.1
 *cupsManualCopies: False
-*cupsFilter:	"application/vnd.cups-postscript 0 pstopxl"
+*cupsFilter:	"application/vnd.cups-postscript 100 pstopxl"
+*cupsFilter:	"application/vnd.cups-pdf 0 pstopxl"
 *ModelName:     "HP LaserJet Series PCL 6"
 *ShortNickName: "HP LaserJet Series PCL 6"
 *NickName:      "HP LaserJet Series PCL 6 CUPS"
@@ -54,6 +55,15 @@
 *UIConstraints: *PageSize A5 *InputSlot Envelope
 *UIConstraints: *PageSize B5 *InputSlot Envelope
 *UIConstraints:	*Duplex *OptionDuplex False
+*UIConstraints: *InputSlot Envelope *PageSize Executive
+*UIConstraints: *InputSlot Envelope *PageSize Letter
+*UIConstraints: *InputSlot Envelope *PageSize Legal
+*UIConstraints: *InputSlot Envelope *PageSize Tabloid
+*UIConstraints: *InputSlot Envelope *PageSize A3
+*UIConstraints: *InputSlot Envelope *PageSize A4
+*UIConstraints: *InputSlot Envelope *PageSize A5
+*UIConstraints: *InputSlot Envelope *PageSize B5
+*UIConstraints:	*OptionDuplex False *Duplex
 
 *OpenUI *PageSize/Media Size: PickOne
 *OrderDependency: 10 AnySetup *PageSize
@@ -196,5 +206,5 @@
 *Font ZapfChancery-MediumItalic: Standard "(001.007S)" Standard ROM
 *Font ZapfDingbats: Special "(001.004S)" Standard ROM
 *%
-*% End of "$Id: pxlmono.ppd,v 1.1.2.1 2003/07/20 22:54:45 mike Exp $".
+*% End of "$Id: pxlmono.ppd 9239 2008-11-24 19:15:04Z till $".
 *%
