printcap configuration problem (if-MAPS 2/4)

Martin Alejandro Paredes Sanchez mapsware at prodigy.net.mx
Sun Aug 24 23:58:52 UTC 2014


Create the file

/usr/local/bin/if-MAPS

In its content, put the following

#!/bin/sh
#
#  This shell espect to be a text filter (filt.input=if capability in
#  /etc/printcap) confusingly called the input filter.
#
#  When LPD starts this filter program. It sets:
#
#   standard input to the file to print
#   standard output to the printer device (tty.device=lp capability)
#   standard error append to the error logging file (spool.log=lf capability)
#   current directory to the spooling directory (spool.dir=sd capability)
#   uid=1(daemon) gid=1(daemon)
#
#  With the following parameters:
#
#  filter-name [-c] -wWidth -lLength -iIndent -n login -h host acct-file
#
#  -c          appears if job submitted with "lpr -l" (print control
#              characters and suppresses page breaks) (raw printing)
#  width       is the page width (page.width=pw capability), default 132
#  length      is the page length (page.length=pl capability), default 66
#  indent      is the amount of the indentation from "lpr -i", default 0
#  login       is the account name of the user printing the file
#  host        is the host name from which the job was submitted
#  acct-file   is the name of the accounting file (acct.file=af capability)
#
#  This filter should exit with the following exit status:
#
#   exit 0     If the filter printed the file successfully.
#   exit 1     If the filter failed to print the file but wants LPD to try
#              to print the file again. LPD will restart a filter if it
#              exits with this status.
#   exit 2     If the filter failed to print the file and does not want LPD
#              to try again. LPD will throw out the file.
#
#  You need the following packages installed
#
#    GhostScript	ghostscript-gpl		GPL Postscript interpreter
#    psUtils		psutils-letter		Utilities for manipulating PostScript documents
#    Enscript		enscript-letter		ASCII to PostScript filter

#
#  If user removes the job, LPD will send SIGINT (SIGNAL INTERRUPT)
#  so trap SIGINT (and a few other signals) to clean up after ourselves.
#
#     1       HUP (hang up)
#     2       INT (interrupt)
#     15      TERM (software termination signal)
#
trap 'LogError 2 "Signal HUP traped"'  1
trap 'LogError 2 "Signal INT traped"'  2
trap 'LogError 2 "Signal TERM traped"' 15

umask -S u=rwx,g=rwx,o=
FilterName=`/usr/bin/basename "$0"`
PATH=/bin:/usr/bin:/usr/local/bin
TMPDIR=`/usr/bin/mktemp -q -d "/var/tmp/$FilterName.XXXXXX"` || ErrorTmpDir
export TEMP="$TMPDIR" TMPDIR PATH
[ -n "$TMPDIR" -a -d "$TMPDIR" ] && /usr/bin/chgrp daemon "$TMPDIR"


#
# SendMailLog: Send the logs by mail (sendmail).
#
SendMailLog() {
   LogTrace "Sending logs by mail (this mail)"
   {
     echo "To: $User@$Host"
     [ -e "$TMPDIR/ErrorFile" ] && echo "Cc: root"
     echo "Subject: Logged information of print job \"$JobFile\""
     echo
     echo "Queue = \"$Queue\""
     echo "File = \"$JobFile\""
     echo
     echo "Your print job was processed by the input-filter \"$FilterName\""
     if [ -e "$TMPDIR/ErrorFile" ]; then
        echo "During the process, an error occurred"
        echo
        echo "The logged error is:"
        /bin/cat "$TMPDIR/ErrorFile"
     else
        echo "and was sent to the printer device successfully"
     fi
     if [ -s "$TMPDIR/DebugFile" ]; then
        echo
        echo "The trace/debug information is:"
        echo
        /bin/cat "$TMPDIR/DebugFile"
     fi
     echo
     echo "This e-mail was sent by a program, do not respond to this message."
     echo "In case of an error, contact your system administrator."
   } | /usr/sbin/sendmail -oem -t
}


