#!/bin/sh 
# 
# fax - script to make, send, receive, view or print a fax
# Copyright Ed Casas 1993, 1994, 1995
# 
# --- Start of user configuration section --- 
# 
# Note: do not put spaces before or after the equal (=) signs.
#

# The name of the efax and efix executables, including full path
# if necessary.

EFAX=efax
EFIX=efix

# The device to which the fax modem is connected (e.g. ttya for
# /dev/ttya).  Use a dial-out (cua) device if sharing modem with
# dial-out programs.

DEV=cua1

# The type of fax modem commands to use.  Un-comment one of the
# following 3 lines depending on your modem.

# CLASS=1
CLASS=2
# CLASS=2.0		# doesn't work yet

# Your fax number in international format, 20 characters maximum.
# Use only digits, spaces, and the "+" character.

FROM="+1 800 555 2368"

# Your name as it should appear on the page header.

NAME="Put Your Name Here"

# The preferred page size for creating and printing faxes.
# Predefined values are "letter", "legal", "a4" and "A4".

PAGE=letter

# The type of printer. Use 'pcl' for HP-PCL or 'ps' for
# Postscript.  See definition of PRINT (below) for other printers
# and print options.

PRTYPE=pcl				# HP-PCL (e.g. HP LaserJet) 
# PRTYPE=ps				# Postscript (e.g. Apple LaserWriter)

# The command to print graphics (the fax image) from standard
# input.  Typically this is "lpr" or "lp".

PRCMD="lpr"

# The command to view a Portable Gray Map (PGM) image from the
# standard input.  Typically this is "xloadimage stdin" or "xv".

VIEWCMD="xloadimage stdin"
# VIEWCMD="pnmtoxwd | xwud"		# alternative
# VIEWCMD="xv -"			# alternative

# The name of the Ghostscript executable including full path if
# necessary.  Required only if sending Postscript files.

GS=gs

# Dial string prefix and suffix such as T for tone dialing, P for
# pulse dialing, 9 to get an external line, commas for delays or
# W to wait for dial tone.  See definition of TELCVT (below) if
# you have more complex requirements.

DIALPREFIX="T"
DIALSUFFIX=""

# The name(s) of lock file(s) according to your system's
# conventions. Protect with single quotes for delayed evaluation.
# Add a leading '#' to the file name for HDB (ASCII) format.

LOCK='-x /usr/spool/uucp/LCK..$DEV'		# "normal" systems
# LOCK=''					# no lock file
# LOCK='-x /usr/spool/locks/LK.047.040.011'	# SysV style names
# LOCK='-x #/usr/spool/uucp/LCK..$DEV'		# HDB format (ASCII pid)
# LOCK='-x /usr/spool/uucp/LCK/LCK..$DEV'	# NeXT

# Uncomment one of the following lines to force xon/xoff flow
# control only if you have one of the modems listed.

# FCINIT="-i\Q4"		# AT&T modems (Dataport, Paradyne)
# FCINIT="-i&H2&R1&I2"		# USR modems (Courier, Sportster)

# ****************************************************************
# The remaining options probably won't need to be changed.
# ****************************************************************

# The fax script file name. "$0" should almost always work.

FAX=$0

# Message verbosity.  Use "chewmainr" for verbose logging when
# analyzing or reporting problems.

VERB="ewin"			# default: errors, warnings, info, negotiation
# VERB="chewmainr"		# everything

# A unique name for logs and received files.  `date +%m%d%H%M%S`
# works on most systems.

TSTAMP=`date +%m%d%H%M%S`
# TSTAMP=`date | tr ' ' '-'`	# alternative - readable date/time
# TSTAMP=$$			# alternative - process number

# Shell command to convert names to phone numbers when sending
# faxes.  At execution $1 will be the name and $f the file name
# to search.  The example below uses a directory file where alias
# lines start with the keyword "fax" followed by the alias in
# parentheses and a colon.  The remainder of the line is taken to
# be the phone number. Other lines are ignored.  For example, if
# one of the files in DIRFILES (defined below) contained the line
# "fax(kpmg): 691-3031", you could use the command "fax send kpmg
# invoice.24".

