Erun() {

	EV=$1
	shift
	if ! $@ ; then	
		echo "$@ fails" >&2
		exit $EV
	fi

}

Cleanup() {

	Erun 2 cd $SPOOLD
	Erun 2 rm -rf $WD

}

Parse_Args() {

	while [ $# -ne 0 ] ; do
	    k=$(echo $1 | cut -c2-2)
	    case $k in
	    j)
		JOB="$(echo $1 | cut -c3- | awk '{printf("%03d",$1)}')"
	    ;;
	    H)
		HOST=$(echo $1 | cut -c3-)
	    ;;
	    C)
		CLASS=$(echo $1 | cut -c3-)
	    ;;
	    J)
		NUMBER=$(echo $(echo $1 |cut -c3-) | cut -f1 -d\ )
	    ;;
	    P)
		PRINTER=$(echo $1 |cut -c3-)
	    ;;
	    L)
		USER=$(echo $1 |cut -c3-)
	    ;;
	    k)
		CONTROL_FILE=$(echo $1 |cut -c3-)
	    ;;
	    a)
		ACCOUNTING_FILE=$(echo $1 |cut -c3-)
	    ;;
	    *)
	    ;;
	    esac
	    shift 1
	done

}

Get_Lpd_Var() {

    local SRCH="\<$1[:space:]*=[:space:]*"
    test -r /etc/lpd.conf && 
	cat /etc/lpd.conf | grep "$SRCH" | 
	sed "s,.*$SRCH\([[:alnum:][:punct:]]*\).*,\1,g" | tail -1l

}

Server_Queues() {

    test -r /etc/lprfax/printcap && 
	cat /etc/lprfax/printcap | awk '{
	if (substr($0,length($0),1) == "\\") {
	    $0=substr($0,1,length($0)-1);
	    printf("%s",$0);
	} else
	    print;
	}' | tr -d ' \009' | awk -F: '{if ($1 != "faxint") next;
		for (i=2;i<=NF;i++)
		    if (substr($i,1,2)=="sv")
			print $i;
	}' | cut -f2 -d=

}

Get_Faxid() {

    test -r /etc/mgetty/sendfax.config && 
	cat /etc/mgetty/sendfax.config | awk '{
	if ($1 == "port") {
	    if ($2 == "'$1'")
		i=1;
	    else	       
		i=0;
	    next;
	}
	if (i && $1 == "fax-id") 
	    print $2
    }'

}


Test_Number() {

	let i=$NUMBER
	if [ "$NUMBER" = "" ] || [ $i = 0 ] ; then
		echo "Bad desination phone number: $NUMBER" >&2
		exit 3
	fi

}	

Make_Coverpage() {

	cat >banner.tex <<EOF
	\documentclass{article}
	\usepackage{graphicx}
	\begin{document}
	\pagestyle{empty}
	\bf\LARGE
	\begin{center}

	\includegraphics[width=0.7\textwidth]{$LOGO}

	\vspace{0.5in}
	\textbf{\textit{\Huge Facsimile}}

	\vspace{1in}

	\begin{tabular}[t]{ll}
	  To: & $TONAME \\\\
	      & $NUMBER\\\\
	  From: & $FROMNAME\\\\
	        & $ID\\\\
	  Date: & $(date)\\\\
	  Files: & $FILES\\\\
	  Pages: & $PAGES
	\end{tabular}

	\end{center}

	\end{document}

EOF

	if ! latex banner.tex >latex.out; then
	    echo Latex failure >&2
	    cat latex.out >&2
	    exit 2
	fi

	Erun 2 dvips banner.dvi 2>/dev/null -o banner.ps

	Erun 2 cat banner.ps | gs  -q -dSAFER -dNOPAUSE -r300 -sDEVICE=faxg3 -r204x196 -sOutputFile=- -

}


Lookup_Names() {

	NUMBER=$1
	TONAME="To whom it may concern"
	FROMNAME=""
	if [ -x /usr/bin/finger ] ; then
		FROMNAME=$(echo $(finger $USER@$HOST |grep Name:) | cut -f4- -d\  )
	fi
	if [ "$FROMNAME" = "" ] ; then
		FROMNAME=$USER@$HOST
	fi

}

Do_Retries() {

	SQ=$(Server_Queues)
	for i in $(echo $SQ | tr ',' ' ') ; do
		j=$(lpq -P$i |grep ^hold | awk '{print $4}')
		k=$(lpq -P$i |grep ^error | awk '{print $4}')
		for l in $j $k; do
			/usr/sbin/lpc release $i $l
		done
	done

}

Fax_Headers() {

	ID=$(Get_Faxid)
	Lookup_Names $NUMBER

	local FH=/etc/mgetty/faxheader
	local HF=/usr/lib/mgetty-fax/cour25.pbm
	let c=1
	for i in $@; do
	    ( set -e ; cat $FH | sed \
		-e "s;@T@;$NUMBER;g" \
		-e "s;@P@;$c;g" \
		-e "s;@M@;$PAGES;g" \
		-e "s;@U@;$USER;g" \
		-e "s;@N@;$FROMNAME;g" \
		-e "s;@D@;$TONAME;g" \
		-e "s;@ID@;$ID;g" \
		-e "s;@DATE@;`date`;g" \
	    | pbmtext -font $HF | pbm2g3 \
	    | g3cat - $i > tmp )
            if [ $? != 0 ] ; then
		echo Cannot place faxheader on $i >&2
		exit 3
            fi
	    Erun 2 mv tmp $i
	    let c=$c+1
	done

}

Check_Sendfax() {

	if ! [ -x $SPOOLD/sendfax ] ||
		[ /usr/sbin/sendfax -nt $SPOOLD/sendfax ] ; then
		Erun 2 cp /usr/sbin/sendfax .
		Erun 2 bpatch ./sendfax /var/log/mgetty/sendfax.log sendfax.log
		Erun 2 mv ./sendfax $SPOOLD
	fi
	Erun 2 test -x $SPOOLD/sendfax

}

Do_Fax() {

	Check_Sendfax
	touch sendfax.log
	tail -f sendfax.log >&2 &
	pid=$!
	$SPOOLD/sendfax -l $PRINTER $NUMBER $(ls -1 *.g3)
	case $? in
	    0) e=0; REASON="Success";;	
	    1) e=3; REASON="Sendfax Command Line error";;
	    2) e=1; REASON="Cannot open fax device $DEVICE";;
	    3) e=2; REASON="Cannot initialize modem";;
	    4) e=1; REASON="$NUMBER is busy";;
	    5) e=2; REASON="No dialtone";;
	    10) e=1; REASON="No carrier";;
	    11) e=1; REASON="Timeout waiting for XON";;
	    12) e=1; REASON="Error in transmission";;
	    15) e=1; REASON="Job terminated prematurely";;
	esac
	kill $pid
	cat sendfax.log |grep '#####' >&2

}

Script_Init() {

	export PATH=/usr/lib/lprfax:/$PATH
	Parse_Args $@
	LOGO=/usr/lib/lprfax/logo.eps

	if [ -r /etc/lprfax/lprfax.conf ] ; then
		. /etc/lprfax/lprfax.conf
	fi

	WD=$(basename $0).$$
	SPOOLD=$(pwd)

	Erun 2 rm -rf $WD
	Erun 2 mkdir $WD
	Erun 2 cd  $WD

	trap Cleanup exit

}

