#!/bin/bash

VERSION=2001121901

### PTAL CUPS backend
### Copyright (C) 2001 Mark J. Horn <mark@hornclan.com>
###
### 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 backends directory, which
### might be in /usr/lib/cups/backends or /usr/local/lib/cups/backends.
### 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
### id cupsd runs with, and then restart CUPS.
###
### 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 currently only works with "mlc:" type devices (i.e. locally
### attached devices).  It does not currently work with "hpjd:" devices.
### Printing to "hpjd:" type devices can already 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.  YMMV.
### 
### DEPENDANCIES
### ------------
### This script is dependant upon builtin 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-devid, ptal-connect, basename, cat
### If these commands are not in /bin, /usr/bin, or /usr/local/bin on
### your computer, set PATH below to include where they are located.

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

### Uncomment for crude debugging output
#DEBUG=true

ME=`basename $0`

if [ -z "$*" ]; then
	for dev in /dev/ptal-mlcd/*; do
		DEV=mlc:${dev#/dev/ptal-mlcd/}
		DESC=`ptal-devid $DEV -short -mfg -mdl 2>/dev/null`
		if [ $? -eq 0 ]; then 
			echo direct $ME:/$DEV \"$DESC\" \"PTAL $DEV\"
		fi
	done
	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}:/}

if [ ! -z "$DEBUG" ]; then
	echo "Args: $0 $*" > /tmp/printargs
	echo "Arg1: $1" >> /tmp/printargs
	echo "Arg2: $2" >> /tmp/printargs
	echo "Arg2: $3" >> /tmp/printargs
	echo "Arg2: $4" >> /tmp/printargs
	echo "Arg2: $5" >> /tmp/printargs
	echo "Arg2: $6" >> /tmp/printargs
	echo "Arg2: $7" >> /tmp/printargs
	command -V ptal-devid >> /tmp/printargs 2>&1
	command -V ptal-connect >> /tmp/printargs 2>&1
	command -V basename >> /tmp/printargs 2>&1
	command -V cat >> /tmp/printargs 2>&1
	declare -p >> /tmp/printargs
	cat $6 > /tmp/printout
	cat /tmp/printout | ptal-connect -print $SENDTO
else
	cat $FILE | ptal-connect -print $SENDTO
fi