LOOKUP='eval sed -n -e "/^fax($1):/{" -e "s/^[^:]*://p" -eq -e"}" $f'

# List of telephone directory file(s) to be searched.  The
# default is the file .faxdir in the user's home directory.

DIRFILES="${HOME:-.}/.faxdir"

# Shell command to modify phone numbers.  This can be used to
# access long distance or alternate carriers, add passwords or
# accounting digits, etc.  In the examples below this is used to
# convert numbers beginning with '+'; the first substitution
# handles same-country calls and the second handles international
# calls.

TELCVT='sed -e s/+1/1/ -e s/+/011/'	# North America
# TELCVT='sed -e s/+44/0/ -e s/+/010/'	# UK
# TELCVT='sed -e s/+852// -e s/+/001/	# Hong Kong
# TELCVT='sed -e s/+33// -e s/+/19W/'	# France (?)
# TELCVT='sed -e s/+1/10288/'		# use AT&T
# TELCVT='sed -e s/+/T82W1682W9W/'	# get out of PBX

# efix options for text-to-fax conversion. -d0.5,0 sets 1/2 inch
# left margin. -l66 gives 66 lines of text per page.

TEXTMARGIN="-d0.5,0 -l66"

# Scaling of the built-in 8x16 built-in font when generating text
# pages.  Vertical scaling by 2 (or 1 at low resolution) gives a
# 12-point font that fits 66 lines on 11 inch paper.

TEXTSIZEhigh="-s2.0x2.0"
TEXTSIZElow="-s2.0x1.0"

# The font files and scaling to use for text-to-fax conversion.
# Comment these out to use the built-in font.

# FONTDIR=`dirname $FAX`			# use appropriate directory
# TEXTSIZEhigh="-f $FONTDIR/efaxfont.h -s 1.0x1.0"
# TEXTSIZElow=" -f $FONTDIR/efaxfont.l -s 1.0x1.0"

# Dimensions of page sizes.  Add any others you like.

PAGE_letter="8.465x11in"	# fax width x letter length
PAGE_legal="8.465x14in"		# fax width x legal length (?)
PAGE_A4="21x29.7cm"		# ISO A4 (?)
PAGE_a4="$PAGE_A4"

# When the print and view commands below are executed, the value
# of $RES will have one of two resolution-dependent values
# (e.g. VIEWhigh or VIEWlow).  $PAGEDIM will have one of the
# above PAGE_x values. Put single quotes around the PRINT and
# VIEW commands and use $f for the input file name.

# Command and options to convert a fax file to printable format.
# Change the scaling and displacement options if your printer
# can't print received faxes at full scale or at the default
# origin.  For printers other than Postscript or PCL select an
# appropriate PRTYPE above (e.g. pbm or ps) and insert the
# appropriate filter (e.g. pbmtoepson or gs).

PRINT='$EFIX -ve -p$PAGEDIM $RES -o$PRTYPE $f'

PRINThigh="-s1.471x1.515 -r300 -d0,0.125" # convert 204x196 (fax) to 300 dpi
PRINTlow=" -s1.471x3.125 -r300 -d0,0.125" # and shift down 1/8"

# PRINT='$EFIX -ve -p$PAGEDIM $RES -o$PRTYPE $f | pbmtoepson'
# PRINThigh="-s0.29x0.36 -r72" # convert 204x196 (fax) to 60x72 dpi
# PRINTlow=" -s0.29x0.72 -r72"

# Command to convert a fax file to PGM format for previewing (pgm
# conversion decimates by 4).

VIEW='$EFIX -ve -p$PAGEDIM $RES -opgm $f'

VIEWhigh="-s1x1"			# faster, whole-page view
VIEWlow=" -s1x2"
#VIEWhigh="-s1.5x1.5 -r306x294"		# slower, readable size
#VIEWlow=" -s1.5x3.0 -r306x294"

