ricoh-sp100-sp204/pstoricohddst-gdi

199 lines
4.3 KiB
Text
Raw Normal View History

#!/bin/bash
# Debug mode: change to 'yes' to enable
DEBUG=no
function log() {
[ "${DEBUG}" = "yes" ] && echo $* | logger -t "$0[$$]"
}
function logpipe() {
[ "${DEBUG}" = "yes" ] && echo $* | logger -t "$0[$$]"
}
2013-09-08 11:39:31 +02:00
function trapINT() {
log "trapINT()"
2013-09-08 12:18:22 +02:00
[ "x$trp" = "x" ] && trp="yes" || return
sleep 30 && { [ ! "${DEBUG}" = "yes" ] && rm -rf $uid; log "Cleanup complete"; } &
2013-09-08 11:39:31 +02:00
exit
}
function stop() {
log "Stop stop page";
2013-09-08 11:39:31 +02:00
echo "stop" > $uid/999999999-page.pbm
}
log "Called with cmdline: $0 $*"
2013-09-08 12:18:22 +02:00
2013-09-08 11:39:31 +02:00
trap "stop; trapINT" SIGINT SIGTERM SIGQUIT
#trap 'echo No' SIGINT SIGTERM SIGQUIT EXIT;
2013-09-06 17:29:49 +02:00
# Username
user="$2"
2013-09-06 17:29:49 +02:00
# Page title (not used at this time, "Document" instead)
ptitle="$3"
2013-09-08 11:39:31 +02:00
my="$0"
options="$5"
pagesize="A4"
for opt in $options; do
case "$opt" in
PageSize=*)
pagesize="$(echo "${opt#PageSize=}" | tr a-z A-Z)"
;;
esac
done
2013-09-06 17:29:49 +02:00
2014-05-07 07:16:14 +02:00
# MS-style EOL
e=$(echo -en "\r")
2013-09-06 17:29:49 +02:00
# Printing date
ddate="`LC_ALL=en_US.UTF-8 date '+%Y/%m/%d %H:%M:%S'`"
2013-09-06 17:29:49 +02:00
# Temporary directory
uid="/tmp/pstoricohddst-gdi-`uuidgen`"
mkdir -p $uid
2014-05-07 07:16:14 +02:00
[ "${DEBUG}" = "yes" ] && exec >$uid/output.stream #> >(tee $uid/output.stream)
[ -x "$(which inotifywait)" ] && {
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"
cat <<EOF
2014-05-07 07:16:14 +02:00
%-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
stage="printing"
}
[ "$page" = "999999999-page.pbm" ] && {
log "Last stage. Flushing PJL footer"
cat <<EOF
2014-05-07 07:16:14 +02:00
@PJL EOJ$e
%-12345X
EOF
pid=`ps ax | grep $uid | grep -v grep | awk ' { print $1 } '`
[ ! "x$pid" = "x" ] && kill $pid
break
}
[ "$stage" = "printing" ] && {
# 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`
# Taking image dimensions
read fn ft xs ys garb < <(identify $uid/$page | tr "x" " ")
# Flushing page header
cat <<EOF
2014-05-07 07:16:14 +02:00
@PJL SET PAGESTATUS=START$e
@PJL SET COPIES=1$e
@PJL SET MEDIASOURCE=AUTO$e
@PJL SET MEDIATYPE=PLAINRECYCLE$e
@PJL SET PAPER=$pagesize$e
2014-05-07 07:16:14 +02:00
@PJL SET PAPERWIDTH=$xs$e
@PJL SET PAPERLENGTH=$ys$e
@PJL SET RESOLUTION=600$e
@PJL SET IMAGELEN=$jsize$e
EOF
log "Flushing image $page"
cat $uid/raster.jbig
# Flushing page footer
# TODO: pixelcount for toner estimate
cat <<EOF
2014-05-07 07:16:14 +02:00
@PJL SET DOTCOUNT=1132782$e
@PJL SET PAGESTATUS=END$e
EOF
}
done
) &
# Converting from PostScript to PostScript-monochrome, then to PBM image format (per page)
2014-05-07 07:16:14 +02:00
#gs -sDEVICE=ps2write -sOutputFile=- -r600 -dQUIET -dBATCH -dNOPAUSE - |
gs -sDEVICE=pbmraw -sOutputFile=${uid}/%03d-page.pbm -r600 -dQUIET -dBATCH -dNOPAUSE -
stop
wait
trapINT
} || {
log "Synchronous variant"
# Converting from PostScript to PostScript-monochrome, then to PBM image format (per page)
log "Converting document to pages"
gs -sDEVICE=ps2write -sOutputFile=- -r600 -dQUIET -dBATCH -dNOPAUSE - | gs -sDEVICE=pbmraw -sOutputFile=${uid}/%03d-page.pbm -r600 -dQUIET -dBATCH -dNOPAUSE -
log "Conversion complete"
2013-09-08 11:39:31 +02:00
cat <<EOF
2014-05-07 07:16:14 +02:00
%-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
2013-09-08 11:39:31 +02:00
EOF
for page in ${uid}/*-page.pbm; do
log "Page $page"
# Converting page to JBIG format (parameters are very special for this printer!)
pbmtojbg -p 72 -o 3 -m 0 -q < $page > $uid/raster.jbig
# Taking image size
jsize=`wc -c < $uid/raster.jbig`
# Taking image dimensions
read fn ft xs ys garb < <(identify $page | tr "x" " ")
log "Identified as ${xs}x${ys}"
# Flushing page header
cat <<EOF
2014-05-07 07:16:14 +02:00
@PJL SET PAGESTATUS=START$e
@PJL SET COPIES=1$e
@PJL SET MEDIASOURCE=AUTO$e
@PJL SET MEDIATYPE=PLAINRECYCLE$e
@PJL SET PAPER=pagesize$e
2014-05-07 07:16:14 +02:00
@PJL SET PAPERWIDTH=$xs$e
@PJL SET PAPERLENGTH=$ys$e
@PJL SET RESOLUTION=600$e
@PJL SET IMAGELEN=$jsize$e
EOF
# Flushing image
cat $uid/raster.jbig
# Flushing page footer
# TODO: pixelcount for toner estimate
cat <<EOF
2014-05-07 07:16:14 +02:00
@PJL SET DOTCOUNT=1132782$e
@PJL SET PAGESTATUS=END$e
EOF
done
# Flushing PJL footer
cat <<EOF
2014-05-07 07:16:14 +02:00
@PJL EOJ$e
%-12345X
EOF
2013-09-08 11:39:31 +02:00
}
2013-09-08 11:39:31 +02:00
#rm -rf $uid;
exit 0