#
# CleanUp: Send the logs to StdError and remove temporary files.
#
CleanUp() {
   [ $LogLevel -ge 1 -a -e "$TMPDIR/ErrorFile" ] && /bin/cat "$TMPDIR/ErrorFile" 1>&2
   [ $LogLevel -ge 2 ] && /bin/cat "$TMPDIR/DebugFile" 1>&2
   if ValueYes MailLog ; then
      SendMailLog
   fi
   [ -n "$TMPDIR" -a -d "$TMPDIR" ] && /bin/rm -rf "$TMPDIR"
}


#
# LogInfo: Log the information message to the file "$TMPDIR/DebugFile"
#
#	$1: Message to log
#
LogInfo() {
   echo -e "$1" >> "$TMPDIR/DebugFile"
}


#
# LogTrace: Log the trace information to the file "$TMPDIR/DebugFile"
#
#	$1: Trace information to log
#
LogTrace() {
   if [ $LogLevel -ge 3 ] ; then
      echo -e "$1" >> "$TMPDIR/DebugFile"
   fi
}


#
# LogError: Log the message error to the file "$TMPDIR/ErrorFile"
#
#	$1: Exit value to use
#	$2: Error message to log
#
LogError() {
   local Header

   /usr/bin/touch "$TMPDIR/ErrorFile"
   Header="`/bin/date -v-7d '+%Y/%m/%d %H:%M:%S'` $FilterName[$$]"
   echo -e "$Header: Error printing '$JobFile', exit code $1" >> "$TMPDIR/ErrorFile"
   echo -e "$Header: $2" >> "$TMPDIR/ErrorFile"
   CleanUp
   exit $1
}


#
# ErrorRunning: An error ocurred while transforming the print job
#
ErrorRunning() {
   LogError 2 "`/bin/cat "$TMPDIR/Error"`"
}


#
# ErrorTmpDir: An error ocurred while trying to create a temporary directory
#
ErrorTmpDir() {
   ProcessControlFile
   LogError 1 "mktemp: Can't create temporary directory"
}


#
# ErrorFileType: The file of the print job, is of a type unsupported
#
ErrorFileType() {
   LogError 2  "Unsupported file type '$FileType'"
}


#
# ErrorNotConfFile: The Queue/printer does not has a configuration file
#
ErrorNotConfFile() {
   LogError 2  "Configuration file not found '$FilterName.conf'"
}


#
# ValueYes: Test a variable and inform if set to YES or NO.
#
#	$1: Variable to be tested
#
ValueYes() {
   local Value

   eval Value=\$${1}
   LogTrace "\tValueYes: $1 is set to '$Value'."
   case $Value in
      yes|true|on)   return 0 ;;
      no|false|off)  return 1 ;;
      *)
         LogInfo "${1} is not set to YES or NO. Assuming ${1}='yes'"
         return 0 ;;
   esac
}


#
# Process the control file (grab variables JobFile, ZOptions).
#
ProcessControlFile() {
   local PId  ControlFile  Line  Value

   {
    read PId
    read -r ControlFile
   } < lock

   Copies=0
   while read -r Line; do
     Value="${Line#?}"
     case "$Line" in
         Z*) [ -n "$TMPDIR" -a -d "$TMPDIR" ] && echo -n "$Value" > "$TMPDIR/-Z Options"  ;;
         M*) MailLog=yes  ;;
         N*) JobFile="$Value" ; : ${JobFile:=StdIn}  ;;
         f*) Copies=`/bin/expr $Copies + 1` ;;
         0*) LogLevel=0 ; MailLog=no ; /bin/cat - > /dev/null ; CleanUp ; exit 0 ;;
     esac
   done < $ControlFile
   if [ $Copies -ge 2 ] && ValueYes CopiesManual; then
      echo "0CopiesManuallyPrinted" >> $ControlFile
   fi
}