# Commands to set up modem.  "-iZ -i&FE0Q0V1X4&D2S7=120 -i&C0"
# sets up almost all modems.  See the efax(1) man page for
# details.

INIT="-iZ -i&FE0Q0V1X4&D2S7=120 -i&C0"

# Command(s) to reset modem when efax finishes. "-zZ" works in
# almost all cases.

RESET="-zZ"
# RESET="-zZ -z&F+FCLASS=0"	# for modems that stay in fax mode after reset

# Speaker mode(M) and loudness(L). Mn sets speaker mode where n
# means: 0:never, 1:until carrier, 2:always, 3:on receive only.

SPKR="-iM1L0"

# Initialization commands for different command sets.  For Class
# 1: "-i+FCLASS=1 -o1".  For Class 2: "-i+FCLASS=2 -i+FCR=1".
# Almost all Class 2 modems also need the -or option to reverse
# the bit order on receive.

CLASS1INIT="-i+FCLASS=1 -o1"			# Class 1
CLASS2INIT="-i+FCLASS=2 -i+FCR=1 -or"		# Class 2
CLASS20INIT="-i+FCLASS=2.0 -i+FCR=1 -o0"	# Class 2.0 (doesn't work)

# The modem's hardware/software capabilities for sending faxes
# excluding the "vr" (first) field.  "3,0,0,0,0,0,0" should
# almost always work. The first digit can be 5 for a 14,400 bps
# fax modem.  See the efax(1) man page for a description of the
# fields.

TXCAP="3,0,0,0,0,0,0"

# Capabilities for receiving faxes.  Usually the same as TXCAP.
# If your modem only receives at 4800 bps use "1,0,0,0,0,0,0".

RXCAP="$TXCAP"

# Additional options required only for transmit or only for
# receive.  None normally required.

RXINIT=""
TXINIT=""

# Command to make a date for the page header. Protect with single
# quotes.  'date "+%y/%m/%d %H:%M"' works on most systems.

HDRDATE='date \"+%y/%m/%d %H:%M\"'	# YY/MM/DD HH:MM (24hour)
# HDRDATE='date'			# longer, more readable

# Page header format.  You may use $DATE, $NAME, $FROM, $TO, and
# "%d/%d" (for page number and count).  Protect with single
# quotes.  Example: '$DATE $NAME ($FROM) --> $TO p. %d/%d'.

HDR='$DATE  $NAME ($FROM)  -->  $TO  p. %d/%d'

# BUSYRETRIES is a list of delays (in seconds) between attempts
# to redial busy numbers.  Comment out if you don't want to retry
# busy numbers.

BUSYRETRIES="30 60 120 300 60 600 60 60 1200 60 60"

# A list of signals to be ignored by the script so efax can
# terminate gracefully.  HUP (1), INT (2), QUIT (3), and TERM
# (15) are probably enough.

SIGNALS="1 2 3 15"

# Command to run another program (efax) at a higher-than-normal
# scheduling priority.  This command isn't used if it fails
# (e.g. because the current user isn't privileged).  Comment this
# out if it causes problems.

NICE="nice -n -10"

# Standard versions of commands that are often aliased.

RM="/bin/rm"
LS="/bin/ls"

# ****************************************************************
# The remaining configuration options apply only to the `fax
# answer' command.  You can ignore these if you will only be
# running efax manually.  See "USING INIT TO RUN EFAX" in the
# efax man page for more information.
# ****************************************************************

# device or file where fatal error messages should be written

CONSOLE=/dev/console

# The directory to store incoming faxes and log files.  This directory
# should already exist and be writable by the user(s) of this script.

FAXDIR=/usr/spool/fax

# The strftime(3) pattern that generates the file name for received files.
# At 10:45:36 on February 25, for example, "%m%d%H%M%S" would produce
# 0225104536, "%j-%H%M" would produce 056-1045, and %d%b%H%M 25Feb1045.

