#! /bin/sh # # zog-hp -- silly script to check incomming postscript, ghostscript it and chuck out PCL # Copyright (C) 2002 Fred Barnes # blank_ps_file () { cat > $1.ps2ps << EOF %!PS-Adobe-2.0 %%Creator: fred barnes %%Pages: 1 %%PageOrder: Ascend %%EndComments %! %%Page: 1 1 showpage %%Trailer %%EOF EOF ps2ps $1.ps2ps $1.ps2psout mv $1.ps2psout $1 rm $1.ps2ps } # echo "$0: called with: $*" 1>&2 # soak up input into a temporary file umask 007 FILE=${TEMPDIR:-/tmp}/$$.zog.ps if [ -f $FILE ]; then rm -f $FILE fi cat > $FILE # check command line args for useful things like which one really and login SPOOL= case $0 in *zog-hp) SPOOL="hp" ;; *zog-hp-draft) SPOOL="hp-draft" ;; *zog-hp-best) SPOOL="hp-best" ;; esac LITERAL_CONTROL_CHARS= OWNER_LOGIN= OWNER_HOST= ACCTFILE= EXITCODE=0 while [ "$1" != "" ]; do case $1 in -c) LITERAL_CONTROL_CHARS=1 ;; -n) OWNER_LOGIN=$2 shift ;; -h) OWNER_HOST=$2 shift ;; -w* | -h* | -i* | -l*) ;; *) if [ "$ACCTFILE" = "" ]; then ACCTFILE=$1 else echo "$0: `date` (${SPOOL:-unknown}): filter run incorrectly. (unexpected $1)" 1>&2 blank_ps_file $FILE EXITCODE=2 fi ;; esac shift done JOBSIZE=`ls -l $FILE | awk '{print $5}'` # make sure we know who is doing this if [ "$OWNER_LOGIN" = "" ] || [ "$OWNER_HOST" = "" ]; then echo "$0: `date` (${SPOOL:-unknown}): job has no owner ($OWNER_LOGIN@$OWNER_HOST, $JOBSIZE bytes)" 1>&2 blank_ps_file $FILE EXITCODE=2 fi # try and see if it's postscript first MIMETYPE=`file -b $FILE | magic2mime` if [ "$MIMETYPE" != "application/postscript" ]; then # can't print this echo "$0: `date` (${SPOOL:-unknown}): job submitted by $OWNER_LOGIN@$OWNER_HOST ($JOBSIZE bytes) is not postscript." 1>&2 blank_ps_file $FILE EXITCODE=2 fi # set some gs options by spool case "$SPOOL" in "hp") GSOPT="-sPrintQuality=normal -sColourModel=Gray" ;; "hp-best") GSOPT="-sPrintQuality=best -sColourModel=Gray" ;; "hp-draft" | *) GSOPT="-sPrintQuality=draft -sColourModel=Gray" ;; esac # lets see what happens when ghostscript processes it.. FAILED=0 echo | gs -q -sDEVICE=pcl3 -sSubdevice=hpdj520 $GSOPT -sOutputFile=$FILE.pcl $FILE 1> $FILE.gsout 2> $FILE.gserr || FAILED=1 if [ $FAILED -eq 1 ]; then echo "$0: `date` (${SPOOL:-unknown}): job submitted by $OWNER_LOGIN@$OWNER_HOST ($JOBSIZE bytes) failed to process. gs output being mailed to user." 1>&2 cat > $FILE.mailout << EOF Print-filter failed to process your print job EOF echo "job for $OWNER_LOGIN@$OWNER_HOST ($JOBSIZE bytes) on spool $SPOOL failed" 1>> $FILE.mailout echo "ghostscript output follows." 1>> $FILE.mailout echo 1>> $FILE.mailout echo "--[standard output]---------------------------------" 1>> $FILE.mailout cat $FILE.gsout 1>> $FILE.mailout echo "--[standard error]----------------------------------" 1>> $FILE.mailout cat $FILE.gserr 1>> $FILE.mailout cat >> $FILE.mailout << EOF . EOF mailx -s "print-filter failure (job for $OWNER_LOGIN@$OWNER_HOST, spool $SPOOL)" $OWNER_LOGIN@$OWNER_HOST < $FILE.mailout rm -f $FILE.mailout EXITCODE=2 # rummage up a blank page blank_ps_file $FILE echo | gs -q -sDEVICE=pcl3 -sSubdevice=hpdj520 -sOutputfile=$FILE.pcl $FILE > $FILE.gsout else echo "$0: `date` (${SPOOL:-unknown}): job submitted by $OWNER_LOGIN@$OWNER_HOST ($JOBSIZE bytes) processed, sending to printer." 1>&2 fi if [ $EXITCODE -eq 0 ]; then # well, looks happy.. cat $FILE.pcl fi rm -f $FILE rm -f $FILE.pcl rm -f $FILE.gsout rm -f $FILE.gserr exit $EXITCODE