/usr/bin/simpleprinterfilter1


#!/bin/sh

# A very simple printer filter used by LPRng.
# PostScript/PDF/PCL/PJL/Text -> HP LaserJet Printers
# 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/spf11XXXXXXX` || exit 1
tempfile2=`mktemp -q /tmp/spf12XXXXXXX` || 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)"

duplex="1"
zaux="${z0}"
while :; do
  test -z "${zaux}" && break || :
  echo "${zaux}" | grep ',' >/dev/null && : || zaux="${zaux},"
  zhdr=`echo "${zaux}" | cut -d',' -f1`
  zaux=`echo "${zaux}" | sed 's@^ *'"${zhdr}"' *,@@'`
  echo "${zhdr}" | grep '^ *simplex *$' >/dev/null && duplex="0" || :
  echo "${zhdr}" | grep  '^ *duplex *$' >/dev/null && duplex="1" || :
  done
unset zhdr zaux

printers=`echo "|${PRINTCAP_ENTRY}:" \
  | tr '\012' ' '                    \
  | sed -e 's,:.*$,|,'               \
        -e 's,|  *,|,g'              \
        -e 's,  *|,|,g'`

fastPrint=0
###echo "${printers}" | grep '|fp[0-9]|' >/dev/null && fastPrint=1 || :

maxPsLevel=3
###echo "${printers}" | grep '|[a-z]p1|' >/dev/null && maxPsLevel=2 || :

action=""
test ${maxPsLevel} -lt 3 && action="convertToPostScriptLevel2" || :
test ${fastPrint}  -eq 1 && action="convertToPcl"              || :

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\ conforming\ at\ level\ 3.0\ * | \
    PostScript\ document\ text\ conforming\ DSC\ level\ 3.0\ * )
      # Done, so far.
      aux="${tempfile1}"
      tempfile1="${tempfile2}"
      tempfile2="${aux}"
      break
      ;;
    PostScript\ document\ text\ conforming\ at\ level\ 2.0\ *  | \
    PostScript\ document\ text\ conforming\ DSC\ level\ 2.0\ * | \
    PostScript\ document\ text\ conforming\ at\ level\ 1.0\ *  | \
    PostScript\ document\ text\ conforming\ DSC\ level\ 1.0\ * | \
    PostScript\ document\ text\ * )
      if [ "${action}" = "convertToPostScriptLevel2" ]; then
        # Do not touch the document.
        action=""
        fi
      # 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
      ;;
    HP\ PCL\ printer\ data\ * )
      # Simply hand over.
      aux="${tempfile1}"
      tempfile1="${tempfile2}"
      tempfile2="${aux}"
      break
      ;;
    PJL\ encapsulated\ PostScript\ document\ text\ * | \
    HP\ Printer\ Job\ Language\ data\ * )
      # Somebody knows what he is doing, pass through.
      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}"

case "`file -b ${tempfile1}    \
         | tr ',' ' '          \
         | sed -e 's,   *, ,g' \
               -e 's,^ ,,'     \
               -e 's, $,,'` @" in
  PostScript\ document\ text\ * )
    if [ "${action}" = "convertToPostScriptLevel2" ]; then
      cmd=""
      cmd="${cmd}gs "
      cmd="${cmd}-q "
      cmd="${cmd}-sDEVICE=ps2write "
      cmd="${cmd}-sOutputFile=${tempfile2} "
      cmd="${cmd}-sPAPERSIZE=a4 "
      cmd="${cmd}-r600x600 "
      cmd="${cmd}-dNOPLATFONTS -dNOPAUSE -dQUIET -dBATCH -dSAFER -dFIXEDMEDIA "
      cmd="${cmd}-dLanguageLevel=2 "
      cmd="${cmd}-c save pop "
      cmd="${cmd}-f ${tempfile1} "
      cmd="${cmd}2> /dev/null "
      eval "${cmd}"
      aux="${tempfile1}"
      tempfile1="${tempfile2}"
      tempfile2="${aux}"
      fi
    break
    ;;
  * )
    break
    ;;
  esac

