#!/bin/sh
#
# $Header: /cvsroot/m2300w/m2300w/src/m2300w-wrapper.in,v 1.2 2004/06/26 22:26:57 gfuer Exp $
#
# Copyright (C) 2003 Rick Richardson & 2004 Leif Birkenfeld
#
# 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 <rickr@mn.rr.com>
#          Leif Birkenfeld <leif@birkenfeld-home.de>
#          Gerhard Fuernkranz <gfuer@users.sourceforge.net>

#*********************************************************************
# This Version of the wrapper-script for the Minolta Magicolor 2300W
# is heavy based on the Script from Rick's 'foo2zjs-wrapper' for the
# Minolta Magiccolor 2300DL.
# So i will leaf his copyright on it.
#*********************************************************************

cat > /tmp/m2300w.log <<EOF
#### neuer Druck ####
Wrapper Aufruf:
$0 $@

EOF

PROGNAME="$0"
BASENAME=`basename $PROGNAME`

PACKAGE_NAME="m2300w"
PACKAGE_VERSION="0.2"
PREFIX="/usr"
BINDIR="$PREFIX/bin"
SHARE="$PREFIX/share/$PACKAGE_NAME/$PACKAGE_VERSION"
PSFILES="$SHARE/psfiles"
PROLOG="$PSFILES/prolog.ps" 
DRIVER="$BINDIR/m2300w"

testmode=0

usage() {
	cat <<EOF

Usage:
$BASENAME [options] [ps-file]

Foomatic printer wrapper for the m2300w printer driver.
This script reads a Postscript ps-file or standard input
and converts it to the Minolta Magicolor 2300W proprietary
printer format.

Version $PACKAGE_VERSION

Normal Options:
-c 		  Print in color (else monochrome)
-C colorModel     Use the specified colorManagment [$COLORMAN]
		    0=Photo Only
		    1=Photo and Text 
		    2=Graphic and Text 
		    3=Less Toner

-m media          Media code to send to printer [$MEDIA]
		    0=standard, 1=carton (heavy paper),
		    2=transparency, 3=envelope, 4=letterhead,
		    5=postcard, 6=labels
-p paper          Paper code [$PAPER]
                    4=A4, 6=B5 JIS, 8=A5, 18=Folio,25=Legal,
		    26=Government Legal, 27=Letter, 31=Executive,
                    33=Statement, 36=Kuvert Monarch, 37=Kuver #10,
		    38=Kuvert DL, 39=Kuvert C5, 40=Kuvert C6, 
		    41=B5 ISO
-r resolution     Set device resolution [$RESCODE]
		    1=600x600dpi (not implemented yet !)
		    2=1200x600dpi
-2 / -4           2-up, 4-up (uses psnup)
-s		  save Toner
                    Discards every second pixel in a chequerboard 
		    order to save 50 percent toner. 
		    It works fine for text and graphic, but it dosn't 
		    for photos.

EOF
											    
exit 1
}

#
#N-up-ify the job.  Requires psnup from psutils package
#
nup2() {
    tr '\r' '\n' | psnup -d2 $nPC -2 -m.2in -q 
}

nup4() {
    tr '\r' '\n' | psnup -d2 $nPC -4 -m.3in -q 
}

error() {
    echo "$BASENAME: $1" >&2
    exit 1
}

#
#       Process the options
#

# use pre-packaged ghostscript 8.xx, if available
if test -f $PREFIX/share/$PACKAGE_NAME/gs/bin/gs-m2300w; then
    GSBIN=$PREFIX/share/$PACKAGE_NAME/gs/bin/gs-m2300w
else
    GSBIN=gs
fi

CMDLINE="$*"
DUPLEX=0
COLOR="-c 1"
COLORMAN=1
MEDIA=0
PAPER=4
RESCODE=2
NUP=
BOOK=
GSDEV=-sDEVICE=pbmraw
CRDFILE=
OVLPI=0
GSOPT=

while getopts "24cC:d:m:n:p:r:R:sg:h?" opt
do
    case $opt in
    c) COLOR="-c 2"; GSDEV=-sDEVICE=pksmraw;;
    C) COLORMAN="$OPTARG";;
    d) DUPLEX="$OPTARG";;
    m) MEDIA="$OPTARG";;
    p) PAPER="$OPTARG";;
    r) RESCODE="$OPTARG";;
    R) RENDERFILE="$OPTARG";;
    s) SAFETONER="-s";;
    g) GSOPT="$OPTARG";;
    2) NUP="2";;
    4) NUP="4";;
    h|\?)
	if [ "$CMDLINE" != "-?" -a "$CMDLINE" != -h ]; then
	    echo "Illegal command:"
	    echo "$0 $CMDLINE"
	    echo
	fi
    usage;;
    esac
done

shift `expr $OPTIND - 1`

case "$RESCODE" in
#1) RESMD="1200" ;RES="-r600x600";; //coming soon
2)  RESMD="1200"; RES="-r1200x600";;
*)  error "Unknown resolution code $RESCODE";;
esac

RESCODESTR="-r $RESCODE";

case "$COLORMAN" in
0) GSCRD2="-l250-kx,ucr125,75-per.crd";;
1) GSCRD2="-l250-kx,ucr100,75-per.crd";;
2) GSCRD2="-l250-kx,ucr100,50-per.crd";;
3) GSCRD2="-l250-kx,ucr100,0-per.crd";;
*) GSCRD2="-l250-kx,ucr100,0-per.crd";;
esac

