#! /bin/sh
#
# turboprint - simple test script to call tpfilter from command line
#
# (c) ZEDOnet 2000,2001
#
# parameters are -Pshortname input output
# shortname is the short printer name defined in tpconfig
# the spool directory for this printer queue is retrieved from printcap
#

eval $(cat /etc/turboprint/system.cfg)

# check if command line is valid; otherwise print usage

if [ $# -ne 3 ] ; then
	echo "turboprint - convert a document to printer data"
	echo "usage: turboprint -P[PRINTERNAME] [INPUTFILE] [OUTPUTFILE]"
	echo "	this is only a script for testing!"
	echo "	normal usage: lpr -P[PRINTERNAME] [INPUTFILE]"
	exit 1
fi

PRINTERNAME=${1#-P}

if [ $PRINTERNAME = $1 -o -z $PRINTERNAME ] ; then
	echo "turboprint - convert a document to printer data"
	echo "usage: turboprint -P[PRINTERNAME] [INPUTFILE] [OUTPUTFILE]"
	echo "	this is only a script for testing!"
	echo "	normal usage: lpr -P[PRINTERNAME] [INPUTFILE]"
	exit 1
fi

echo "turboprint - executing tpfilter manually"
echo "printer name: $PRINTERNAME"

# find the printcap line for a given printer name

PRINTCAPLINE=$(grep -E "^$PRINTERNAME\|" "$TPFILE_PRINTCAP")

if [ -z $PRINTCAPLINE ] ; then
	PRINTCAPLINE=$(grep -E "\|$PRINTERNAME\|" "$TPFILE_PRINTCAP")
fi
if [ -z $PRINTCAPLINE ] ; then
	PRINTCAPLINE=$(grep -E "\|$PRINTERNAME:" "$TPFILE_PRINTCAP")
fi
if [ -z $PRINTCAPLINE ] ; then
	echo "Error: printer not found in printcap"
	exit 1
fi

# the spool directory is one before the end of the line, 2nd or 3rd name

SPOOLDIR=$(echo "$PRINTCAPLINE" | cut -d'|' -f4 | cut -d':' -f1)

if [ $SPOOLDIR ] ; then
	SPOOLDIR=$(echo "$PRINTCAPLINE" | cut -d'|' -f3 | cut -d':' -f1)
else
	SPOOLDIR=$(echo "$PRINTCAPLINE" | cut -d'|' -f2 | cut -d':' -f1)
fi
if [ -z $SPOOLDIR ] ; then
	echo "Error: spooldir not found"
	exit 1
fi

echo "spool directory: $SPOOLDIR"

if [ ! -e $2 ] ; then
	echo "Error: Input file $2 not found"
	exit 1
fi

# call tpfilter

COMMANDLINE="$TPPATH_FILTERS/tpfilter -h $HOSTNAME -n $LOGNAME -j $2 $TPPATH_SPOOL/$SPOOLDIR/acct < $2 > $3"

echo "*** calling tpfilter now ***"

eval $COMMANDLINE

echo "*** tpfilter finished ***"
echo "the output file may be copied to the printer now"
echo "for example: cat [OUTPUTFILE] > /dev/lp0"
exit 0