#!/bin/bash
#
# (c) D. Czechowski 2010
#
# Based on email dated June 2003 to printing.cups.devel
# (c) Kurt Pfeifle, Danka Deutschland GmbH
# [ this is not really a copyright ; - ) Use and modify freely as you like,
# but don't complain to me...]

JOBID=$1
USER=$2
TITLE=$3
COPIES=$4
OPTIONS=$5
FILE=$6
PID=$$

# if there are no arguments: print the accepted URI
if [ $# -eq 0 ]; then
echo "DEBUG: $1, $2, $3, $4, $5, $6, $$"
echo "direct robo "Unknown" "Send job to graphtecprint""
exit 0
fi

# in case of wrong number of arguments: print usage hint
if [ $# -ne 5 -a $# -ne 6 ]; then
echo ""
echo "Usage: robo job-id user title copies options [file]"
echo "(this executes graphtecprint)"
echo ""
echo "Install a printqueue with 'lpadmin -p -v robo:/ -E"
echo ""
exit 1
fi

PLOTTER=Robo
TMPFILE="/tmp/plot-"`date +%Y%m%d%H%M%S`

# check "accepting" status first...
GREPSTRING="not accepting" ;
if lpstat -a $PLOTTER | grep "$GREPSTRING" &> /dev/null ; then
echo "ERROR: printer $PLOTTER not accepting jobs"
exit 1
fi

# feed postscript data to graphtecprint
logger "trigger graphtecprint $1 $2 $3 $4 $5 $6 $$"
if [ $6 == ""]; then
	logger "print from localhost"
	cd /tmp/
	while read data; do
	    echo "$data" >> $TMPFILE
	done
else
	logger "print from remote: $5"
	cd /tmp/
	cat $6 >> $TMPFILE
fi


cat $TMPFILE | DISPLAY=:0 sudo -u labor graphtecprint &> /tmp/graphtecprint.log
logger "graphtecprint triggerd - exiting"
rm $TMPFILE

exit 0

