# ****************************************************************
# --- Start of faxserver processing ---
# ****************************************************************

# --- 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

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

# -------- resolve dependencies on command-line arguments

eval LOCK=\"$LOCK\"			# depends on DEV

# make device name w/o directories

case $DEV in
	*/*) DEVN=`echo $DEV|sed -e s./._.g` ;;
	*) DEVN=$DEV ;;
esac

case $CLASS in
	2.0) 	CLASSINIT="$CLASS20INIT" ;;	# Class 2.0
	2)   	CLASSINIT="$CLASS2INIT"	 ;;	# Class 2
	1)   	CLASSINIT="$CLASS1INIT"	 ;;	# Class 1
	*)   	echo "Error: CLASS=\"${CLASS}\" not valid."  ; exit 2 ;;
esac

# fax send : fax files to given number

for FAX in `ls $SPOOLDIR/ `; do
  BASEDIR=$SPOOLDIR/$FAX

  if [ -r $BASEDIR/faxinfo ]; then

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

	case "$TO" in '') TO="$TELNO" ;; *) ;; esac
	
	TELNO=`grep "ToFaxNumber:" $BASEDIR/faxinfo | sed -e 's/ToFaxNumber://'`
	TELNO=`echo $TELNO|sed "s/[ 	()][ 	()]*//g"`

	case $TELNO in 
	'') 	;; 
	*)	TELNO="${DIALPREFIX}${TELNO}${DIALSUFFIX}" ;; 
	esac
	
# Read User info data from faxinfo

EMAIL=`grep "EmailAddress:" $BASEDIR/faxinfo | sed -e 's/EmailAddress://' `

if [ "$IGNOREUSERFROM" ]; then
 FROM="$DEFAULTFROM"
else
 FROM=`grep "FromFaxNumber: " $BASEDIR/faxinfo | sed -e 's/FromFaxNumber: //' `
 if [ ! "$FROM" ]; then
   FROM="$DEFAULTFROM"
 fi
fi

if [ "$IGNOREUSERHDRADDR" ]; then
 HDRADDR="$DEFAULTHDRADDR"
else
 HDRADDR=`grep "HeadAddress: " $BASEDIR/faxinfo | sed -e 's/HeadAddress: //' `
 if [ ! "$HDRADDR" ]; then
   HDRADDR="$DEFAULTHDRADDR"
 fi
fi

        FILES="$BASEDIR/g3fax_*"
	
	# check that all files are OK

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

	# send it

	echo Now processing directory: $BASEDIR from $EMAIL ...
	
	logfile=$BASEDIR/`date +%m%d%H%M%S`.log
	statfile=$BASEDIR/`date +%m%d%H%M%S`.stat

	for t in 0 $BUSYRETRIES ; do

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

		DATE=`eval "$HDRDATE"`
		eval HDR=\"$HDRCMD\"
		
		echo | tee -a $logfile
		
		($NICE $EFAX -v "$VERB" \
		-d/dev/$DEV $LOCK $INIT $SPKR \
		$CLASSINIT $FCINIT $TXINIT \
		-c "$TXCAP" -l "$FROM" $RESET -h "$HDR" \
		-t "$TELNO" $FILES 2>&1; echo $? > $statfile) | tee -a $logfile

		ERR=`cat $statfile`
		echo "Status: $ERR"
		
		echo | tee -a $logfile
		case $ERR in
		0)	echo "Fax from $EMAIL delivered successfully to $TELNO" |\
			  tee -a $logfile
			break 2 ;;
		1)	echo "$TELNO currently busy" | tee -a $logfile
			;;
		*)	echo "There were errors (see ${logfile})." | tee -a $logfile
			;; # retry just as with busy
		esac
	done # busyretries
	break
    done # endless while loop
    
    # Send logfile by email if requested:
    # "Debug" will send an email after every attempt,
    # "True" only if attempt was successful.
    
    if grep "Email: Debug" $BASEDIR/faxinfo >/dev/null; then
    	mailx -s faxmessage $EMAIL < $logfile
    fi
    
    # Next routine removes a fax if it couldn't be sent for more than
    # MAXTRY times and then mails the user a warning.
    
    TRYCOUNT=`ls $BASEDIR/*.log | wc -l`
    if [ $TRYCOUNT -gt $MAXTRY -a $ERR -ne 0 ]; then
	echo | tee -a $logfile
	echo "Fax from $EMAIL failed: too many retries ($MAXTRY)" |tee -a $logfile 
	echo "Please check the faxnumber ($TELNO) and try again." |tee -a $logfile 
	echo "All faxfiles are available in $BASEDIR on your server." |tee -a $logfile 
        mailx -s "fax failed" root < $logfile	
        mailx -s "fax failed" $EMAIL < $logfile	
	mv $BASEDIR/faxinfo $BASEDIR/failinfo
    fi

    # remove faxdir if delivery was successfull
    if  [ $ERR -eq 0 ]; then
       if grep "Email: True" $BASEDIR/faxinfo >/dev/null; then
  	  mailx -s faxmessage $EMAIL < $logfile
       fi
       rm -rf $BASEDIR
    fi
  fi  # only if faxinfo exists
done
