# ****************************************************************
# --- Start of faxdirect processing ---
# ****************************************************************

# Read User info data from faxinfo

BASEDIR=$2

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

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

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

	case $# in
	0) echo "missing phone number to call" ; ERR=2 ; break ;;
	esac
	
	# look up names

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

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

	case $TELNO in 
	'') 	;; 
	*)	TELNO="${DIALPREFIX}${TELNO}${DIALSUFFIX}" ;; 
	esac
	
	EMAIL=`grep "EmailAddress:" $BASEDIR/faxinfo | sed -e 's/EmailAddress://' `
	
        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 -e "\nStarting to dial...\n" | 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

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

echo "Fax script finished .."

exit $ERR