ANSFNAME="%d%b%H%M%S"

# umask for received files. Use 022 to allow anyone to retrieve faxes.

UMASK=022

# The command to execute when a fax is received.  Normally this
# sends someone e-mail or prints the faxes.  Protect with single
# quotes.  The variable FILES will contain the names of the
# received files and RESSWITCH will be -l if the file was
# received at low resolution.  Comment this out to do nothing.

NOTIFY='mail -s "received fax files $FILES on `date`" $FAXMGR <$logfile'
# NOTIFY='lpr $logfile ; $FAX print $RESSWITCH $FILES'

# The user to be sent mail when a fax is received.

FAXMGR=`whoami`

# The number of rings to wait before answering.

ANSRINGS=1

# Initialization options to enable fax/data adaptive answer (AA).
# You must also define GETTY (below) to enable AA.  NOTES: (1) AA
# does not work properly on some (2400/9600) modems unless the
# modem initialization is done at 2400 bps (not possible with
# efax). (2) I have been unable to make AA work with my Zoom
# modem in Class 1.  Note that USR modems do not support modem
# adaptive answer (+FAE=) in Class 1.

# DATAINIT=""				# Class 1 no adaptive answer
# DATAINIT="-i+FAE=1"			# Class 1 modem adaptive answer
# DATAINIT="-i+FCLASS=0 -oa"		# Class 1 software adaptive answer
# DATAINIT="-i+FAA=0"			# Class 2 no adaptive answer
# DATAINIT="-i+FAA=1"			# Class 2 modem adaptive answer
# DATAINIT="-i+FAA=0 -i+FCLASS=0 -oa"	# Class 2 software adaptive answer

# Argument passed to exec(2) of "/bin/sh -c" for incoming data calls.  This
# command will usually exec getty(8).  Up to 6 %d arguments are replaced by
# the baud rate following the CONNECT response from the modem (or 300 if
# none).  Use 'nice' if required to reduce any special priority set by
# NICE. Comment out if you don't want to handle data calls.

# GETTY="exec /sbin/getty -h $DEV %d"		# for getty_ps (Linux)

#
# --- End of user configuration section ---
#

# --- set any variables given on command line

while : ; do
	case $# in 0) break ;; esac
	case "$1" in [A-Z]*=*) eval $1 ; shift ;; *) break ;; esac
done

# -------- initialize 

ERR=0
logfile=${TSTAMP}.log

$NICE true 2>/dev/null ; case $? in 0) ;; *) NICE="" ;; esac

eval LOCK=\"$LOCK\"			# resolve dependency on DEV

eval DEVN=`echo $DEV|sed -e s./._.`	# make device name w/o directories

eval PAGEDIM=\"\$PAGE_$PAGE\"		# resolve dependency on PAGE

case "$CLASS" in
	2.0) CLASSINIT="$CLASS20INIT"	;;	# Class 2.0
	2) CLASSINIT="$CLASS2INIT"	;;	# Class 2
	1) CLASSINIT="$CLASS1INIT"	;;	# Class 1
	*) echo "bad CLASS=$CLASS" ; exit 2 ;;
esac

# --- check for a command and optional resolution flag

res=high
VR=1
case $# in 
	0) cmd=receive ;; 
	1) cmd=$1 ; shift ;; 
	*) cmd=$1 ; shift ; 
		case $1 in -l) RESSWITCH=-l ; res=low ; VR=0 ; shift ;; 
	esac ;; 
esac

# -------- do the appropriate command

while : ; do 	# so we can use `break' to get to the end of the script

case $cmd in 

