#!/bin/sh
#
# convert ps to the format required by the printer on this queue
#
# if the printer is a PostScript printer, just cat it through
# if the printer uses ghostscript, we'll run it now
# if the printer is neither, we die (with good message to someone)
#

#
# read in PostScript configuration settings
#
. ${SPOOLDIR}/postscript.cfg

#
# see if we should reverse order
#
# support will be added for this in the future
# psorder needed and is part of netatalk, which we dont currently ship
#

if [ "$PAPERSIZE" = "letter" ]; then
    mpage_paper="Letter"
elif [ "$PAPERSIZE" = "a4" ]; then
    mpage_paper="A4"
elif [ "$PAPERSIZE" = "legal" ]; then
    mpage_paper="Legal"
else
    mpage_paper="Letter"
fi

#
# weird case - some PS doesnt get handled by mpage well
# so we allow nup=1 to just cat PS to printer w/o mpage interferring
#
if [ "$NUP" = "1" ]; then
    border="-o"
    mpage_cmd="cat -"
else
    border=""
    mpage_cmd="mpage -b$mpage_paper $border -$NUP -m${RTLFTMAR}lr -m${TOPBOTMAR}tb"
fi

#
# if the driver is "POSTSCRIPT" it means the printer handles Postscript
# natively, no need to run gs.
# if the driver is "TEXT" then it means the printer cannot handle PS input
#
if [ "$GSDEVICE" = "POSTSCRIPT" ]; then
   eval $mpage_cmd
elif [ "$GSDEVICE" = "TEXT" ]; then
   echo "Error - ps-to-printer.fpi - this printer cannot print postscript"
   echo "                            and ghostscript does not have support"
   exit 1
else
#  we're using ghostscript
#  stc740p.upp    740*740  mine
#  stc740pl.upp   360*360  mine
#  stc740_m.upp   740*740  from the net

    if [ "$GSDEVICE" = "uniprint" ]; then
        if [ "$RESOLUTION" = "720x720" ]; then
                configfile="stc740p.upp"
        elif [ "$RESOLUTION" = "1440x720" ]; then
                configfile="stc740ih.upp"
        else
                configfile="stc740pl.upp"
        fi
        eval "$mpage_cmd | gs -q @$configfile \
        -sPAPERSIZE=$PAPERSIZE \
        -sOutputFile=- \
        $EXTRA_GS_OPTIONS \
        -"
    else
        eval "$mpage_cmd | gs -q -sDEVICE=$GSDEVICE \
           -r$RESOLUTION \
           -sPAPERSIZE=$PAPERSIZE \
           -dNOPAUSE \
           -dSAFER \
           -sOutputFile=- \
           $COLOR \
           $EXTRA_GS_OPTIONS \
           -"
    fi
fi

#
#
#   see if we need to send a form feed to eject the page from printer
#
    if [ "$PS_SEND_EOF" = "YES" ]; then
      printf "\004"
    fi

exit 0