#
# ProcessOptionsFile: Validate and include options coming from a file.
#
#	$1: File with options to validate and include
#
ProcessOptionsFile() {
   local Option CmdOption Var Val ValidOption WidthInches HeightInches

   if [ -r "$1" ]; then
      Val=`/usr/bin/basename "$1"`
      LogTrace "Running 'ProcessOptionsFile' file '$Val'"
      /usr/bin/grep --invert-match --regexp='^#' --regexp='^$' "$1" | /usr/bin/tr ", " "\n" | /usr/bin/tr -Cd "[:alnum:]=\n." > "$TMPDIR/Options"

      while read -r CmdOption; do
        Option=`echo "$CmdOption"| /usr/bin/tr "[:upper:]" "[:lower:]"`
        case "$Option" in
            custom.*|papersize=custom.*)
               ValidOption="PaperSize=custom"
               Val=${Option##papersize=}
               Val=${Val##custom.}
               case "$Val" in
                    *x*in) Val=${Val%*in} ; Var=1     ;;
                    *x*mm) Val=${Val%*mm} ; Var=25.4  ;;
                    *x*) Var=72  ;;
                    *)   ValidOption='' ;;
               esac
               if [ -n "$ValidOption" ]; then
                  WidthInches=`echo  -n "$Val" | /usr/bin/cut -d 'x' -f 1`
                  HeightInches=`echo -n "$Val" | /usr/bin/cut -d 'x' -f 2`
                  WidthInches=`echo "scale=2; $WidthInches / $Var" | /usr/bin/bc`
                  HeightInches=`echo "scale=2; $HeightInches / $Var" | /usr/bin/bc`
                  if [ "${WidthInches}" = "0" -o "${HeightInches}" = "0" ] ; then
                     ValidOption=''
                  else
                     PaperSizeInches="${WidthInches}x${HeightInches}"
                  fi
               fi ;;
            *=*)
               Var=`echo -n "$Option" | /usr/bin/cut -d '=' -f 1`
               Val=`echo -n "$Option" | /usr/bin/cut -d '=' -f 2`
               if [ "${Val##*[!0-9]*}" ] ; then
                  case "$Var" in
                     book)    ValidOption="Book=$Val"   ;;
                     nup)     ValidOption="nUp=$Val"    ;;
                     *)       ValidOption=''            ;;
                  esac
               else
                  ValidOption=`/usr/bin/nawk -F: -v Var="$Var" -v Val=",$Val," 'tolower($1) == Var && $2 ~ Val {print $1"="$3}' $FilterName.MappingOptions`
               fi
               ;;
            *) Var=
               Val="$Option"
               ValidOption=`/usr/bin/nawk -F: -v Val=",$Val," '$2 ~ Val {print $1"="$3}' $FilterName.MappingOptions`
               ;;
        esac
        if [ -n "$ValidOption" ]; then
           eval $ValidOption
           LogTrace "\t'$CmdOption'\t==> '$ValidOption'"
        else
           LogInfo "\t'$CmdOption'\tIgnored"
        fi
      done < "$TMPDIR/Options"
   fi
}