if [ "${duplex}" = "1" ]
  then
    sed -e 's,^%%BeginFeature: *\*Duplex  *.*\r*$,%%BeginFeature: *Duplex\r,'                      \
        -e 's,^ *<< */Duplex  *false *>> *setpagedevice *\r*$,<< /Duplex true >> setpagedevice\r,' \
      < ${tempfile1} > ${tempfile2}
  else
    sed -e 's,^%%BeginFeature: *\*Duplex  *.*\r*$,%%BeginFeature: *Duplex\r,'                      \
        -e 's,^ *<< */Duplex  *true *>> *setpagedevice *\r*$,<< /Duplex false >> setpagedevice\r,' \
      < ${tempfile1} > ${tempfile2}
  fi
aux="${tempfile1}"
tempfile1="${tempfile2}"
tempfile2="${aux}"

case "`file -b ${tempfile1}    \
         | tr ',' ' '          \
         | sed -e 's,   *, ,g' \
               -e 's,^ ,,'     \
               -e 's, $,,'` @" in
  PostScript\ document\ text\ * )
    if [ "${action}" = "convertToPcl" ]; then
      cmd=""
      cmd="${cmd}gs "
      cmd="${cmd}-q "
      if [ "${duplex}" = "1" ]
        then cmd="${cmd}-sDEVICE=ljet4d "
        else cmd="${cmd}-sDEVICE=ljet4 "
        fi
      cmd="${cmd}-sOutputFile=${tempfile2} "
      cmd="${cmd}-sPAPERSIZE=a4 "
      cmd="${cmd}-r600x600 "
      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
    break
    ;;
  * )
    break
    ;;
  esac

case "`file -b ${tempfile1}    \
         | tr ',' ' '          \
         | sed -e 's,   *, ,g' \
               -e 's,^ ,,'     \
               -e 's, $,,'` @" in
  PostScript\ document\ text\ * )
    echo -n "e%-12345X@PJL JOBrn" | tr 'ern' '\033\015\012' > ${tempfile2}
    echo -n "@PJL SET RESOLUTION = 600rn" | tr 'rn' '\015\012' >> ${tempfile2}
    echo -n "@PJL ENTER LANGUAGE = POSTSCRIPTrn" | tr 'rn' '\015\012' >> ${tempfile2}
    cat ${tempfile1} >> ${tempfile2}
    echo -n "d" | tr 'd' '\004' >> ${tempfile2}
    echo -n "e%-12345X@PJL EOJrn" | tr 'ern' '\033\015\012' >> ${tempfile2}
    echo -n "e%-12345X" | tr 'e' '\033' >> ${tempfile2}
    aux="${tempfile1}"
    tempfile1="${tempfile2}"
    tempfile2="${aux}"
    break
    ;;
  * )
    break
    ;;
  esac

# Wake the printer up. Output 4096 escape characters.
string="e"
for i in 1 2 3 4 5 6 7 8 9 10 11 12 ; do
  string="${string}${string}"
  done
echo -n "${string}" | tr 'e' '\033' > ${tempfile2}
# Output 128 printer resets.
string="eE"
for i in 1 2 3 4 5 6 7 ; do
  string="${string}${string}"
  done
echo -n "${string}" | tr 'e' '\033' >> ${tempfile2}
# Create a Universal Exit Language (UEL) command prototype.
# The UEL command is a special PJL command and
# it is recognized by PCL 5 and HP-GL/2, too.
uel="e%-12345X"
# Create an empty PJL command prototype.
emptypjl="@PJLrn"
# Concatenate them.
string="${uel}${emptypjl}"
# Chain up 128 of them.
for i in 1 2 3 4 5 6 7 ; do
  string="${string}${string}"
  done
# Print out the UEL/PJL command chain.
echo -n "${string}" | tr 'ern' '\033\015\012' >> ${tempfile2}
cat ${tempfile1} >> ${tempfile2}
# Again print out the UEL/PJL command chain.
echo -n "${string}" | tr 'ern' '\033\015\012' >> ${tempfile2}
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