xref: /plan9/rc/bin/fax (revision 651ff87a0a0495ebcb861c70ae633c18d66b574a)
1#!/bin/rc
2# fax [-v] telephone-number recipient [file ...] - send files via fax
3rfork e
4view=no
5stdin=no
6
7fn usage {
8	echo 'usage: fax [-v] telephone-number recipient [file ...]' >[1=2]
9	exit usage
10}
11
12switch($1){
13case -v
14	view=yes
15	shift
16}
17
18switch ($#*) {
19case 0 1
20	usage
21case 2
22	stdin=yes
23}
24telno=`{echo $1|sed 's/[ \-]//g'}
25shift
26
27if (! ~ $telno [0-9]* +[0-9]*)
28	usage
29
30# our old phone system needed *9 to get outside; the new one just needs 9
31switch($telno){
32case ???????
33	telno='9,'^$telno
34case ??????????
35	telno='9,1'^$telno
36case 1??????????
37	telno='9,'^$telno
38case 011*
39	telno='9,'^$telno
40case +*
41	telno=`{echo $telno | sed 's/\+/9,011/'}
42}
43
44recip=$1
45shift
46
47script=/tmp/fax.$pid
48header=/tmp/faxh.$pid
49user=`{cat /dev/user}
50tmp=/tmp/fax.g3.$pid
51tmpin=/tmp/fax.in.$pid
52tmpps=/tmp/fax.ps.$pid
53tel=`{grep '\) '$user /lib/tel}
54myname=`{echo $tel | sed 's/ \(.*//'}
55if (~ $#myname 0)
56	myname=''
57ext=`{echo $tel | sed 's/.*\) [^ ]* [^ ]* ([^ ]*).*/\1/'}
58
59fn sigexit {
60	rm -f $tmp.* $script $header $header.* $tmpin $tmpps
61}
62fn sigint {
63	sigexit
64	exit interrupt
65}
66
67# gather input into a file
68switch($stdin){
69case yes
70	cat >$tmpin
71	infiles=$tmpin
72case *
73	infiles=($*)
74}
75
76# convert to g3
77g3files=()
78a=1
79for(i in $infiles){
80	switch(`{file $i}){
81	case *:*g3* *:*fax*
82		g3files=($g3files $i)
83	case *:*postscript
84		gs -dSAFER '-sDEVICE=dfaxlow' '-sOUTPUTFILE='$tmp'.'$#a'.%03d' \
85			-dNOPAUSE -dQUIET $i quit.ps
86		g3files=($g3files $tmp.$#a.*)
87	case *
88		lp -dstdout $i >$tmpps
89		gs -dSAFER '-sDEVICE=dfaxlow' '-sOUTPUTFILE='$tmp'.'$#a'.%03d' \
90			-dNOPAUSE -dQUIET $tmpps quit.ps
91		g3files=($g3files $tmp.$#a.*)
92	}
93	a=($a 1)		# count by increasing list length
94}
95
96pages=`{echo $g3files|wc -w}
97
98# use delimiters that are unlikely to be supplied in arguments
99{
100	echo -n sFAXddd101	echo -n `{date}		# treat `{} output list specially
102	echo103	echo sFAXFFF$"myname^∮
104	echo s∮FAXEEE∮$"user^∮
105	echo sFAXVVV$"ext^∮
106	echo s∮FAXTTT∮$"recip^∮
107	echo sFAXfff$"telno^∮
108	echo s∮FAXPPP∮$"pages^∮
109} >>$script
110sed -f $script /sys/lib/fax/h.ps >$header
111
112gs -dSAFER '-sDEVICE=dfaxlow' '-sOUTPUTFILE='$header'.%03d' \
113	-dNOPAUSE -dQUIET $header quit.ps
114
115files=()
116for(i in $header.* $g3files){
117	files=($files -f $i)
118}
119
120switch($view){
121case yes
122	page $header.* $g3files
123case *
124	upas/qer $files /mail/faxoutqueue fax $user $telno	</dev/null
125	rx fax /sys/lib/fax/faxgoose				</dev/null
126}
127