#
# FixOptions: Initialize options with default values if not set
#             change/calculate others options depending on values of other options
#
FixOptions() {
   local WidthDPI HeightDPI WidthInches HeightInches WidthPixels HeightPixels WidthPoints HeightPoints

   LogTrace "Running 'FixOptions'"
   WidthDPI=`echo  -n "$Resolution" | /usr/bin/cut -d 'x' -f 1`
   HeightDPI=`echo -n "$Resolution" | /usr/bin/cut -d 'x' -f 2`

   [ -z "$PaperSizeInches" ] && PaperSizeInches=`/usr/bin/nawk -F: -v Opt=",$PaperSize," '$2 ~ Opt {print $4}' $FilterName.MappingOptions`
   WidthInches=`echo  -n "$PaperSizeInches" | /usr/bin/cut -d 'x' -f 1`
   HeightInches=`echo -n "$PaperSizeInches" | /usr/bin/cut -d 'x' -f 2`

   WidthPixels=`echo  "$WidthDPI  * $WidthInches"  | /usr/bin/bc | /usr/bin/cut -d '.' -f 1`
   HeightPixels=`echo "$HeightDPI * $HeightInches" | /usr/bin/bc | /usr/bin/cut -d '.' -f 1`
   PaperSizePixels="${WidthPixels}x${HeightPixels}"

   # PostScript points (each point is 1/72 inch or 0.35mm)
   WidthPoints=`echo  "72 * $WidthInches"  | /usr/bin/bc | /usr/bin/cut -d '.' -f 1`
   HeightPoints=`echo "72 * $HeightInches" | /usr/bin/bc | /usr/bin/cut -d '.' -f 1`
   PaperSizePoints="${WidthPoints}x${HeightPoints}"

   case "$PaperSize" in
        env*)
           PaperType=envelope
           LogInfo "PaperType=$PaperType (Fixed because PaperSize=$PaperSize)" ;;
        *postcard*)
           PaperType=thick
           LogInfo "PaperType=$PaperType (Fixed because PaperSize=$PaperSize)" ;;
   esac

   # Fix option for Duplex
   : ${DuplexManual:=yes}
   : ${Duplex:=off}
   : ${Book:=off}

   # Fix the margin; expresed in Postscript Points
   : ${MarginLeft:=24}
   : ${MarginRight:=24}
   : ${MarginTop:=24}
   : ${MarginBottom:=24}

   # Fix option for nUp
   : ${nUp:=off}
   [ "${nUp}" = "1" ] && nUp=off
   : ${nUpLayout:=h}
   : ${nUpMargin:=off}
   if ValueYes nUpMargin ; then nUpMargin="-m`/bin/expr $MarginBottom - 2` -b2" ; else nUpMargin="-m$MarginBottom" ; fi
   : ${nUpBorder:=off}
   if ValueYes nUpBorder ; then nUpBorder="-d1" ; else nUpBorder="" ; fi

   # Fix option for Copies
   : ${CopiesManual:=yes}
   : ${Copies:=1}

   : ${FilterText:=enscript}
   : ${MailLog:=false}
   [ -z "$ResCPI" ] && ResCPI=`echo "scale=0; $Width / $WidthInches" | /usr/bin/bc`
   [ -z "$ResLPI" ] && ResLPI=`echo "scale=0; $Lenght / $HeightInches" | /usr/bin/bc`
}


#
#
#
DuplexNotification() {
   eval HOME=~$User
   export HOME DISPLAY=:0
   LogTrace "Duplex Notification 'kdialog'\n\tHOME=$HOME\n\tDISPLAY=$DISPLAY"
   sudo -E /usr/local/kde4/bin/kdialog --msgbox "You printed a file in duplex mode. The 1st part has been sent to the printer $Queue.\n\nWait for all pages to be ejected, then put them back into the printer.\n\nTo print the rest of the job, clic in the button" --title "Duplex Notification"
}


#
# SendToPrinter: Send the file to StdOut (attached to the printer device)
#
SendToPrinter() {
   local lnCopies

   lnCopies=$Copies
   if ValueYes CopiesManual ; then
      lnCopies=1
   fi
   while [ $lnCopies -le $Copies ] ; do
      LogTrace "Sendig to printer ($lnCopies)"
      /bin/cat "$1"
      lnCopies=`/bin/expr $lnCopies + 1`
   done
}


#
# PrintJob: 
#
PrintJob() {
   SendToPrinter "$TMPDIR/1stPart"
   if ValueYes Duplex && ValueYes DuplexManual ; then
      DuplexNotification
      SendToPrinter "$TMPDIR/2ndPart"
   fi
}


