#!/bin/sh

#* Copyright (C) 2003  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.
#*
#* Author: Rick Richardson <rickr@mn.rr.com>

PROGNAME="$0"

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

	Foomatic printer wrapper for the foo2zjs printer driver.

Options:
-c                Print in color (else black and white)
-d duplex         Duplex code to send to printer [$DUPLEX]
-m media          Media code to send to printer [$MEDIA]
                    1=standard, 2=transparency, 3=glossy, 257=envelope,
                    259=letterhead, 261=thickstock, 262=postcard, 263=labels
-p paper          Paper code [$PAPER]
                    1=letter, 5=legal, 7=executive, 9=A4, 11=A5, 13=B5
-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
-2 / -4           2-up, 4-up
-D lvl            Set Debug level [$DEBUG]
EOF

	exit 1
}

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

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

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

#
#       Process the options
#
#	-p1 -m1 -r1200x600 -c -n1
CMDLINE="$*"
DEBUG=0
DUPLEX=1
COLOR=
MEDIA=1
COPIES=1
PAPER=1
RES=1200x600
SOURCE=7
NUP=
BC=
AIB=
GSDEV=-sDEVICE=pbmraw
unset OPTIND
while getopts "24cd:m:n:p:r:s:ABD:h?" opt
do
	case $opt in
	c)	COLOR=-c; GSDEV=-sDEVICE=bitcmyk;;
	d)	DUPLEX="$OPTARG";;
	m)	MEDIA="$OPTARG";;
	n)	COPIES="$OPTARG";;
	p)	PAPER="$OPTARG";;
	r)	RES="$OPTARG";;
	s)	SOURCE="$OPTARG";;
	B)	BC=-B;;
	D)	DEBUG="$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 "$PAPER" in
1)	paper=letter;    XDIM="10200"; YDIM="6600";;
5)	paper=legal;     XDIM="10200"; YDIM="8400";;
7)	paper=executive; XDIM="8700";  YDIM="6300";;
9)	paper=a4;        XDIM="9920";  YDIM="7015";;
11)	paper=a5;        XDIM="6992";  YDIM="4960";;
13)	paper=b5;        XDIM="8598";  YDIM="6070";;
*)	error "Illegal paper code $PAPER";;
esac
PAPERSIZE="-sPAPERSIZE=$paper";

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

case $NUP in
2)	PREFILTER="nup2";;
4)	PREFILTER="nup4";;
*)	PREFILTER=cat;;
esac

#
#	Main Program
#
GS="gs -q -dBATCH -dSAFER -dQUIET -dNOPAUSE"

$PREFILTER \
| $GS $PAPERSIZE -g$DIM -r$RES $GSDEV -sOutputFile=- - \
| foo2zjs -r$RES -g$DIM -p$PAPER -m$MEDIA -n$COPIES -d$DUPLEX $BC $AIB
