#!/bin/ash
# by M. Andreoli
 
. /etc/color

#set -x

#--------------
# functions
#--------------

default()
{
if [ -r /setup/cnf/fax.cnf ] ; then
        . /setup/cnf/fax.cnf
else

DEVICE="/dev/modem"
CLASS="1"
CAPABILITY="5"
FROM="Ananke"
VERBOSITY="high"
RINGS=2
ADAPTATIVE=n
INIT="-iZ -i&F1&D2S7=120 -i&C0 -iL1M1"

fi

FORMAT="21x29.7cm"
RESOLUTION="204x196"
if [ ! -r /etc/fax.conf ] ; then
cat > /etc/fax.conf <<END
-d /dev/modem
-vewchinrxtf
-iZ -i&F1&D2S7=120 -i&C0 -iL1M1
-o1
-c 1,5,0,0,0,0,0,0
-x /var/lock/LCK..modem
-kZ
-jS0=2
END
fi

}


header()
{
cat <<END
  __            
 / _| __ ___  __
| |_ / _| \ \/ /
|  _| (_| |>  < 
|_|  \__/_/_/\_\


END
}

log()
{
echo "$@" 1>&2
}

cleanup()
{
log "Stop signal received, cleanup"
rm $TMP/fax$$.page 2>/dev/null
kill -9 `pidof efax`
}


# fax test: extracted by efax "fax" script (C) Ed. Casas

fax_test()
{
	read -p "Turn on the modem -ENTER-" dummy
	case "$CLASS" in
	2.0) 
	CLASSINIT="-o0"
	CMDS=" -i+FCLASS=2.0 -i+FMI? -i+FMM? -i+FMR? \
	-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+FLP=?  -i+FLP? -i+FPP=?  -i+FPP? "
	;;
	2)
	CLASSINIT=""
	CMDS=" -i+FCLASS=2 -i+FMFR? -i+FMDL? -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+FLPL=? -i+FLPL?  -i+FSPL=? -i+FSPL? \
	-i+FPTS=? -i+FPTS?  -i+FREL=? -i+FREL? -i+FREV? "
	;;
	1) 
	CLASSINIT="-o1"
	CMDS="-i+FCLASS=1 \
	-i+FAE=?  \
	-i+FTM=? -i+FRM=? \
	-i+FTH=? -i+FRH=? \
	-i+FTS=? -i+FRS=? -iS10?"
	;;
	esac

	echo
	echo "Please wait, this will take a minute..." 1>&2
	echo 

	efax -vchewinr -d$DEVICE  \
	$CLASSINIT  \
	-iI0 -iI1 -iI3 -iI4 \
	-i+FCLASS=? -i+FCLASS? \
	$CMDS \
	-i ""  -k"Z" -oe -T >/tmp/fax.test 2>&1

	echo "Thank you." 1>&2
	echo "Check ERROR messages and change CLASS running \
'setup -f fax' again "
        echo "if you see too much (importants) ERROR strings."
        echo
        read -p "press -ENTER-" dummy	
	less /tmp/fax.test
	
}

fax_make()
{
header > $TMP/fax$$.page
cat >> $TMP/fax$$.page <<END
	------------------------------------------------------------------

	TO:	
	Fax-Nr: <put number here> 

	------------------------------------------------------------------

	From:   
 	Fax:   

	Date:      


	MESSAGE:

	   

END
	vi $TMP/fax$$.page
	echo
	read -p "s)end a)bort :" ans


case "$ans" in
a|A)
	echo "Aborted"
	sleep 1
	;;
s|S)
	fax_send
	;;
*)
	;;
esac

}

extract_phone_text()
{
set -- `cat $TMP/fax$$.page \
| sed -n "/%[0-9]/P" |sed 's/.*%[ ]*\([0-9]*\).*/\1/'`
echo $1
}

extract_phone_ps()
{
cat $TMP/fax$$.page | efn
}

extract_phone()
{
p=$(extract_phone_text)
[ -z "$p" ] && p=$(extract_phone_ps) 
echo $p
}

