205 lines
4.5 KiB
Bash
Executable file
205 lines
4.5 KiB
Bash
Executable file
#!/bin/bash
|
||
|
||
#Next four lines must be uncommented in MacOS
|
||
#GS=/opt/local/bin/gs
|
||
#PBMTOJBG=/opt/local/bin/pbmtojbg
|
||
#IDENTIFY=/opt/local/bin/identify
|
||
#uid="/var/spool/cups/tmp/pstoricohddst-gdi-`uuidgen`"
|
||
|
||
# Next four lines must be uncommented in Linux
|
||
GS=gs
|
||
PBMTOJBG=pbmtojbg
|
||
IDENTIFY=identify
|
||
uid="/tmp/pstoricohddst-gdi-`uuidgen`"
|
||
|
||
# Debug mode: change to 'yes' to enable
|
||
DEBUG=${DEBUG+no}
|
||
|
||
function log() {
|
||
[ "${DEBUG}" = "yes" ] && echo $* | logger -t "$0[$$]"
|
||
}
|
||
|
||
function logpipe() {
|
||
[ "${DEBUG}" = "yes" ] && echo $* | logger -t "$0[$$]"
|
||
}
|
||
|
||
function trapINT() {
|
||
log "trapINT()"
|
||
[ "x$trp" = "x" ] && trp="yes" || return
|
||
sleep 30 && { [ ! "${DEBUG}" = "yes" ] && rm -rf $uid; log "Cleanup complete"; } &
|
||
exit
|
||
}
|
||
|
||
function stop() {
|
||
log "Stop stop page";
|
||
echo "stop" > $uid/999999999.page.pbm
|
||
}
|
||
|
||
function pageOutput() {
|
||
|
||
# Converting page to JBIG format (parameters are very special for this printer!)
|
||
$PBMTOJBG -p 72 -o 3 -m 0 -q < $uid/$page > $uid/raster.jbig
|
||
|
||
# Taking image size
|
||
jsize=`wc -c < $uid/raster.jbig | awk ' { printf($1); }' `
|
||
|
||
# Taking image dimensions
|
||
#read fn ft xs ys garb < <($IDENTIFY $uid/$page | tr "x" " ") #Not work under MacOS
|
||
ident=`$IDENTIFY $uid/$page | tr "x" " "`
|
||
xs=`echo $ident | awk ' { print $3 } '`
|
||
ys=`echo $ident | awk ' { print $4 } '`
|
||
log "Identified as ${xs}x${ys}"
|
||
|
||
# Flushing page header
|
||
cat <<EOF
|
||
@PJL SET PAGESTATUS=START$e
|
||
@PJL SET COPIES=1$e
|
||
@PJL SET MEDIASOURCE=$mediasource$e
|
||
@PJL SET MEDIATYPE=PLAINRECYCLE$e
|
||
@PJL SET PAPER=$pagesize$e
|
||
@PJL SET PAPERWIDTH=$xs$e
|
||
@PJL SET PAPERLENGTH=$ys$e
|
||
@PJL SET RESOLUTION=${resolution%x600}$e
|
||
@PJL SET IMAGELEN=$jsize$e
|
||
EOF
|
||
|
||
# Flushing image
|
||
cat $uid/raster.jbig
|
||
|
||
# Flushing page footer
|
||
# TODO: pixelcount for toner estimate
|
||
cat <<EOF
|
||
@PJL SET DOTCOUNT=1132782$e
|
||
@PJL SET PAGESTATUS=END$e
|
||
EOF
|
||
|
||
#end of pageOutput
|
||
}
|
||
|
||
function pjlHeader() {
|
||
cat <<EOF
|
||
%-12345X@PJL$e
|
||
@PJL SET TIMESTAMP=$ddate$e
|
||
@PJL SET FILENAME=Document$e
|
||
@PJL SET COMPRESS=JBIG$e
|
||
@PJL SET USERNAME=$user$e
|
||
@PJL SET COVER=OFF$e
|
||
@PJL SET HOLD=OFF$e
|
||
EOF
|
||
}
|
||
|
||
function pjlFooter() {
|
||
cat <<EOF
|
||
@PJL EOJ$e
|
||
%-12345X$e
|
||
EOF
|
||
}
|
||
|
||
log "Called with cmdline: $0 $*"
|
||
|
||
trap "stop; trapINT" SIGINT SIGTERM SIGQUIT
|
||
#trap 'echo No' SIGINT SIGTERM SIGQUIT EXIT;
|
||
# Username
|
||
user="$2"
|
||
|
||
# Page title (not used at this time, "Document" instead)
|
||
ptitle="$3"
|
||
my="$0"
|
||
options="$5"
|
||
|
||
pagesize="A4"
|
||
resolution="600"
|
||
mediasource="TRAY1"
|
||
for opt in $options; do
|
||
case "$opt" in
|
||
PageSize=*)
|
||
pagesize="$(echo "${opt#PageSize=}" | tr a-z A-Z)"
|
||
;;
|
||
Resolution=*)
|
||
resolution=${opt#Resolution=}
|
||
resolution=${resolution%dpi}
|
||
;;
|
||
InputSlot=*)
|
||
mediasource=${opt#InputSlot=}
|
||
;;
|
||
esac
|
||
done
|
||
|
||
# MS-style EOL
|
||
e=$(echo -en "\r")
|
||
|
||
# Printing date
|
||
ddate="`LC_ALL=en_US.UTF-8 date '+%Y/%m/%d %H:%M:%S'`"
|
||
|
||
# Temporary directory
|
||
uid="/tmp/pstoricohddst-gdi-`uuidgen`"
|
||
mkdir -p $uid
|
||
[ "${DEBUG}" = "yes" ] && exec >$uid/output.stream #> >(tee $uid/output.stream) #does not work under MacOS
|
||
|
||
if [ -x "$(which inotifywait)" ]; then {
|
||
log "Asynchronous variant"
|
||
|
||
(
|
||
stage="empty"
|
||
inotifywait -q -m -r -e close_write --format '%f' $uid | grep --line-buffered 'page.pbm$' | while read page; do
|
||
log "Page submitted"
|
||
[ "$stage" = "empty" ] && {
|
||
log "1st stage. Flushing PJL header"
|
||
pjlHeader;
|
||
|
||
stage="printing"
|
||
}
|
||
[ "$page" = "999999999.page.pbm" ] && {
|
||
log "Last stage. Flushing PJL footer"
|
||
pjlFooter;
|
||
|
||
pid=`ps ax | grep $uid | grep -v grep | awk ' { print $1 } '`
|
||
[ ! "x$pid" = "x" ] && kill $pid
|
||
break
|
||
}
|
||
[ "$stage" = "printing" ] && {
|
||
pageOutput;
|
||
}
|
||
done
|
||
) &
|
||
|
||
# Converting from PostScript to PostScript-monochrome, then to PBM image format (per page)
|
||
#$GS -sDEVICE=ps2write -sOutputFile=- -r$resolution -dQUIET -dBATCH -dNOPAUSE - |
|
||
$GS -sDEVICE=pbmraw -sOutputFile=${uid}/%03d.page.pbm -r$resolution -dQUIET -dBATCH -dNOPAUSE -
|
||
|
||
stop
|
||
wait
|
||
|
||
trapINT
|
||
}
|
||
else {
|
||
log "Synchronous variant"
|
||
mkdir ${uid}/gs-temp
|
||
export TEMP=${uid}/gs-temp
|
||
|
||
# Converting from PostScript to PostScript-monochrome, then to PBM image format (per page)
|
||
log "Converting document to pages"
|
||
$GS -sDEVICE=ps2write -sOutputFile=- -r$resolution -dQUIET -dBATCH -dNOPAUSE - | $GS -sDEVICE=pbmraw -sOutputFile=${uid}/%03d.page.pbm -r$resolution -dQUIET -dBATCH -dNOPAUSE -
|
||
log "Conversion complete"
|
||
|
||
pjlHeader;
|
||
|
||
ls -1 $uid | grep "page.pbm" | while read page; do
|
||
#for page in ${uid}/*.page.pbm; do #looks untested - does not work
|
||
log "Page $page"
|
||
|
||
pageOutput;
|
||
|
||
done
|
||
|
||
# Flushing PJL footer
|
||
pjlFooter;
|
||
|
||
if [ ! "${DEBUG}" = "yes" ]; then
|
||
rm -rf $uid
|
||
fi
|
||
}
|
||
fi
|
||
|
||
exit 0
|