#
#
#
PreProcessingFile() {
   local OptTemp

   if ValueYes Book ; then
      Book=`/bin/expr $Book \* 4`
      LogTrace "Running 'psbook'\n\tpsbook -q -s$Book FileIn FileOut"
      /usr/local/bin/psbook -q -s$Book "$TMPDIR/FileIn" "$TMPDIR/FileOut" 2> "$TMPDIR/Error" || ErrorRunning
      /bin/mv "$TMPDIR/FileOut" "$TMPDIR/FileIn"
      nUp=2
      nUpMargin="-m`/bin/expr $MarginBottom - 27` -b27"  # inner magin 27 PostScript points (3/8" 9mm)
      nUpLayout=h
      Duplex=short
   fi
   if ValueYes nUp ; then
      case "$nUpLayout" in
         lrtb|h)   OptTemp=""       ;;    # Horizontal			Left to right, top to bottom (default)
         rltb|hr)  OptTemp="-r -c"  ;;    # Horizontal Reversed		Right to left, top to bottom
         tblr|v)   OptTemp="-c"     ;;    # Vertical			Top to bottom, left to right
         tbrl|vr)  OptTemp="-r"     ;;    # Vertical Reversed		Top to bottom, right to left
      esac
      LogTrace "Running 'psnup'\n\tpsnup $OptTemp $nUpMargin $nUpBorder -$nUp -q FileIn FileOut"
      /usr/local/bin/psnup $OptTemp $nUpMargin $nUpBorder -$nUp -q "$TMPDIR/FileIn" "$TMPDIR/FileOut" 2> "$TMPDIR/Error" || ErrorRunning
      /bin/mv "$TMPDIR/FileOut" "$TMPDIR/FileIn"
   fi
   case "$Duplex" in
      no|false|off)
         OptTemp="--simplex" ;;
      short)
         OptTemp="--tumble"  ;;
      long)
         OptTemp="--duplex"  ;;
   esac
   [ "$PaperTray" = "manual" ] && OptTemp="$OptTemp --manualfeed"
   LogTrace "Running 'psset'\n\tpsset --quiet --no-fix $OptTemp --output=FileOut FileIn"
   /usr/local/bin/psset --quiet --no-fix $OptTemp --output="$TMPDIR/FileOut" "$TMPDIR/FileIn" 2> "$TMPDIR/Error" || ErrorRunning
   /bin/mv "$TMPDIR/FileOut" "$TMPDIR/FileIn"

   JobPages=`/usr/local/bin/psselect -p_1 "$TMPDIR/FileIn" 2>&1 > /dev/null | /usr/bin/tr "[]" ":" | /usr/bin/cut -d ':' -f 2`
   LogTrace ">>>\tJobPages=$JobPages"

   if ValueYes DuplexManual && ValueYes Duplex ; then
      if [ `/bin/expr $JobPages % 2` -eq 1 ] ; then
         LogTrace "Running 'psselect (adding 1 page for even pages)'\n\tpsselect -q -p1-,_ FileIn FileOut"
         /usr/local/bin/psselect -q -p1-,_ "$TMPDIR/FileIn" "$TMPDIR/FileOut"
         /bin/mv "$TMPDIR/FileOut" "$TMPDIR/FileIn"
      fi
      /usr/local/bin/psselect -q -e -r "$TMPDIR/FileIn" "$TMPDIR/1stPart"
      /usr/local/bin/psselect -q -o    "$TMPDIR/FileIn" "$TMPDIR/2ndPart"
      if [ "$Duplex" = "long" ] ; then
         /bin/mv "$TMPDIR/1stPart" "$TMPDIR/FileIn"
         /usr/local/bin/pstops -q 'U(1w,1h)' "$TMPDIR/FileIn" "$TMPDIR/1stPart"
      fi
      /bin/rm "$TMPDIR/FileIn"
   else
      /bin/mv "$TMPDIR/FileIn" "$TMPDIR/1stPart"
   fi
}


#
#
#
PrintPostScript() {
   local lcFile

   PreProcessingFile
   if [ "$GS_DEVICE" != ps ] ; then
      GS_FONTPATH=/usr/local/share/ghostscript/fonts:/usr/local/lib/X11/fonts/Type1
      GS_LIB=
      # -g<width>x<height>  page size in pixels   | -r<res>  pixels/inch resolution
      GS_OPTIONS="$GS_OPTIONS -g$PaperSizePixels -r$Resolution -q -dBATCH -dSAFER -dNOPAUSE"
      #GS_OPTIONS="$GS_OPTIONS -sPAPERSIZE=$PaperSize -r$Resolution -q -dBATCH -dSAFER -dNOPAUSE"
      export GS_DEVICE GS_FONTPATH GS_LIB GS_OPTIONS
      LogTrace "Running 'gs'\n\tGS_DEVICE='$GS_DEVICE'\n\tGS_LIB='$GS_LIB'\n\tGS_FONTPATH='$GS_FONTPATH'\n\tGS_OPTIONS='$GS_OPTIONS'"
      for lcFile in "1stPart" "2ndPart" ; do
         if [ -f "$TMPDIR/$lcFile" ] ; then
            LogTrace "\tgs -sOutputFile=FileOut $lcFile"
            /usr/local/bin/gs -sOutputFile="$TMPDIR/FileOut" "$TMPDIR/$lcFile" 2> "$TMPDIR/Error" || ErrorRunning
            /bin/mv "$TMPDIR/FileOut" "$TMPDIR/$lcFile"
            type PostProcessingFile > /dev/null 2>&1 && PostProcessingFile "$lcFile"
         fi
      done
   fi
   PrintJob
}