file_type()
{
case $(dd if=$TMP/fax$$.page bs=1c count=20) in
%!PS*)
	echo postscript
	;;
*)
	echo text
esac
}


fax_send()
{

ft=$(file_type)

case "Z$1Z" in
ZZ)
NUMBER=$(extract_phone)
	;;
*)
	NUMBER=$1
	;;
esac

FROM="$(hostname):$(id)"

if [ -z "$NUMBER" ] ; then
	wave -c 220 4
	log "Missing the Fax-Nr: field from text"
	exit
fi

log "Fax to: ($NUMBER), from ($FROM)"

# translate Ps->tiffg3

case $ft in
text)
efix -ve -otiffg3 -p"$FORMAT" -r"$RESOLUTION" \
-n $TMP/fax-sent-$$.%03d $TMP/fax$$.page
;;
postscript)

cat $TMP/fax$$.page  | gs -sDEVICE=tiffg3 \
-sOutputFile=- -dNOPAUSE -q -dSAFER - | \
 efix -ve -otiffg3 -p"$FORMAT" -r"$RESOLUTION" \
-n $TMP/fax-sent-$$.%03d
esac 

rm $TMP/fax$$.page

HDR="From: $FROM      TO: ($NUMBER), Date=`date`"
efax `cat /etc/fax.conf` -s \
-h"$HDR" -j"&H1" \
-t T${NUMBER} $TMP/fax-sent-$$.* 2>&1 

}


pick()
{
local n=$1
skip=`expr $n - 1`
/bin/ls -1 | tail +$skip | (read line; echo $line) 
}


manage()
{
cd $TMP
echo -e "$BRIGHT$GREEN"
/bin/ls -1q fax-sent* | cat -n
echo -e "$NORMAL"

while [ 1 ] ; do
read -p "Select a number: " number
read -p "p)rint v)iew r)emove     q)uit " cmd

f=`pick $number`
case $cmd in
v)
	xv $f;;
p)
	lpr $f;;
r)
	rm $f;;
q)
	return 0;;
*)
	echo error;;
esac
done 

}


menu()
{

if [ $# = 0 ] ; then
clear                                                                            
cat <<END

			    == FAX MANAGER ==                                                                                 
	c)onfigure:	run 'setup -f fax'                                   

	t)est fax:	test modem features
	w)ait fax:	answer phone now and receive fax files 
	n)ew fax:	create a fax and send it
	m)anage fax:	print, view, etc.	

	q)uit h)elp                                                                          
	- Log errors in /var/log/fax.log
	- Fax stored in /tmp/fax
END
echo
read -p "Enter > " ans

else
	ans=$1
fi

case "$ans" in
-h|h)
cat <<END

fax: script for fax managing
(C) M.Andreoli

	Interactive usage: write fax

	Script usage:

		#cat text-boxy | fax -   ... or ...
		#cat text-boxy | fax phone-number 

	Note:
	- phone number is extract from a field like "%phone-number"
	  in your text. This field is REQUIRED. Ex:  %05878261
	- input text and Postscript are supported

END
	read -p "press -enter-" enter
	;;

c*)
	setup -f fax
	;;
t*)
	fax_test
	;;
w*)
	echo wait for fax 
	while [ 1 ] ; do /usr/sbin/in.faxd ; done 
	;;
n*)
	fax_make
	;;
m*)
	manage
	;;	 
q)	
	rm -f $TMP/fax.*
	exit ;;
*)
	echo "error"
	;;
esac


}

#------------
# Main
#------------

default
TMP=/tmp/fax$$
TMP=/tmp/fax
[ -x "$TMP" ] || (mkdir -p $TMP; chmod 777 $TMP)

trap "cleanup" 2 3

case "${1}" in
-)
	kill -9 `pidof efax` 2>/dev/null
	cat > $TMP/fax$$.page
	fax_send
	exit
	;;
[0-9]*)
	kill -9 `pidof efax` 2>/dev/null
	cat > $TMP/fax$$.page
	fax_send $1
	exit
esac

case $# in
0)	while [ 1 ] ; do menu ; done;;
*)	menu $1;;
esac

# End
