#!/bin/sh
PAGE=a4
FAXPRG=/usr/bin/capisuitefax
LOGDIR=/var/log/cups
#
# This is fax4CUPS 1.24, a fax back-end for CUPS
#
# Copyright (C) 2001-2003 Sebastiano Vigna <vigna@acm.org>
#
#   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.
#
# Modified by Michael Goffioul <goffioul@imec.be>
#
#   - phone number as option instead of job name: "-o phone=<number>"
#
# Additional fixes by Kevin Ivory <Ivory@SerNet.de>
#
# Additional mgetty-fax support by Daniel Glanzmann <dg@ezcom.de>
#
# Modified by Daniel Glanzmann <dg@ezcom.de>
#
#   - take care of mgetty-fax return values
#   - small cleanup
#
# Additional capisuite support by Daniel Glanzmann <dg@ezcom.de>
#


# Called with no arguments, we list the capi device

if [ $# -eq 0 ]; then
	if [ ! -x "$FAXPRG" ]; then exit 0; fi
	if [ -e /dev/capi20 ]; then echo "direct capisuite-fax:/dev/capi20 \"Unknown\" \"CapiSuite (capisuitefax on /dev/capi20)\""; fi
	exit 0
fi

USER=$2
FROM=$USER

DEV=${DEVICE_URI#capisuite-fax:/dev/}
RES=""		# Default resolution is high; set this to -l for low resolution
NUMBER=""	# Use option "phone" as number by default

# If we find six arguments, the last one is the fax name; otherwise,
# we have to create a temporary file.

if [ $# -eq 6 ]; then
	FAXNAME=$6
else
	FAXNAME=$(mktemp </dev/null /tmp/fax.XXXXXX) || (echo "Failed to create temporary file" >>$LOGDIR/$DEV; exit 1)
	cat >$FAXNAME
fi

# Parse user-specified options from the PostScript file and set -l/-m
function getSelectedOption() {
	for i in $@; do
		case $i in
			\**) SELOPT=${i#[*]}; return ;;
		esac
	done
}

function parseOptions() {
	
	while read LINE; do 
		MAIN=${LINE%%:*}
		OPTIONS=${LINE##*:}
		getSelectedOption $OPTIONS

		case $MAIN in
			PageSize/*) echo "PAGE=$( echo -n $SELOPT | tr '[:upper:]' '[:lower:]' )" ;;
			Resolution/*) if [ $SELOPT == "204x98dpi" ]; then echo "RES=-l"; fi ;;
			Dial/*) if [ $SELOPT == "Manually" ]; then echo "NUMBER=-m"; elif [ $SELOPT == "JobName" ]; then echo "NUMBER=-j"; fi ;;
		esac
	done
}

eval $(lpoptions -p $PRINTER -l | parseOptions)
if [ "$NUMBER" == "-j" ]; then 
	NUMBER="$3"; 
fi

# Scan user options and set -l/-m (override previous choices if necessary)

for opt in $5; do
	case "$opt" in
		lowres*) RES="-n" ;;
		hires*) RES="" ;;
		manual*) NUMBER="-m" ;;
		jobname*) NUMBER="$3" ;;
		phone=*) NUMBER="${opt#phone=}" ;;
		media=*) PAGE=$( echo ${opt#media=} | tr '[:upper:]' '[:lower:]' ) ;;
	esac
done

echo 1>&2 # Apparently the first character emitted is somehow "eaten" by the reader

# do some cheking before continuing
if [ -z "$NUMBER" ]; then
	echo "ERROR: Empty phone number"
	exit 1
elif [ ! -x "$FAXPRG" ]; then
	echo "ERROR: $FAXPRG: executable not found"
	exit 1
fi

if echo "$NUMBER" | grep -q "." ; then
	NUMBER=${NUMBER%%.*}
fi

pushd $LOGDIR # If there are problem, capisuite-fax will generate a log file in the current directory
chown $USER $FAXNAME

sudo -u $USER $FAXPRG -d $NUMBER $FAXNAME > $LOGDIR/capisuitefax.log 2>&1

RC=$?

case $RC in
	0) echo "INFO: Fax sent" 1>&2 ;;
	# capisuitefax has no specific return values
        1) echo "ERROR: Could not send fax" 1>&2 ;;
esac

rm -f $FAXNAME.[0-9][0-9][0-9]
if [ $# -lt 6 ]; then rm -f $FAXNAME; fi

exit $RC
