#!/bin/sh
#
# CUPS2Pegg Version 0.1a
#
# This script is a CUPS Backend for use with pegg in order to enable
# cups-printing with a CASIO Label Printer.
#
#
# 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.
#
# Copy this script to your CUPS Backend directory (eg. /usr/lib/cups/backend)
# and restart the cups deamon. DO NOT FORGET TO COPY THE CASIO_KLKP.PPD
# FILE INTO THE PPD DIRECTORY OF CUPS (eg. /usr/share/cups/model)
#
# This backend is based on the pdfdistiller script by Michael Goffioul it is
# licensed by the Terms of the GPL. You should have recieved a copy of the GPL
# along with this file
#
# Copyright (C) Michael Goffioul (goffioul@imec.be) 2001
# Pegg Modifications Copyright (C) Daniel Amkreutz (aixpresso@web.de) 2004

LOGFILE=/var/log/cups/cups2pegg.log
CONVERT=`which convert`
PEGG=`which pegg`
XBM2CRW=`which xbm2crw`

echo "Executables: $PEGG & $CONVERT" > $LOGFILE
echo "Arguments: |$1|$2|$3|$4|$5|$6|" >> $LOGFILE
date >> $LOGFILE

# case of no argument, prints available URIs

if [ $# -eq 0 ]; then
	if [ ! -x "$PEGG" ]; then
		exit 0
	fi
	if [ ! -x "$CONVERT" ]; then
		exit 0
	fi
	if [ ! -x "$XBM2CRW" ]; then
		exit 0
	fi
	echo "direct cups2pegg \"Unknown\" \"Pegg Printer\""
	exit 0
fi

# case of wrong number of arguments
if [ $# -ne 5 -a $# -ne 6 ]; then
	echo "Usage: cups2pegg job-id user title copies options [file]"
	exit 1
fi

# get CUPS2Pegg working directory from device URI, and check write status

PEGGDIR=${DEVICE_URI#cups2pegg:}

if [ ! -d "$PEGGDIR" -o ! -w "$PEGGDIR" ]; then
	echo "ERROR: directory $PEGGDIR not writable"
	exit 1
fi

echo "CUPS2PEGG working directory: $PEGGDIR" >> $LOGFILE


# start conversion

if [ $# -eq 6 ]; then
       echo "Using File $6"
       echo
       echo "CONVERT OOUTPUT"
       echo "---------------"
       $CONVERT -page 512x64 -rotate 90 $6 $PEGGDIR/pegg_tmp.xbm >> $LOGFILE
       echo "XBM2CRW OUTPUT"
       echo "--------------"
       $XBM2CRW $PEGGDIR/pegg_tmp.xbm >> $LOGFILE
       echo "Pegg    OUTPUT"
       echo "--------------"
       $PEGG -5 $PEGGDIR/pegg_tmp.xbm.crw >> $LOGFILE

else
       echo
       echo "Using /dev/stdin"
       cat /dev/stdin > $PEGGDIR/pegg_tmp.ps
       echo "CONVERT OOUTPUT"
       echo "---------------"
       $CONVERT -page 512x64 -rotate 90 $PEGGDIR/pegg_tmp.ps $PEGGDIR/pegg_tmp.xbm >> $LOGFILE
       echo "XBM2CRW OUTPUT"
       echo "--------------"
       $XBM2CRW $PEGGDIR/pegg_tmp.xbm >> $LOGFILE
       echo "Pegg    OUTPUT"
       echo "--------------"
       $PEGG -5 $PEGGDIR/pegg_tmp.xbm.crw >> $LOGFILE

fi

exit 0
