#!/bin/bash
# CUPS backend for YajHFC

if [ $# == 0 ]; then
  echo 'file yajhfc:/var/spool/yajhfc/printer-$USERNAME "YajHFC Fax Printer" "HylaFAX" "MFG:HylaFAX;MDL:(HylaFAX);DES:YAJHFC HylaFAX Printer;CLS:PRINTER;CMD:POSTSCRIPT;"'
  exit
fi

# Get the user that owns the job
USERNAME=$2

if [ -z $USERNAME ]; then
	echo "WARNING: No user given. Discarding print job." 1>&2
	exit 0
fi

if [ -n "$DEVICE_URI" ]; then
  PIPENAME=${DEVICE_URI#yajhfc:}
  PIPENAME=${PIPENAME/\$USERNAME/$USERNAME}
else
  PIPENAME="/var/spool/yajhfc/printer-$USERNAME"
fi

if [ "$PIPENAME" == "/" ]; then
  PIPENAME="/var/spool/yajhfc/printer-$USERNAME"
fi

if [ ! -p "$PIPENAME" ]; then
	echo "WARNING: Pipe $PIPENAME does not exist or is not a pipe, discarding print job (is YajHFC running?)" 1>&2
	exit 0
fi

if [ ! -w "$PIPENAME" ]; then
	echo "ERROR: Pipe $PIPENAME is not writeable." 1>&2
	exit 1
fi

m=`cat $6 >"$PIPENAME" 2>&1`
RC=$?
case $RC in
	0) echo "INFO: Fax sent" 1>&2 ;;
	*) echo "ERROR: cat error (RC=$RC): $m" 1>&2 ;;
esac

exit $RC