# fax answer : clean up logs and exec efax. normally run by init(8).

	answer)

	if cd $FAXDIR ; then :
	else
		echo "Error: $FAX cannot cd to $FAXDIR" >>$CONSOLE ; sleep 30
		break
	fi

	while [ -f ${DEVN}.stop ] ; do sleep 15 ; done

	umask $UMASK

	for f in ${DEVN}.[0-9]*   	# clean up old log files
	do
	  if [ ! -r $f ] ; then continue ; fi
	  if grep "done, returning [145]" $f >/dev/null ; then 
		true  # ignore `signal,' `locked,' and `no modem' exits
	  else
		FILES=`sed -n -e '/received/s/[^>]*>\([^(]*\).*/\1/p' $f`
		FILES=`echo $FILES`
		logfile=$f 
		grep "session\ *98lpi" $logfile >/dev/null
		case $? in 0) RESSWITCH=-l ;; esac
		eval $NOTIFY
		echo >>${DEVN}.log
		cat $f >>${DEVN}.log
	  fi
	  $RM -f $f
	done	

	exec $NICE $EFAX -d/dev/$DEV -v"$VERB" $LOCK $INIT $SPKR \
	$CLASSINIT $FCINIT $RXINIT \
	-c "$VR,$RXCAP" -l "$FROM" $RESET \
	$DATAINIT -g "$GETTY" \
	-iS0=$ANSRINGS -w -s -r "$ANSFNAME" \
	> ${DEVN}.$$ 2>&1

	echo ERROR: $FAX answer exec failed >>$CONSOLE ; sleep 30

	break
	;;


# fax wait : runs fax answer (emulates init(8) but PPID != 0)

	wait)
	while : ; do
		echo running $FAX answer
		$FAX answer
	done
	break
	;;


# fax queue : list received fax files

	q*)

	cd $FAXDIR ; 
	case $? in 0) ;; *) echo "cannot cd to $FAXDIR" ; break ;; esac

	if $LS [0-9]*.[0-9][0-9][0-9] >/dev/null 2>/dev/null
	then
		echo 
		echo Fax files in `pwd` :
		echo
		$LS -l [0-9]*.[0-9][0-9][0-9]
		echo
	fi

	break 
	;;	

# fax start/stop/status : manage fax receive daemon

	start | stop | st* )		# common section

	cd $FAXDIR ; 
	case $? in 0) ;; *) echo "cannot cd to $FAXDIR" ; break ;; esac

	if $LS ${DEVN}.[0-9]* >/dev/null 2>/dev/null ; then
		logfile=`$LS ${DEVN}.[0-9]*|tail -1`
	else
		echo no fax answer process for device $DEV
		break 
	fi

	efaxpid=`echo $logfile | sed -e "s/${DEVN}\.//g"`

	case $cmd in

# fax start - remove stop file so fax answer will continue

	start)

	if [ ! -w . ] ; then echo "can't write  `pwd`" ; break ; fi
        $RM -f ${DEVN}.stop
	break 
	;;

# fax stop - make a stop file and kill current fax answer daemon

	stop)

	if [ ! -w . ] ; then echo "can't write `pwd`" ; break ; fi
	touch ${DEVN}.stop
	echo stopping fax daemon for $DEV, pid=$efaxpid
	kill -TERM $efaxpid
	break 
	;;


# fax status - display pid and log file for current daemon

	st*)

	if [ -f ${DEVN}.stop ] ; then stat="(set to stop)" ; fi
	echo "fax receive daemon for $DEV ${stat} ( log: $FAXDIR/${logfile} )"

	echo
      	if ps -u $efaxpid 2>/dev/null ; then :
      	else
		echo "NOT ACTIVE (last was process $efaxpid)"
      	fi

	echo
	egrep "Warning|Error|starts|activity|opened|received|done" $logfile

	case $# in 
	0) ;; *) echo "---------------" ; sleep $1 ; exec $FAX status $1 ;;
	esac

	break
	;;

	esac	# common section
	;;