#
#
#
PrintPDF() {
   LogTrace "Running 'pdf2ps'\n\tpdf2ps FileIn FileOut"
   /usr/local/bin/pdf2ps "$TMPDIR/FileIn" "$TMPDIR/FileOut" 2> "$TMPDIR/Error" || ErrorRunning
   /bin/mv "$TMPDIR/FileOut" "$TMPDIR/FileIn"
   PrintPostScript
}


#
#
#
PrintEnscript() {
   local WidthPoints HeightPoints

   WidthPoints=`echo -n "$PaperSizePoints" | /usr/bin/cut -d 'x' -f 1`
   HeightPoints=`echo -n "$PaperSizePoints" | /usr/bin/cut -d 'x' -f 2`
   HOME=$TMPDIR
   # enscript doesn't use HOME environment variable, uses the value from passwd(5)
   # daemon ==> /root
   # $HOME/.enscriptrc ==> /root/.enscriptrc
#   HOME=/root
   # Media definitions for enscript:
   #        name            width   height  llx     lly     urx     ury
   #Media:  letter          612     792     38      24      574     768
   echo -e "Media:\t$PaperSize\t$WidthPoints\t$HeightPoints\t$MarginLeft\t$MarginTop\t`/bin/expr $WidthPoints - $MarginRight`\t`/bin/expr $HeightPoints - $MarginBottom`" > "$HOME/.enscriptrc"
   LogTrace "Running 'enscript'\n\tAdding to \$HOME/.enscriptrc\n\tMedia:\t$PaperSize\t$WidthPoints\t$HeightPoints\t$MarginLeft\t$MarginTop\t`/bin/expr $WidthPoints - $MarginRight`\t`/bin/expr $HeightPoints - $MarginBottom`"
   PaperOrientationES="--$PaperOrientation"
   ColorModeES="--color=blackwhite"
   if [ "$ColorMode" = "color" ] ; then
      ColorModeES="--color=emacs"
   fi
   ENSCRIPT="--no-header --copies=1 --quiet --indent=${Indent}p --lines-per-page=$Lenght --media=$PaperSize $ColorModeES $PaperOrientationES"
   export HOME ENSCRIPT
   LogTrace "\tENSCRIPT='$ENSCRIPT'"

   LogTrace "\tenscript --output=FileOut FileIn"
   /usr/local/bin/enscript --output="$TMPDIR/FileOut" "$TMPDIR/FileIn" 2> "$TMPDIR/Error" || ErrorRunning
   /bin/mv "$TMPDIR/FileOut" "$TMPDIR/FileIn"
   PrintPostScript
}


