#!/bin/sh
# mgetty newfax script package Copyright(c) 1998 Tycho Softworks.
# $Id: newfax 1.2 Tue, 23 Jun 1998 03:30:07 -0400 dyfet $
#
# A newfax system for RedHat Linux based on samples and scripts from
# mgetty distribution.  Please note that I assume /usr/sbin/newfax is
# the entry point for 'new_fax', rather than the more archiac
# /usr/local/bin/new_fax normally used by mgetty.  This I feel is more
# consistent with the intent of the Linux fnstd, and it's implimentation
# under RedHat Linux, though an appropriate symlink can be created.

DIST=/etc/mgetty+sendfax/faxdist
PATH=/usr/sbin:$PATH
PRINTERS=` grep "^[a-z]*:" /etc/printcap | cut -d: -f1`

log()
{
	echo `date +"%m/%d/%y %H:%M"` $* >>/var/log/faxlog
}

# take the fax informations and log status line
status="$1"
id="$2"
pages="$3"
shift 3

if test -z "$id" ; then
	id='_???_'
fi

# get name and distribution of station id
dest=`grep ^"$id": $DIST | cut -d: -f 2,3`
if test -z "$dest" ; then
	name="$id"
	dest=`grep ^\*: $DIST | cut -d: -f 3`
else
	name=`echo $dest | cut -d: -f1`
	dest=`echo $dest | cut -d: -f2,3`
fi

# if no default faxdist entry or a blank destination, then we reject the
# fax.

if test -z "$dest" ; then
	log "fax rejected (from $name, $pages page(s), status $status)"
	if test ! -z "$*" ; then
		rm $*
	fi
	exit 0
fi

# if the testination is a named printer, we send it to the printer rather
# than mail it.  This may cause a problem for users named the same as
# printers, but this represents an unlikely scenario.

if test ! -z "$PRINTERS" ; then
	for printer in $PRINTERS ; do
		if test "$dest" = "$printer" ; then
			log "fax spooled to $printer (from $name, $pages page(s), status $status)"	
			if test ! -z "$*" ; then
				newfax2hplj $* | lpr -P$printer 2>&1 >/dev/null
				rm $*
			fi
			exit 0
		fi
	done
fi

log "fax mailed to $dest (from $name, $pages page(s), status $status)"
newfax2mime $* | sendmail -f"$name <$id@fax>" $dest 2>&1 >/dev/null
if test ! -z "$*" ; then
	rm $*
fi
exit 0	