# fax make : convert a text or Postscript file to fax format

	m*)

	case $# in 0) echo "No files specified" ; ERR=2 ; break ;; esac

	if [ ! -r $1 ] ; then echo "Can't read $1" ; ERR=2 ; break ; fi

	read x <$1
	case $x in
	%!*)	echo $1 is postscript
		$GS -q -sDEVICE=dfax$res -dNOPAUSE \
		-sOutputFile="|dd bs=32 skip=1 of=$1.%03d 2>/dev/null" \
		$1 </dev/null >/dev/null
		;;
	*)	echo $1 is text
		eval $EFIX -ve -itext -p$PAGEDIM $TEXTMARGIN \
			\$TEXTSIZE$res -n $1.%03d $1
		;;
	esac

	break
	;;

# fax send/poll : fax files to given number, converting first if necessary

	s*|poll)
	
	case $cmd in
	poll) 
	case $# in
	0) echo "missing phone number to poll" ; ERR=2 ; break ;;
	esac
	;;
	s*) 
	case $# in
	0|1) echo "missing phone number or file name(s)" ; ERR=2 ; break ;;
	esac
	;;
	esac

	# look up names

	case $1 in [A-Za-z]*) 
		for f in $DIRFILES ; do
			if [ -r $f ] ; then TELNO=`$LOOKUP` ; fi
			case "x$TELNO" in x) continue ;; *) break ;; esac
		done
		case "x$TELNO" in
			x) echo "Name lookup for $1 failed" ; ERR=2 ; break ;;
			*) echo "Lookup: $1 = $TELNO" ;;
		esac
		;;
		*) TELNO="$1" ;;
	esac

	case "x$TO" in x) TO="$TELNO" ;; *) ;; esac

	TELNO=`echo $TELNO|sed "s/[ 	()]*//g"`

	# handle manual dialing and number->dial string conversions

	case "$TELNO" in
	-m*)	MANINIT="-iX3" ; TELNO="" ; shift ;;
	+*)	eval TELNO=\"`echo $TELNO | $TELCVT`\" ; shift ;;
	*) 	TELNO="$DIALPREFIX$TELNO$DIALSUFFIX" ; shift ;;
	esac

	case $cmd in
	poll) 
	efaxcmd="-p"
	FILES="$*"
	;;

	*) 
	efaxcmd="-t"

	# use `fax make' to convert files if they need to be updated

	FILES=""
	for f in $* ; do
		if [ ! -r $f ] ; then
	      		echo "can't read file $f" ; ERR=2 ; break 2 
		fi
		case $f in
		*.[0-9][0-9][0-9]) FILES="$FILES $f" ;;	# skip image files
	   	*)	if echo ${f}.001: $f \; x | make -r -q -f - ; then
				echo ${f}.nnn is up-to-date
			else
	 			$RM -f ${f}.[0-9][0-9][0-9]
	 			/bin/sh $FAX make $RESSWITCH $f
			fi
			if [ -r $f.001 ] ; then 
                		FILES="$FILES $f.[0-9][0-9][0-9]"
	      		else		# something's wrong, catch it later
	        		FILES="$FILES $f.001"
	      		fi
			;;
		esac
        done

	# check that all files are OK

	for f in $FILES ; do
	   if [ ! -r $f ] ; then
	      echo "can't read file $f" ; ERR=2 ; break 2 
	   fi
	done

	;;
	esac

	# send it

	for t in 0 $BUSYRETRIES ; do

		case $t in 
		0) ;; *) echo "Will try again in $t seconds" ; sleep $t ;;
		esac

		eval DATE=\"\`$HDRDATE\`\"
		eval "HDR=\"$HDR\""

		( trap "" $SIGNALS ; \
		$NICE $EFAX -d/dev/$DEV -v "$VERB" $LOCK $INIT $SPKR \
		$CLASSINIT $FCINIT $TXINIT \
		-c "$VR,$TXCAP" -l "$FROM" $RESET \
		-h "$HDR" \
		$MANINIT $efaxcmd "$TELNO" $FILES \
		2>&1 | tee $logfile )

		if grep Error: $logfile >/dev/null ; then 
			echo "There were errors (this log file is ${logfile})."
			ERR=2 ; break
		fi

		if grep "is\ busy" $logfile >/dev/null ; then
			echo Busy. ; ERR=1
		else
			ERR=0 ; break
		fi

	done

	break 
	;;

