#! /bin/bash
#
# pstoturboprint - CUPS spooler filter for turboprint driver system
#
# stdin = postscript
# conversion to bitmap image data using ghostscript
# conversion to printer data using tpprint
#
# Copyright 2002 ZEDOnet
# Version 1.85
# Created 2.10.2002 Florian Zeiler <zeiler@turboprint.de>
# 07.04.2003 process substitution >(...) removed (problems with bash 2.05)
# 08.05.2003 set PATH variable (gs sometimes not found)
# 22.10.2003 named pipe between ghostscript and tpfilter (fixes problem with
#            ghostscript writing messages to stdout even if -q set)
# 07.04.2005 extract parameters, then call rastertoturboprint
PATH=/bin:/usr/bin:/usr/local/bin
PATH=${PATH}:/usr/bin/TeX:/usr/X11R6/bin
PATH=${PATH}:/sbin:/usr/sbin
#export PATH

########
# get Turboprint paths
########
eval $(cat "/etc/turboprint/system.cfg")
LOGFILE="$TPPATH_LOG/turboprint_cups.log"
TPSTDIN="$TPPATH_FILTERS/tpstdin"
TPPRINT="$TPPATH_BIN/tpprint"

########
# write some info to logfile
########
echo >> $LOGFILE || LOGFILE="/dev/null"
echo "#######################################################" >> $LOGFILE
echo "New print job $(date) (pstoturboprint 1.92)" >> $LOGFILE
echo "job-id $1" >> $LOGFILE
echo "user $2" >> $LOGFILE
echo "title $3" >> $LOGFILE
echo "copies $4" >> $LOGFILE
echo "options $5" >> $LOGFILE
echo "file $6" >> $LOGFILE

########
# read a chunk from the postscript file to evaluate ppd-style settings
########
# this file holds the first chunk (up to 64K) of stdin
# search for %%EndSetup up to 1MB max. chunk size
CHUNKFILE="$TPPATH_TEMP/pstoturboprint$$.chunk"
VARFILE="$TPPATH_TEMP/pstoturboprint$$.var"
FIFOFILE="$TPPATH_TEMP/pstoturboprint$$.fifo"
$TPSTDIN --pscut $CHUNKFILE >> $LOGFILE

# settings that are necessary for ghostscript are extracted
echo "$TPPRINT -v2 -l$LOGFILE --ppdfile=$PPD --psfeatures $CHUNKFILE $VARFILE" >> $LOGFILE
$TPPRINT -v2 -l$LOGFILE --ppdfile=$PPD --psfeatures $CHUNKFILE $VARFILE >> $LOGFILE
echo "----------- Start of var file -----------" >> $LOGFILE
cat $VARFILE >> $LOGFILE
echo "----------- End of var file -----------" >> $LOGFILE
eval $(cat $VARFILE)

########
# convert additional options to pass them to tpprint
########
# set the options parameter as the new command line
set -- $5
TPOPTION=""

for i ; do
	OPTION=${i%%=*}
	VALUE=${i##*=}
	if [ "$OPTION" = "turboprint" ] ; then
		TPOPTION="$TPOPTION -$VALUE"
	else
		TPOPTION="$TPOPTION ---$i"
	fi
done

########
# build command line for ghostscript
# -sDEVICE= name of output format
# -r resolution
# -g output image size in pixels
# -dSAFER interpret postscript in read-only mode
# -dNOPAUSE don't wait after page end
# -dBATCH don't wait after file end
# -sOutputFile=- write to stdout
# -q don't write debug output to stdout
# - instead of input file: read from stdin
########
#
case $GSCOLORMODE in
 0) GSDRIVER="pbmraw" ;;
 1) GSDRIVER="pgmraw" ;;
 2) GSDRIVER="pcx24b" ;;
 3) GSDRIVER="pcx256" ;;
 4) GSDRIVER="bmp32b" ;;
 *) GSDRIVER="pcx24b" ;;
esac

GSCOMMANDLINE="gs -sDEVICE=$GSDRIVER -r${GSXDPI}x$GSYDPI -g${GSWIDTH}x$GSHEIGHT\
 -dSAFER -dNOPAUSE -dBATCH"

########
# build command line for tpprint
#
# -a0    : ignore image aspect
# -e1    : ignore unprintable page margins
#          (image created by gs includes margins)
# -sp    : print size of the image in pixels
# -v2    : verbose level 2 = print all messages
# -l     : set name of logfile
# - -    : read image from stdin and send printer data to stdout
########
TPCOMMANDLINE="$TPPRINT -a0 -e1 -s${TPWIDTH}x$TPHEIGHT -v2 -l$LOGFILE $TPOPTION --ppdfile=$PPD --psheader=$CHUNKFILE"

########
# check if esp ghostscript > 7.07 is installed => use cups output format
########
GSRESULT=$(gs --help | grep Ghostscript)
GSVERSION=${GSRESULT%% Ghostscript*}
GSMAJOR=${GSRESULT%%.*}
GSMINOR=${GSRESULT#*.}
GSMINOR=${GSMINOR%%.*}
GSMINOR=${GSMINOR%% *}
GSMAJOR=${GSMAJOR##*[^0-9]}
GSMAJOR=${GSMAJOR%%[^0-9]*}
GSMINOR=${GSMINOR##*[^0-9]}
GSMINOR=${GSMINOR%%[^0-9]*}
GSMAJOR=$[ $GSMAJOR + 0 ]
GSMINOR=$[ $GSMINOR + 0 ]
echo "GSRESULT=$GSRESULT GSVERSION=$GSVERSION GSMAJOR=$GSMAJOR GSMINOR=$GSMINOR" >> $LOGFILE
if [ "$GSVERSION" = "ESP" -a $GSMAJOR -ge 7 -a $GSMINOR -ge 7 ] ; then
	GSCOMMANDLINE="gs -sDEVICE=cups -r${GSXDPI}x$GSYDPI -g${GSWIDTH}x$GSHEIGHT -dNOMEDIAATTRS -dSAFER -dNOPAUSE -dBATCH"
	eval "set -- $ZEDOPARM"
	for i ; do
        	TPOPTION="$TPOPTION ---$i"
	done
	TPCOMMANDLINE="$TPPRINT --cups -v2 -l$LOGFILE --ppdfile=$PPD --psheader=$PPD $TPOPTION"
fi



# put together first chunk and remainder of stdin
INPUTFILTER="$TPSTDIN --paste $CHUNKFILE"

# put send output of ghostscript to tpprint
COMPLETEPIPE="$INPUTFILTER | $GSCOMMANDLINE  -sOutputFile=$FIFOFILE - >> $LOGFILE"

########
# now execute ghostscript & other pipe commands
########
echo "GSCOMMANDLINE=$GSCOMMANDLINE" >> $LOGFILE
echo "TPCOMMANDLINE=$TPCOMMANDLINE" >> $LOGFILE
echo "COMPLETEPIPE=$COMPLETEPIPE" >> $LOGFILE
echo "----------- Start of print job -----------" >> $LOGFILE
mkfifo $FIFOFILE >> $LOGFILE
$TPCOMMANDLINE $FIFOFILE - &
eval $COMPLETEPIPE
echo "------------ End of print job ------------" >> $LOGFILE

########
# remove temp files
########
rm $CHUNKFILE >> $LOGFILE
rm $VARFILE >> $LOGFILE
rm $FIFOFILE >> $LOGFILE

########
# wrap around log file
########
$TPSTDIN --cutlogfile $LOGFILE 128
########
# send remaining data from stdin to null device
########
cat > /dev/null
