/usr/bin/simpleprinterfilter2


#!/bin/sh

# A very simple printer filter used by LPRng.
# PostScript/PDF/Text -> Epson Stylus Color 600, 1440x720dpi, Inkjet Paper
# This script was written by Stephan Seidl, no copyright is claimed.
# It is offered as-is, without any warranty.
# This script is in the public domain; do with it what you wish.

#set -x

PATH="/bin:/usr/bin:${PATH}"
export PATH

basename=`basename $0`

tempfile1=`mktemp -q /tmp/spf21XXXXXXX` || exit 1
tempfile2=`mktemp -q /tmp/spf22XXXXXXX` || exit 1

trap "rm -f ${tempfile1} ${tempfile2} ${tempfile1}~ ${tempfile2}~" EXIT SIGHUP SIGINT SIGQUIT SIGTERM

cat > ${tempfile1}

u0=""
u1=""
h0=""
h1=""
f0=""
f1=""
z0=""
for word in ${@} ; do
  case "${word}" in
    -L?* ) u0=`echo ${word} | sed 's,^-L,,'` ;;
    -n?* ) u1=`echo ${word} | sed 's,^-n,,'` ;;
    -H?* ) h0=`echo ${word} | sed 's,^-H,,'` ;;
    -h?* ) h1=`echo ${word} | sed 's,^-h,,'` ;;
    -J?* ) f0=`echo ${word} | sed 's,^-J,,'` ;;
    -f?* ) f1=`echo ${word} | sed 's,^-f,,'` ;;
    -Z?* ) z0=`echo ${word} | sed 's,^-Z,,'` ;;
    esac
  done
test -z "${u0}" && u0="${u1}"
test -z "${h0}" && h0="${h1}"
test -z "${f0}" && f0="${f1}"
test -z "${f0}" && f0="(stdin)"

while :; do
  case "`file -b ${tempfile1}    \
           | tr ',' ' '          \
           | sed -e 's,   *, ,g' \
                 -e 's,^ ,,'     \
                 -e 's, $,,'` @" in
    gzip\ compressed\ data\ * )
      # Uncompress and continue.
      gzip -dc ${tempfile1} > ${tempfile2} 2> /dev/null
      aux="${tempfile1}"
      tempfile1="${tempfile2}"
      tempfile2="${aux}"
      continue
      ;;
    PostScript\ document\ text\ * )
      # Done, so far.
      aux="${tempfile1}"
      tempfile1="${tempfile2}"
      tempfile2="${aux}"
      break
      ;;
    PDF\ document\ version\ * )
      if [ -z "${action}" ]; then
        # The conversion has to be done here.
        cmd=""
        cmd="${cmd}gs "
        cmd="${cmd}-q "
        cmd="${cmd}-sDEVICE=ps2write "
        cmd="${cmd}-sOutputFile=${tempfile2} "
        cmd="${cmd}-sPAPERSIZE=a4 "
        cmd="${cmd}-dNOPLATFONTS -dNOPAUSE -dQUIET -dBATCH -dSAFER -dFIXEDMEDIA "
        cmd="${cmd}-c save pop "
        cmd="${cmd}-f ${tempfile1} "
        cmd="${cmd}2> /dev/null "
        eval "${cmd}"
        aux="${tempfile1}"
        tempfile1="${tempfile2}"
        tempfile2="${aux}"
        fi
      # Done, so far.
      aux="${tempfile1}"
      tempfile1="${tempfile2}"
      tempfile2="${aux}"
      break
      ;;
    UTF-8\ Unicode\ * )
      # We can only hope that the document is not Chinese.
      iconv --from-code=UTF-8 --to-code=ISO-8859-1//TRANSLIT "${tempfile1}" > "${tempfile2}"
      aux="${tempfile1}"
      tempfile1="${tempfile2}"
      tempfile2="${aux}"
      continue
      ;;
    ASCII\ *                   | \
    ISO-8859\ text\ *          | \
    ISO-8859\ English\ text\ * | \
    *\ shell\ script\ text\ executable\ * )
      linelength=`wc -L < ${tempfile1}`
      linelength=`expr ${linelength} + 1`
      test ${linelength} -lt  80 && linelength=80  || :
      test ${linelength} -gt 218 && linelength=218 || :
      cmd=""
      cmd="${cmd}a2ps "
      cmd="${cmd}-1 "
      cmd="${cmd}--output=${tempfile2} "
      cmd="${cmd}--medium=A4 "
      cmd="${cmd}--chars-per-line=${linelength} "
      cmd="${cmd}--delegate=no "
      cmd="${cmd}--pretty-print=plain "
#     cmd="${cmd}--no-header "
#     cmd="${cmd}--header=\"Printed by ${u0} at ${h0}\" "
      cmd="${cmd}--header=\"\" "
      cmd="${cmd}--left-title=\"`date '+%a, %d %b %Y'`\" "
      cmd="${cmd}--center-title=\"${f0}\" "
      cmd="${cmd}--left-footer=\"\" "
      cmd="${cmd}--right-footer=\"\" "
      cmd="${cmd}< ${tempfile1} "
      cmd="${cmd}2> /dev/null "
      eval "${cmd}"
      break
      ;;
    * )
      # Unsupported output from `file'.
      filter=`dirname "${0}"`
      filter=`cd "${filter}" && pwd`
      filter="${filter}/${basename}"
      txt=""
      txt="${txt}Error in ${filter}:;"
      txt="${txt}\`file -b \${inputfile}\` yielded;"
      txt="${txt}\"`file -b ${tempfile1}`\",;"
      txt="${txt}not being supported.;"
      cmd=""
      cmd="${cmd}echo \"\${txt}\" "
      cmd="${cmd}| tr ';' '\012' "
      cmd="${cmd}| a2ps "
      cmd="${cmd}-1 "
      cmd="${cmd}--output=${tempfile2} "
      cmd="${cmd}--medium=A4 "
      cmd="${cmd}--chars-per-line=80 "
      cmd="${cmd}--delegate=no "
      cmd="${cmd}--pretty-print=plain "
#     cmd="${cmd}--no-header "
#     cmd="${cmd}--header=\"Printed by ${u0} at ${h0}\" "
      cmd="${cmd}--header=\"\" "
      cmd="${cmd}--left-title=\"`date '+%a, %d %b %Y'`\" "
      cmd="${cmd}--center-title=\"${f0}\" "
      cmd="${cmd}--left-footer=\"\" "
      cmd="${cmd}--right-footer=\"\" "
      cmd="${cmd}2> /dev/null "
      eval "${cmd}"
      break
      ;;
    esac
  done

aux="${tempfile1}"
tempfile1="${tempfile2}"
tempfile2="${aux}"

cmd=""
cmd="${cmd}gs "
cmd="${cmd}-q "
cmd="${cmd}-dNOPAUSE "
cmd="${cmd}-dSAFER "
cmd="${cmd}-dPARANOIDSAFER "
cmd="${cmd}-sOutputFile=${tempfile2} "
cmd="${cmd}-sPAPERSIZE=a4 "
cmd="${cmd}@stc600ih.upp "
cmd="${cmd}${tempfile1} "
cmd="${cmd}< /dev/null "
cmd="${cmd}> /dev/null "
cmd="${cmd}2> /dev/null "
eval "${cmd}"
aux="${tempfile1}"
tempfile1="${tempfile2}"
tempfile2="${aux}"

cat ${tempfile1}
#cat ${tempfile1} | `dirname $0`/simpleprinterfilterdebug "${@}"

rm -f ${tempfile1} ${tempfile2} ${tempfile1}~ ${tempfile2}~

exit 0


Stephan K.H. Seidl