# fax rm : delete files, possibly from spool directory

	rm)
	
	if $LS -l $FAXDIR/$1 2>/dev/null ; then cd $FAXDIR ; fi
	$RM $1
	break
	;;

# fax [receive] : answer phone now and receive fax files

	r*)
	
	case $# in 0) ;; *) TSTAMP=$1 ;	logfile=${TSTAMP}.log ;; esac

	( trap "" $SIGNALS ; \
	$NICE $EFAX -d/dev/$DEV -v "$VERB" $LOCK $INIT $SPKR \
	$CLASSINIT $FCINIT $RXINIT \
	-c "$VR,$RXCAP" -l "$FROM" $RESET \
	-r ${TSTAMP} \
	2>&1 | tee $logfile )

	if grep Error: $logfile >/dev/null ; then
		echo "There were errors (this log file is ${logfile})."
		ERR=2 ; break 
	fi

	if grep "session\ *98lpi" $logfile >/dev/null ;	then
		echo " NOTE --> "
		echo " NOTE --> ${TSTAMP}.nnn received at low resolution."
		echo " NOTE --> "
		echo " NOTE --> Use -l option to print, view or to re-send "
		echo " NOTE --> "
	fi

	break
	;;

# fax cut : cut out part of a  fax

	cut)
	case $res in low) RES=204x98 ;; *) RES=204x196 ;; esac
	case $# in 0|1|2|3|4) 
		echo "missing argument(s)" 1>&2
		cmd=help ; continue ;; 
	esac
	X=$1 ; shift ; Y=$1 ; shift ; W=$1 ; shift ; H=$1 ; shift
	case "$1" in mm|"in"|cm|pt) UNITS=$1 ; shift ;; esac
	$EFIX -d -${X},-${Y}$UNITS -p ${W}x${H}$UNITS -r$RES $1
	break
	;;


# fax paste : paste one fax into others

	paste)
	case $res in low) RES=204x98 ;; *) RES=204x196 ;; esac
	case $# in 0|1|2|3) 
		echo "missing argument(s)" 1>&2
		cmd=help ; continue ;; 
	esac
	X=$1 ; shift ; Y=$1 ; shift ; 
	case "$1" in mm|"in"|cm|pt) UNITS=$1 ; shift ;; esac
	OVFILE=$1 ; shift
	for f in $* ; do
		echo "$f ..."
		if $EFIX -d ${X},${Y}$UNITS -r$RES $OVFILE | \
			$EFIX -O- $f >efax.tmp
		then
			mv efax.tmp $f
		fi
	done
	break
	;;


# fax print : print fax files, possibly from spool directory

        p*) 

	if $LS $FAXDIR/$1 2>/dev/null ; then cd $FAXDIR ; fi
	eval RES=\$PRINT$res
	for f in $*
	do
  		echo "$f ... "
		eval "$PRINT | $PRCMD"
	done
	break
	;;

# fax view : display image, possibly from spool directory  

	v*)
	
	if $LS $FAXDIR/$1 2>/dev/null ; then cd $FAXDIR ; fi
	eval RES=\$VIEW$res
	for f in $* 
	do
  		echo "$f ... "
		eval "$VIEW | $VIEWCMD"
	done
	break
	;;


# fax new : create a cover page for a fax (needs work)

	new)

	fname=${1-new.fax}
	DATE=`date "+%B %d %Y"`
	cat >$fname 2>/dev/null << EOF

	________________________________________________

			 FAX COVER PAGE
	________________________________________________

	 To:	x
	fax:

	________________________________________________

	From:	$NAME
	 fax:	$FROM

	Date:	$DATE

	Pages:	1 (including this page)

	________________________________________________

EOF
	${VISUAL-{EDITOR-vi}} $fname

	break
	;;

