#!/bin/bash
# scripts/ptal-cups.  Generated from ptal-cups.in by configure.

### PTAL CUPS backend
### Copyright (C) 2001-2002 Mark J. Horn <mark at hornclan dot com>
### Modified somewhat by David Paschal to integrate with hpoj codebase
###
### This program is free software; you can redistribute it and/or
### modify it under the terms of the GNU General Public License
### as published by the Free Software Foundation; either version 2
### of the License, or (at your option) any later version.
### 
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
### GNU General Public License for more details.
### 
### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
###
### A copy of the GNU General Public License is available at: 
### http://www.gnu.org/licenses/gpl.txt

### INSTALLATION
### ------------
### To use this script, copy it into your CUPS backend directory, which
### might be in /usr/lib/cups/backend or /usr/local/lib/cups/backend.
### A couple of folks have reported problems with naming the script
### anything other than "ptal".  I believe (hope) I've corrected all
### those problems.  Make sure that the script is executable by whatever
### user ID cupsd runs with, and then restart CUPS.  To make the script
### executable, run "chmod a+rx ptal" (assuming that you named the script
### "ptal").
###
### In order for CUPS to detect your PTAL-connected printer at boot time
### it will have to be started *AFTER* ptal-init.  If it isn't, then 
### CUPS will not detect your printer, and you will not be able to 
### configure, nor print to your printer.
###
### You should then be able to configure printers in CUPS with the
### devices you've configured.  I have only been able to configure
### devices with the CUPS web interface.  I have not been able to 
### get kups to properly configure ptal devices.  IMHO, this is a 
### bug in kups.
###
### This single script can be used to support multiple devices.  Each 
### supported device needs to be announced by the script when called 
### without arguments.  CUPS determines what printers are available
### when cupsd starts.  Thus any additional devices that you add 
### will require a restart of cupsd.
###
### This script now works with both "mlc:" type devices (i.e. locally
### attached devices) and "hpjd:" devices (HP JetDirect).
### Printing to "hpjd:" type devices can also be done in CUPS 
### using the "socket" backend (aka "AppSocket").  Using that backend
### to print shouldn't interfere with scanning from "hpjd:" devices
### using the ptal driver.
### 
### DEPENDENCIES
### ------------
### This script is dependent upon built-in commands in bash.  It has not
### been tested against other non-bash shells.  I don't expect that
### it will work with them.  You are invited to test other shells.
### Please let me know if you have any success.
### 
### This script depends on correctly installed hpoj ptal drivers
### available at: http://hpoj.sourceforge.net
###
### This script depends on the following programs being in the PATH
###   ptal-device, ptal-devid, ptal-connect, basename, cat
### If these commands are not in /usr/bin, /bin, /usr/bin,
### or /usr/local/bin on your computer, set PATH below to include
### where they are located.

PATH=$PATH:/usr/bin:/bin:/usr/bin:/usr/local/bin

### Uncomment for crude debugging output
# DEBUG=true
### Where we log debug stuff to:
DEBUG_PRINTARGS=/tmp/printargs
DEBUG_PRINTOUT=/tmp/printout

if [ -n "$DEBUG" ]; then
	echo "Args: $0 $*" > $DEBUG_PRINTARGS
	echo "Arg1: $1" >> $DEBUG_PRINTARGS
	echo "Arg2: $2" >> $DEBUG_PRINTARGS
	echo "Arg3: $3" >> $DEBUG_PRINTARGS
	echo "Arg4: $4" >> $DEBUG_PRINTARGS
	echo "Arg5: $5" >> $DEBUG_PRINTARGS
	echo "Arg6: $6" >> $DEBUG_PRINTARGS
	echo "Arg7: $7" >> $DEBUG_PRINTARGS
	command -V ptal-device >> $DEBUG_PRINTARGS 2>&1
	command -V ptal-devid >> $DEBUG_PRINTARGS 2>&1
	command -V ptal-connect >> $DEBUG_PRINTARGS 2>&1
	command -V basename >> $DEBUG_PRINTARGS 2>&1
	command -V cat >> $DEBUG_PRINTARGS 2>&1
	declare >> $DEBUG_PRINTARGS
fi

ME=`basename $0`

if [ -z "$*" ]; then
	unset FOUND_DEVICE
	for DEV in `ptal-device` ; do
		DESC=`ptal-devid $DEV -short -mfg -mdl 2>/dev/null`
		if [ $? -eq 0 ]; then 
			echo direct $ME:/$DEV \"$DESC\" \"PTAL $DEV\"
			FOUND_DEVICE=1
		fi
		[ -n "$DEBUG" ] && ptal-devid $DEV >> $DEBUG_PRINTARGS
	done

	if [ -z "$FOUND_DEVICE" ]; then
		echo direct $ME \"Unknown\" \"PTAL device not found\"
	fi

	exit 0
fi

### For raw printing, $6 is the file to print.  For driver processed
### printing, $6 is empty and the data to print is in stdin.
FILE=$6

### When advertising multiple printers, the script has to be able
### determine where it should send real print jobs.  This is done
### through the environment variable $DEVICE_URI
SENDTO=${DEVICE_URI#${ME}:}	# Remove the backend name and colon.
# Remove arbitrary number of slashes:
while true ; do
	foo=${SENDTO#/}
	if test "$foo" == "$SENDTO" ; then
		break
	fi
	SENDTO=$foo
done

if [ -n "$DEBUG" ]; then
	echo "SENDTO: $SENDTO" >> $DEBUG_PRINTARGS
	cat $FILE > $DEBUG_PRINTOUT
	FILE=$DEBUG_PRINTOUT
fi
cat $FILE | ptal-connect $SENDTO -print -infretry -retrydelay 10