SCREEN="$PSFILES/screen${RESMD}.ps"
CRDFILE="$PSFILES/2300w-${RESMD}@150${GSCRD2}";

#CRDFILE="$PSFILES/2300w-1200@150s-per.crd";
#CRDFILE="$PSFILES/2300w-1200@150n-l200-kx,ucr100,50-per.crd";

case "$PAPER" in
4|A4|a4) 				nPC="-pa4"; 	gsPaperCode="-sPAPERSIZE=a4";	paper="4"; 	resX="4752"; resY="6808"; expMedia="-1";;
6|B5JIS|b5jis) 				nPC=""; 	gsPaperCode=""	;	paper="6"; 	resX="4088"; resY="5856"; expMedia="-1";;
8|A5|a5)				nPC="-pa5"; 	gsPaperCode="-sPAPERSIZE=a5";	paper="8"; 	resX="3288"; resY="4752"; expMedia="-1";;
18|FOLIO|folio|Folio)			nPC="-pfolio"; 	gsPaperCode=""	;	paper="18"; 	resX="4752"; resY="7584"; expMedia="-1";;
25|legal|LEGAL|Legal)			nPC="-plegal"; 	gsPaperCode="-sPAPERSIZE=legal";	paper="25";	resX="4896"; resY="8136"; expMedia="-1";;
26)					nPC=""; 	gsPaperCode="-sPAPERSIZE=a4";	paper="26";	resX="4896"; resY="7592"; expMedia="-1";;
27|Letter|letter|LETTER)		nPC="-pletter"; 	gsPaperCode="-sPAPERSIZE=letter";	paper="27";	resX="4896"; resY="6392"; expMedia="-1";;
31|Executive|EXECUTIVE|executive)	nPC=""; 	gsPaperCode=""	;	paper="31";	resX="6096"; resY="4144"; expMedia="-1";;
33|Statement|statement|STATEMENT)	nPC=""; 	gsPaperCode=""	;	paper="33";	resX="3096"; resY="4896"; expMedia="-1";;
36)					nPC=""; 	gsPaperCode=""	;	paper="36";	resX="2120"; resY="4296"; expMedia="3";;
37)					nPC=""; 	gsPaperCode=""	;	paper="37";	resX="2272"; resY="5496"; expMedia="3";;
38)					nPC=""; 	gsPaperCode=""	;	paper="38";	resX="2392"; resY="4992"; expMedia="3";;
39)					nPC=""; 	gsPaperCode=""	;	paper="39";	resX="3616"; resY="5200"; expMedia="3";;
40)					nPC=""; 	gsPaperCode=""	;	paper="40";	resX="2480"; resY="3616"; expMedia="3";;
41)					nPC=""; 	gsPaperCode=""	;	paper="41";	resX="3944"; resY="5696"; expMedia="3";;
*) error "Unknown paper code $PAPER";;
esac
let resX=$resX*$RESCODE;
PAPERSIZE="-g$resX"x"$resY";
PAPERCODE="-p $paper";



case "$MEDIA" in
0|standard)				MEDIA="0";;
1|carton)				MEDIA="1";;
2|transparency)				MEDIA="2";;
3|envelope)				MEDIA="3";;
4|letterhead)				MEDIA="4";;
5|postcard)				MEDIA="5";;
6|labels)				MEDIA="6";;
[0-9]*);;
*)error "Unknown media code $MEDIA";;
esac

# overwrite mediacode if the selected paper requires special media (according to the original windows driver)
case "$expMedia" in
-1)					MEDIA="$MEDIA";;
*)					MEDIA="$expMedia";;
esac
MEDIACODE="-m $MEDIA";


#
# 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

#
# Filter thru psnup if N-up printing has been requested
#
case $NUP in
2)PREFILTER="nup2";;
4)PREFILTER="nup4";;
*)PREFILTER=cat;;
esac


if [ "$RENDERFILE" = ""  ]; then
    RENDERFILE="$PROLOG $CRDFILE $SCREEN"
fi

GS="$GSBIN -q -dBATCH -dSAFER -dQUIET -dNOPAUSE"

cat >> /tmp/m2300w.log <<EOF

Prolog= $PROLOG

CRD= $CRDFILE

Alle= $RENDERFILE

Aufruf GS und m2300w:

$PREFILTER \
| ($GS $gsPaperCode $RES $GSDEV $GSOPT \
    -sOutputFile="|cat 1>&3" $RENDERFILE - >>/tmp/m2300w.log) 3>&1 \
| tee /tmp/m2300w_gsout.pbm | $DRIVER $M2300WUCR $SAFETONER $PAPERCODE $MEDIACODE -i - -o - $COLOR $RESCODESTR | tee /tmp/m2300w.prn


EOF

$PREFILTER \
| ($GS $gsPaperCode $RES $GSDEV $GSOPT \
    -sOutputFile="|cat 1>&3" $RENDERFILE - >>/tmp/m2300w.log) 3>&1 \
| tee /tmp/m2300w_gsout.pbm | $DRIVER $M2300WUCR $SAFETONER $PAPERCODE $MEDIACODE -i - -o - $COLOR $RESCODESTR | tee /tmp/m2300w.prn


