#!/bin/bash

### MTINK CUPS backend
### Derived from "ptal" backend from Mark J. Horn <mark at hornclan dot 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 backend directory, which
### might be in /usr/lib/cups/backend or /usr/local/lib/cups/backend.
### Name it "mtink". 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 mtink" (assuming that you named the script
### "mtink").
###
### In order for CUPS to detect your MTINK-connected printer at boot time
### it will have to be started *AFTER* mtink.  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.
###
### 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.
###
### 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 the correctly installed mtink drivers
### available at: http://xwtools.automatix.de/
###
### This script depends on the following programs being in the PATH
### 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 basename >> $DEBUG_PRINTARGS 2>&1
	command -V cat >> $DEBUG_PRINTARGS 2>&1
	declare >> $DEBUG_PRINTARGS
fi

ME=`basename $0`

if [ -z "$*" ]; then
	for DEV in `ls -1 /var/mtink 2>/dev/null` ; do
		DESC=`basename $DEV`
		if [ $? -eq 0 ]; then 
			echo direct $ME:/$DESC \"Epson Stylus $DESC\" \"MTINK $DESC\"
		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 [ -n "$DEBUG" ]; then
	echo "SENDTO: $SENDTO" >> $DEBUG_PRINTARGS
	cat $FILE > $DEBUG_PRINTOUT
	FILE=$DEBUG_PRINTOUT
fi

### We need a lock in order to avoid that more as one process
### write to the pipe. The server will work as root, so we have
### to do this externally, locking the pipe or setting the right
### to read only do not work.

while [ -f /var/lock/subsys/mtink.$SENDTO ]
do
   echo mtink:$SENDTO busy; will retry in 30 seconds... 1>&2
   sleep 30
done

### cleanup at exit
trap "rm /var/lock/mtink.$SENDTO" 1 2 3 23 13 15
### create the lock file
touch /var/lock/mtink.$SENDTO 

### process the input stream
cat $FILE > /var/mtink/$SENDTO
STAT=$?

### wait a little bit so the last data are proccessed
### by the server
sleep 5

### remove the lock file
rm /var/lock/mtink.$SENDTO 
exit $STAT
