#!/bin/zsh

ProcessColorModel=DeviceGray
ProcessColorModel=DeviceRGB  # DeviceRGB, DeviceGray
STP_InputSlot=Matte          # Standard, Velvet, CD, Matte
STP_MediaType=Coated         # Plain, Coated, Transparency
STP_Quality=UltraPhoto       # Draft, Standard, High, UltraPhoto
RES=5760x1440
STP_Resolution=5760x1440sw
STP_PrintingDirection=Unidirectional
STP_UseGloss=False
STP_ImageType=Text

#debug
STP_MediaType=Plain

#RES=1440x1440
#STP_Resolution=1440x1440sw
#STP_MediaType=Coated         # Plain, Coated, Transparency
STP_MediaType=Transparency         # Plain, Coated, Transparency

#####

usage() {
    cat <<EOF
Usage: r800print.sh [options] psfile
    -n NOP
    --back     Use paper tray
    --cd       Use CD tray
    --draft    Draft quality
    -A .. -Z   Select destination PCB tray slot
    -1 .. -9   Select source page
    --red ...  Map black to another color
EOF
}
#####

SETCOLOR=""
FILE=""
SPEC=""
NOP=""
while [ "$*" ]; do
    case "$1" in
	-n)         NOP=1 ;;
	--back)     STP_InputSlot=Standard ;;
	--cd)       STP_InputSlot=CD ;;
	--720)      RES=720x720;
	            STP_Resolution=720x720 ;;
	--draft)    RES=720x720;
	            STP_Resolution=720x720;
		    STP_Quality=Draft;
	            STP_MediaType=Plain;
		    STP_PrintingDirection=None ;;
	-A)         W=75;  H=100; X=40; Y=20  ;;
	-B)         W=50;  H=100; X=40; Y=20  ;;
	-C)         W=25;  H=100; X=40; Y=20  ;;
	-D)         W=100; H=50;  X=40; Y=185 ;;
	-E)         W=75;  H=50;  X=40; Y=185 ;;
	-F)         W=50;  H=50;  X=40; Y=185 ;;
	-G)         W=25;  H=50;  X=40; Y=185 ;;
	-l)         X=$((X-W)) ;;
	-r)         X=$((X+W)) ;;
	-a)         Y=$((Y-H)) ;;
	-b)         Y=$((Y+H)) ;;
	--red)      SETCOLOR="1 0 0 setrgbcolor" ;;
	--green)    SETCOLOR="0 1 0 setrgbcolor" ;;
	--blue)     SETCOLOR="0 0 1 setrgbcolor" ;;
	--cyan)     SETCOLOR="0 1 1 setrgbcolor" ;;
	--magenta)  SETCOLOR="1 0 1 setrgbcolor" ;;
	--yellow)   SETCOLOR="1 1 0 setrgbcolor" ;;
	-[0-9]*)
		    if [ "$W" ]; then
			CX=$((  X+W*0.5 - 105.0 ))
			CY=$((-(Y+H*0.5 - 148.5)))
		    else
			CX=0
			CY=0
		    fi;
		    if [ "$SPEC" = "" ]; then
			SPEC="99:$1(${CX}mm,${CY}mm)"
		    else 
			SPEC="$SPEC+$1(${CX}mm,${CY}mm)"
		    fi
	           ;;

	--help)     usage; exit 0 ;;
	-*)         (echo "Unknown option: $1"; usage) 1>&2; exit 1 ;;
	*)          FILE=$1
    esac
    shift
done

if [ "$SPEC" != "" ]; then
    pstops -pa4 "$SPEC" $FILE /tmp/x.ps  ||  exit 1
    FILE=/tmp/x.ps
    if [ "$W" ]; then
	XPT=$(((105.0-W*0.5)*595/210))
	YPT=$(((148.5-H*0.5)*842/297))
	WPT=$((W*595.0/210))
	HPT=$((H*842.0/297))
	sed -e "s/PStoPSclip clip/PStoPSclip clip newpath $XPT $YPT moveto $WPT 0 rlineto 0 $HPT rlineto -$WPT 0 rlineto closepath clip/"  < $FILE  > /tmp/y.ps
	FILE=/tmp/y.ps
    fi
fi
if [ "$SETCOLOR" ]; then
    sed -e "s/^0 gray/$SETCOLOR/g" < $FILE > /tmp/z.ps
    FILE=/tmp/z.ps
fi

#####

STP=""
for i in STP_InputSlot STP_MediaType STP_Quality STP_Resolution STP_PrintingDirection STP_UseGloss STP_ImageType; do
    v=$(eval echo \$$i)
    echo "$i : $v"
    STP="$STP,$i=$v"
done

echo $STP $FILE

#####

if [ "$NOP" ]; then
    gv -media=a4 -geometry 530x624 -scale=0.25 $FILE
    exit 1
fi

gs  -dPARANOIDSAFER -dNOPAUSE -dBATCH \
    -sDEVICE=ijs -sIjsServer=ijsgutenprint.5.2 \
    -dIjsUseOutputFD -sOutputFile=/dev/usb/lp0 \
    -sDeviceManufacturer=Epson -sDeviceModel=escp2-r800 \
    -dDEVICEWIDTHPOINTS=595 -dDEVICEHEIGHTPOINTS=842 -r$RES \
    -sProcessColorModel=$ProcessColorModel \
    -sIjsParams=STP_VERSION=5.2.3,STP_CDInnerRadius=Small,STP_ColorCorrection=None,STP_DitherAlgorithm=None,STP_EnableBlackDensity=Disabled,STP_BlackDensity=1.000000,STP_EnableBlackGamma=Disabled,STP_BlackGamma=1.000000,STP_EnableBlackTrans=Disabled,STP_BlackTrans=1.000000,STP_EnableBlueDensity=Disabled,STP_BlueDensity=1.000000,STP_EnableCyanBalance=Disabled,STP_CyanBalance=1.000000,STP_EnableCyanDensity=Disabled,STP_CyanDensity=1.000000,STP_EnableCyanGamma=Disabled,STP_CyanGamma=1.000000,STP_EnableDensity=Disabled,STP_Density=1.000000,STP_EnableDropSize1=Disabled,STP_DropSize1=1.000000,STP_EnableDropSize2=Disabled,STP_DropSize2=0.000000,STP_EnableDropSize3=Disabled,STP_DropSize3=0.000000,STP_EnableGCRLower=Disabled,STP_GCRLower=0.200000,STP_EnableGCRUpper=Disabled,STP_GCRUpper=0.500000,STP_EnableGamma=Disabled,STP_Gamma=1.000000,STP_EnableGlossLimit=Disabled,STP_GlossLimit=1.000000,STP_EnableInkLimit=Disabled,STP_InkLimit=8.000000,STP_EnableMagentaBalance=Disabled,STP_MagentaBalance=1.000000,STP_EnableMagentaDensity=Disabled,STP_MagentaDensity=1.000000,STP_EnableMagentaGamma=Disabled,STP_MagentaGamma=1.000000,STP_EnableRedDensity=Disabled,STP_RedDensity=1.000000,STP_EnableYellowBalance=Disabled,STP_YellowBalance=1.000000,STP_EnableYellowDensity=Disabled,STP_YellowDensity=1.000000,STP_EnableYellowGamma=Disabled,STP_YellowGamma=1.000000,STP_FullBleed=False,STP_InkSet=cmykrbmatte,STP_InkType=None,STP_LinearContrast=False,STP_Weave=None,STP_Brightness=1.000000,STP_Contrast=1.000000$STP $FILE  2> /tmp/r800.log