#
#
#
PrintA2PS() {
   local WidthPoints HeightPoints

   WidthPoints=`echo -n "$PaperSizePoints" | /usr/bin/cut -d 'x' -f 1`
   HeightPoints=`echo -n "$PaperSizePoints" | /usr/bin/cut -d 'x' -f 2`
   # Media definitions for a2ps:
   #         name            width   height  llx     lly     urx     ury
   #Medium:  letter          612     792     38      24      574     768
   export HOME=$TMPDIR
   /bin/mkdir -p "$HOME/.a2ps"
   echo -e "Options:\t--quiet --columns=1 --rows=1 --major=rows --borders=off --no-header\nMedium:\t$PaperSize\t$WidthPoints\t$HeightPoints\t$MarginLeft\t$MarginTop\t`/bin/expr $WidthPoints - $MarginRight`\t`/bin/expr $HeightPoints - $MarginBottom`" > "$HOME/.a2ps/a2psrc"
   LogTrace "Running 'a2ps'\n\tAdding to \$HOME/.a2ps/a2psrc\n\tOptions:\t--quiet --columns=1 --rows=1 --major=rows --borders=off --no-header\n\tMedium:\t$PaperSize\t$WidthPoints\t$HeightPoints\t$MarginLeft\t$MarginTop\t`/bin/expr $WidthPoints - $MarginRight`\t`/bin/expr $HeightPoints - $MarginBottom`"
   LogTrace "\ta2ps --medium=$PaperSize --$PaperOrientation --margin=$Indent --chars-per-line=$Width --lines-per-page=$Lenght --output=FileOut FileIn"
   /usr/local/bin/a2ps --medium=$PaperSize --$PaperOrientation --margin=$Indent --chars-per-line=$Width --lines-per-page=$Lenght --output="$TMPDIR/FileOut" "$TMPDIR/FileIn" 2> "$TMPDIR/Error" || ErrorRunning
   /bin/mv "$TMPDIR/FileOut" "$TMPDIR/FileIn"
   PrintPostScript
}


#
#
#
PrintText() {
   case "$FilterText" in
      a2ps)     PrintA2PS     ;;
      enscript) PrintEnscript ;;
      *)
         LogInfo "FilterText is not set properly. Assuming FilterText=enscript"
         PrintEnscript ;;
   esac
}


###############################################################################
#                           ***   Main Body   ***
###############################################################################
Queue=`/usr/bin/basename "$PWD"`
JobPages=0

#
# Process command line arguments
#
while getopts cw:l:i:n:h: Option; do
   case $Option in
      c) FileType=raw   ;;
      w) Width=$OPTARG   ;;
      l) Lenght=$OPTARG  ;;
      i) Indent=$OPTARG  ;;
      n) User=$OPTARG    ;;
      h) Host=$OPTARG    ;;
   esac
done
eval AccountingFile=\$$OPTIND
unset Option
: ${AccountingFile:=/dev/null}

#
# Load default configuration for the printer/queue (driver name, paper size, method, resolution, etc...)
#
[ -f "$FilterName.conf" ] || ErrorNotConfFile
. $FilterName.conf


#
# Process the control files created by LPD
#
ProcessControlFile

#
# Load user configuration file with options for the printer/queue
# and command line options (-Z)
#
ProcessOptionsFile "`eval echo ~$User`/.config/$FilterName/${Queue}.conf"
ProcessOptionsFile "$TMPDIR/-Z Options"

#
# Fix options
#
FixOptions

/bin/cat - > "$TMPDIR/FileIn"
: ${FileType:=`/usr/bin/file --brief --dereference --mime-type "$TMPDIR/FileIn" | /usr/bin/tr "[:upper:]" "[:lower:]"`}
LogTrace "\n--- ENVIRONMENT VARIABLES ---\n`printenv | /usr/bin/sort`"
LogTrace "\n--- Input-Filter Variables ---\n`set | /usr/bin/grep -e '^[A-Z][a-z]' -e '^nUp' | /usr/bin/sort --ignore-case`\n"

case "$FileType" in
     raw)
        SendToPrinter "$TMPDIR/FileIn" ;;
     application/pdf)
        PrintPDF ;;
     application/postscript)
        PrintPostScript ;;
     text/*)
        PrintText ;;
     *)
        ErrorFileType ;;
esac

#
# Printer accounting
#
LogTrace "Accounting to $AccountingFile\n\tPagesPrinted=`/bin/expr $JobPages \* $Copies` Host=$Host User=$User"
/usr/bin/printf "%7.2f\t%s:%s\n"  `/bin/expr $JobPages \* $Copies`  "$Host"  "$User" >> "$AccountingFile"

#
#  Clean up and exit
#
CleanUp
exit 0


More information about the freebsd-questions mailing list