# fax test : make list of shell variables/values and modem features

	t*)

	sed -n -e '/^# --- End/q' -e '/^[A-Z]*\=/p' $0 

	case "$CLASS" in
	2.0) 
	CMDS=" \
	-i+FAA=?  -i+FAA? -i+FBO=?  -i+FBO? \
	-i+FPI=?  -i+FPI? -i+FCQ=?  -i+FCQ? \
	-i+FCR=?  -i+FCR? -i+FCC=?  -i+FCC? \
	-i+FCS=?  -i+FCS? -i+FIS=?  -i+FIS? \
	-i+FET=?  -i+FET? -i+FID=?  -i+FID? \
	-i+FMM=?  -i+FMM? -i+FCT=?  -i+FCT? \
	-i+FPS=?  -i+FPS? -i+FMR=?  -i+FMR? \
	-i+FMI=?  -i+FMI? -i+FSP=?  -i+FSP? \
	-i+FPP=?  -i+FPP? "
	;;
	2)
	CMDS=" -i+FAA=?  -i+FAA? \
	-i+FAXERR=?  -i+FAXERR?  -i+FBADLIN=?  -i+FBADLIN? \
	-i+FBADMUL=?  -i+FBADMUL?  -i+FBOR=?  -i+FBOR? \
	-i+FBUF?  -i+FBUG=?  -i+FBUG?  -i+FCIG=?  -i+FCIG? \
	-i+FCQ=?  -i+FCQ?  -i+FCR=? -i+FCR?  -i+FDCC=?  -i+FDCC? \
	-i+FDCS=?  -i+FDCS?  -i+FDIS=? 	-i+FDIS?  \
	-i+FDT=?  -i+FECM=?  -i+FECM? \
	-i+FET=?  -i+FET?  -i+FLID=?  -i+FLID?  -i+FMDL? -i+FMFR? \
	-i+FMINSP=?  -i+FMINSP? -i+FPHCTO=?  -i+FPHCTO?  \
	-i+FPTS=? -i+FPTS?  -i+FREL=? -i+FREL? -i+FREV? "
	;;
	1) 
	CMDS="\
	-i+FAE=? -i+FAE? \
	-i+FTM=? -i+FRM=? \
	-i+FTH=? -i+FRH=? \
	-i+FTS=? -i+FRS=? "
	;;
	esac

	for i in 1 2 3 ; do echo "Please wait..." 1>&2 ; sleep 2 ; done

	$EFAX -vewica -d/dev/$DEV $LOCK $INIT $SPKR \
	$CLASSINIT $FCINIT $TXINIT \
	-iI0 -iI1 -iI3 -iI4 \
	-i+FCLASS=? -i+FCLASS? \
	$CMDS \
	-i"&V" -i "" -i "" -i "" -i "" -i "" -z"Z" -oe -T 2>&1

	echo "Done." 2>&1
	break
	;;	

# fax help : show command arguments

	-\?|\?|-h*|h*)

cat 1>&2 <<EOF
 Usage: 

   fax [ r[eceive] [-l] [filename-prefix] ]
   fax m[ake] [-l] { postscript-file-name | text-file-name }
   fax s[end] [-l] { -m | telephone-number } filename...
   fax { p[rint] | v[iew] | rm } [-l] filename...
   fax cut x y w h [in|mm|cm|pt] filename 
   fax paste x y [in|mm|cm|pt] filename filename...
   fax test

   fax [ stop | start | st[atus] | q[ueue] ]
   fax wait
   fax answer [-l]

 use -l for low (96 lpi) resolution
 use -m if the number has been dialed manually 	

 if given no arguments, answers the phone and receives a fax.

 use VAR=value to set variables (e.g. "fax DEV=cua2 receive")

 session logs are written to a file with the date/time as the
 file name and extension of .log (except for automatic reception).

 fax device is /dev/$DEV, incoming spool directory is $FAXDIR

EOF

	ERR=1
	break
	;;

	*)
	echo "Error. Invalid command ($cmd)" ; ERR=2 ; break
	;;

esac	
done

exit $ERR
