Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 205 lines (171 sloc) 5.9 KB
#!/bin/sh
#
# This script is listening on the logbook printer queue
# The comand to start this script is <full_path>/lplisten
#
VERSION="0.9"
PRINTER_HOME=`dirname $0`
WORK=$PRINTER_HOME
LOGBOOK=`basename $PRINTER_HOME`
# printer pipe name
PIPE=prnt
# Labfolder API
pythonScript=@pythonScript
SERVER=@SERVER
HOSTNAME=@HOSTNAME
PROJECTNAME=@PROJECTNAME
PRINTERNAME=${PROJECTNAME}
USER=@USER
PASSWORD=@PASSWORD
cd $PRINTER_HOME
#########################################
echo "START of $0 version $VERSION for $PIPE - `date`" >>./lplisten.log
echo " PRINTER_HOME is: $PRINTER_HOME" >>./lplisten.log
echo " LOGBOOK is: $LOGBOOK" >>./lplisten.log
echo " PIPE is: $PIPE" >>./lplisten.log
echo " WORK is : $WORK" >>./lplisten.log
runloop ()
{
while { $TRUE;} do
cd $PRINTER_HOME
# listen at the pipe until data arrives
cat < ./${PIPE} > ./logbook-prnt-in;
echo "FILE REACHED - `date`" >>./lplisten.log
# get the author, host and title, which are generated by the
# cups system in log file /var/log/cups/page_log
if test -f /var/log/cups/page_log ; then
STRING=`tail /var/log/cups/page_log | grep $LOGBOOK | tail -1`
AUTHOR=`echo $STRING | cut -f2 -d' '`
LPHOST=`echo $STRING | cut -f9 -d' '`
TSTRING=`echo $STRING | cut -f10- -d' '`
TSTRING=`echo $TSTRING | sed 's/-/ /g' |sed 's/|/ /g'| sed 's/[[:blank:]]\+/ /g' | sed 's/ /_/g' | sed 's/_*$//g'`
else
echo " Error: File /var/log/cups/page_log does not exist" >>./lplisten.log
AUTHOR="PrintMeta"
LPHOST="unknown"
TSTRING=""
fi
echo " AUTHOR is : $AUTHOR" >>./lplisten.log
echo " LPHOST is : $LPHOST" >>./lplisten.log
echo " TSTRING is : $TSTRING" >>./lplisten.log
taildone=0
# Used for file name (format: 2003-05-06T18:30:00-00)
#zeit=`date '+%Y-%m-%dT%H:%M:%S'`
#entry=${zeit}-00
#uhr=`date '+%H:%M:%S'`
#isodate=`date '+%Y-%m-%d'`
seconds=`date '+%s'`
#PWD=`pwd`
#BASE=`basename $PWD`
cp ./logbook-prnt-in ./dump-in
# This test coudln't handle a variable banner size
LPerror=`head -n 1 ./logbook-prnt-in`
if test "X^M^M#####^M" = "X$LPerror"; then
tail +20 ./logbook-prnt-in > ./logbook-lpr
taildone=1
elif test "X^M%!PS-Adobe-3.0" = "X$LPerror"; then
echo " Postscript with leading CR" >>./lplisten.log
tail -c +2 ./logbook-prnt-in > ./logbook-lpr
taildone=1
else
echo "No header or banner found" >>./lplisten.log
cp -p ./logbook-prnt-in ./logbook-lpr
fi
cp ./logbook-lpr ./dump-lpr
mv ./logbook-lpr ./logbook-tail
cp ./logbook-tail ./dump-tail
# avoid files created/printed from a WINDOWS PC with
# HP printer driver
HPerror=`head -n 1 ./logbook-tail`
if test "X-12345X@PJL JOB" = "X$HPerror" ; then
echo " Windows File created with HP driver" >>./lplisten.log
i=2
tail +$i ./logbook-tail > ./logbook-tmp
PSHEAD=`head -n 1 ./logbook-tmp | cut -c1-4`
while ( test "%!PS" != $PSHEAD ) do
i=`expr $i + 1`
tail +$i ./logbook-tail > ./logbook-tmp
PSHEAD=`head -n 1 ./logbook-tmp | cut -c1-4`
done
taildone=1
else
mv ./logbook-tail ./logbook-tmp
fi
cp ./logbook-tmp ./dump-tmp
# Check if it is image or text data
#FILETYPE=`file ./logbook-tmp | cut -f2 -d: | cut -f2 -d' '`
FILETYPE=`file ./logbook-tmp | cut -f2 -d: | cut -f2 -d' ' | tr -d [[:space:]]`
echo " File format is: $FILETYPE" >>./lplisten.log
if test "ASCII" = "$FILETYPE" || test "English" = "$FILETYPE" ; then
FILETYPE="ascii"
fi
########################################
# ASCII file -> entry.txt
if test "ascii" = "$FILETYPE" || test "English" = "$FILETYPE" ; then
# insert the text normalize HTML entities
printf "<text>" > ./entry.txt
cat ./logbook-tmp \
| tr -d '[\000-\010][\013-\014][\016-\037][\177-\377]' \
| sed 's/&/\&amp;/g' \
| sed 's/>/\&gt;/g' \
| sed 's/</\&lt;/g' \
| sed 's/"/\&quot;/g' \
| sed "s/'/\&apos;/g" \
>> ./entry.txt
echo "</text>" >> ./entry.txt
########################################
# PDF file
elif test "PDF" = "$FILETYPE" ; then
echo " PDF file" >>./lplisten.log
mv ./logbook-tmp ./entry.pdf
########################################
# PNG file
elif test "PNG" = "$FILETYPE" ; then
echo " PNG file" >>./lplisten.log
mv ./logbook-tmp ./entry.pdf
########################################
# JPG file
elif test "JPG" = "$FILETYPE" ; then
echo " JPG file" >>./lplisten.log
mv ./logbook-tmp ./entry.pdf
########################################
# Postscript file
elif test "PostScript" = "$FILETYPE" ; then
mv ./logbook-tmp ./tmp.ps
# replace dummy "^M" by "\n"(solves problems with Tektronix PS files)
cat ./tmp.ps | tr '\015' '\n' > ./entry.ps
/bin/rm -f ./tmp.ps
########################################
# Unsupported format
else
echo " Wrong format $FILETYPE" >>./lplisten.log
echo "<text>" >> ./entry.txt
echo "Wrong data format: $FILETYPE" >> ./entry.txt
echo "</text>" >> ./entry.txt
# !!! ToDo mv ./logbook-tmp ./bad_files/$zeit-tmp
fi
########################################
# Done with file creation - move them to data path
echo " Load files to logbook ..." >> ./lplisten.log
for EXT in pdf ps txt; do
if test -f entry.${EXT}; then
mv entry.${EXT} ${TSTRING}.${EXT}
### Use labfolder API to create a new entry and attach the file
python ${pythonScript} "${SERVER}" "${HOSTNAME}" "${USER}" "${PASSWORD}" "${PROJECTNAME}" ${TSTRING}.${EXT} ${AUTHOR} ${LPHOST} lplisten.log
rm -f ${TSTRING}.${EXT}
echo " moved entry.${EXT} to $LOGBOOK" >> ./lplisten.log
fi
done
echo " done moving files." >>./lplisten.log
########################################
# clean up
for EXT in "pdf" "ps" "txt"; do
if test -f entry.${EXT} ; then
/bin/rm -f entry.${EXT}
fi
done
echo "File created @ `date`" >>./lplisten.log
done;
}
# Run loop as function to catch PID of subshell
{ (runloop >&3 3>&- & echo $!); } 3>&1 > $WORK/lplisten.pid