This file is indexed.

/usr/bin/faxspool is in mgetty-fax 1.1.36-3.1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
#!/bin/bash
#
# faxspool - sample script how to spool fax input data to a spool
#            directory, creating jobs to be run by faxrunq
#
# sccsid: $Id: faxspool.in,v 4.33 2006/12/07 15:16:31 gert Exp $ (c) Gert Doering
#
# syntax: faxspool [flags] <phone-number> <job(s)>
#
# <job(s)> may be any number of files. The file type has to be guessed -
# for now, the following file extensions are recognized:
#
#   .ps  -> PostScript
#   .t   -> plain ascii text
#   .dvi -> TeX device independent output file (use dvips, then like .ps)
#   .pbm -> PortableBitMap (use pbm2g3)
#   .pgm -> PortableGrayMap (use pgmtopbm | pbm2g3)
#   .ppm -> PortablePixMap (use ppmtopgm | pgmtopbm | pbm2g3)
#   .g3  -> raw G3 fax data
#   .lj  -> HP Laserjet PCL4 (use hp2pbm)
#   .xwd -> xwindow-dump (by xwd program, use xwdtopnm)
#
# ChangeLog:
#  3.6.93: use dvips instead of dvialw now (GD)
# 15.9.93: use g3cat to concatenate header and page (GD)
#  3.10.93: use "hp2hig3" for hp-pcl4-files (cl)
# 19.10.93: phone directories (caz)
# $Log: faxspool.in,v $
# Revision 4.33  2006/12/07 15:16:31  gert
# new sequence for fax header: @S@ - print job number (F000123)
#
# Revision 4.32  2006/09/26 09:05:30  gert
# giftoppm is nowadays called giftopnm...
#
# Revision 4.31  2005/02/24 16:28:26  gert
# fix whole user/USER mess
#   - drop $USER.  $user is only used for fax header substitution now.
#   - mail (-f) is in $mail_to
#   - only forced user name (-u/$force_user) is put into $JOB.q
#
# Revision 4.30  2004/11/14 13:14:08  gert
# change test -x to -r for $HOME/.make.coverpg (mode r-- = no coverpage)
#
# Revision 4.29  2004/11/13 22:35:54  gert
# test for $HOME/.make.coverpg with "-x"
# if spooling from stdin ("-"), don't test for "size > 0" (-s) - breaks stuff
#
# Revision 4.28  2004/07/17 10:17:49  gert
# per-user coverpg and faxheader files (Andreas Barth)
#
# Revision 4.27  2004/02/05 17:39:44  gert
# call "(umask 077 ; mkdir)" in a sub-shell: saves the need to save/restore umask
#
# Revision 4.26  2003/10/09 12:40:16  gert
# refuse empty (0 bytes) input files
#
# Revision 4.25  2003/06/28 20:44:04  gert
# reset umask after creating temp directory
#
# Revision 4.24  2003/04/09 21:05:14  gert
# fix case of "faxing without header" (-h -) - wasn't copying G3 files
#
# Revision 4.23  2003/01/21 13:27:12  gert
# abort if input file names contain whitespace or quote characters
#
# Revision 4.22  2002/11/23 15:14:57  gert
# revert 4.14 -> 4.15 change: faxqueue_done is now back in $FAX_SPOOL_OUT
# (new permission concept, faxrunq/faxrunqd no longer running as root)
#
# Revision 4.21  2002/11/19 10:08:39  gert
# do not check + mkdir $FAX_SPOOL_OUT/ anymore (faxq-helper does this)
# do not write to $FAX_SPOOL_OUT/<subdir> directly anymore - create G3 files
# in $TMP/<subdir>/, use faxq-helper to copy them to fax queue.
# same applies to .source-files/$b - copy via faxq-helper
#
# Revision 4.20  2002/11/17 22:06:18  gert
# FAX_ALLOW + FAX_DENY moved to fax-helper
#
# Revision 4.19  2002/11/16 22:21:36  gert
# initial adaptions for faxq-helper
#
# Revision 4.18  2000/10/11 10:06:41  gert
# fix combination of "copy_sorce" and "faxspool.rules" (command line destroyed)
#
# Revision 4.17  2000/08/06 18:06:21  gert
# make wording of success message clearer for -t <hh:mm> case
#
# Revision 4.16  2000/08/06 18:02:13  gert
# support PDF input files (via acroread)
#
# Revision 4.15  2000/08/06 14:28:37  gert
# go from using $fax_spool_out/.last_run to VARRUNDIR/faxqueue_done
#
# Revision 4.14  2000/07/18 20:18:44  gert
# allow W and w in phone number
#
# Revision 4.13  1999/06/29 13:19:45  gert
# rename faxspool.filter to faxspool.rules - 14-char limit!
#
# Revision 4.12  1999/05/27 14:49:09  gert
# lookup file extention in "faxspool.filter" if built-in list doesn't find it
#
# Revision 4.11  1999/05/27 13:48:55  gert
# implement handling of external conversion programs (input to G3)
#

FAX_SPOOL=/var/spool/fax
FAX_SPOOL_OUT=/var/spool/fax/outgoing
FAX_SEQ=$FAX_SPOOL_OUT/.Sequence
FAX_SEQ_LOCKDIR=/var/lock/fax
FAX_SEQ_LOCK=$FAX_SEQ_LOCKDIR/LCK..seq
LAST_RUN=/var/spool/fax/outgoing/faxqueue_done

# helper program for privileged queue access
FAXQ_HELPER=/usr/lib/mgetty-fax/faxq-helper

# fax phone directories - format: <alias> <fax phone number>
GLOBAL_PHONE_DIR=/etc/mgetty/faxaliases
PRIVATE_PHONE_DIR=$HOME/.faxnrs

# you have to adapt this to your local system!
#
# this is the file with the fax header - @T@ / @P@ / @M@ / @U@ stand for
# telephone / page number / maximum page number / user name, respectively
FAX_HEADER=/etc/mgetty/faxheader
#
# for creating the fax page header, pbmtext is used, and this specifies
# the font file to use (fine/normal res.)
PBMFONT_HEADER_F=/usr/lib/mgetty-fax/cour25.pbm
PBMFONT_HEADER_N=/usr/lib/mgetty-fax/cour25n.pbm
#
# if you want to use pbmtext for converting ASCII texts, use these fonts:
PBMFONT_BODY_F=/usr/lib/mgetty-fax/cour25.pbm
PBMFONT_BODY_N=/usr/lib/mgetty-fax/cour25n.pbm

#
# this file overrides internal conversion functions
FS_FILTER=/etc/mgetty/faxspool.rules

#
# these are the drivers used in Ghostscript - choose dfaxhig/low or faxg3
#GS_DRIVER_HI="-sDEVICE=dfaxhigh"
#GS_DRIVER_LO="-sDEVICE=dfaxlow"
GS_DRIVER_HI="-sDEVICE=faxg3 -r204x196"
GS_DRIVER_LO="-sDEVICE=faxg3 -r204x98"

#
# program that will generate fax coverpage (see "man coverpg")
#
MAKE_COVER_PG=/etc/mgetty/make.coverpg

#
# local fax number (will be overriden by "sendfax.config" if set there!)
#
FAX_STATION_ID=" "

#
# echo program that will accept escapes (bash: "echo -e", sun: /usr/5bin/echo)
#
echo="echo -e"

AWK=awk

#
# make sure that pbm2g3 and newslock can be found
#
PATH=/usr/bin:$PATH

#### end of configuration section

# change coverpage/header defaults to per-user files, if $HOME exists
if [ -d "$HOME" ]; then
  if [ -r $HOME/.make.coverpg ]; then
    MAKE_COVER_PG=$HOME/.make.coverpg
  fi
  if [ -r $HOME/.faxheader ]; then
    FAX_HEADER=$HOME/.faxheader
  fi
fi

#
#### shell functions for conversions
#

# naming scheme: fs_cvt_$type()
# converts a $type file ($1) to g3 file(s), named $2.$i, where "i"
# should be the page number. Can be omitted if $1 has only one page.
# $3 can be a flag argument, "-n" for normal resolution

#
# convert portable bitmap (not scaled) - see pbm(5), pbm2g3(1)
#
fs_cvt_pbm()
{
    pbm2g3 "$1" >"$2".001
}

#
# convert portable greymap (not scaled) - see pgm(5)
#
fs_cvt_pgm()
{
    pgmtopbm "$1" | pbm2g3 >"$2".001
}

#
# convert portable pixmap (no scaling) - see ppm(5)
#
fs_cvt_ppm()
{
    ppmtopgm "$1" | pgmtopbm | pbm2g3 >"$2".001
}

#
# "convert" G3 file
# actually, it's just copied, but I want the interface to be the same
# later on, one could do page resizing, resolution changing, ... here
#
fs_cvt_g3()
{
    g3cat "$1" >"$2".001
}

#
# convert a X11 xwd file - scaled to fill the whole page width
# since we do not know whether it's colour or grey or what, we have
# to do *all* the conversions *all* the time - ugly. FIXME.
#

fs_cvt_xwd()
{
    REDUCE="cat"
    test X"$3" = X-n && REDUCE="pnmscale -yscale 0.5"

    xwdtopnm "$1" |\
	pnmscale -xysize 1728 2000 |\
	$REDUCE |\
	ppmtopgm |\
	pgmtopbm |\
	pbm2g3 >"$2".001
}

#
# convert a CompuServe GIF file, also properly scaled
# problem: a GIF file can contain multiple images - FIXME
#

fs_cvt_gif()
{
    REDUCE="cat"
    test X"$3" = X-n && REDUCE="pnmscale -yscale 0.5"

    giftopnm "$1" |\
	pnmscale -xysize 1728 2000 |\
	$REDUCE |\
	ppmtopgm |\
	pgmtopbm |\
	pbm2g3 >"$2".001
}

#
# convert TIFF file
# problem1: conversion always via ppm, pgm
# problem2: multipage TIFFs
#
fs_cvt_tif()
{
    REDUCE="cat"
    test X"$3" = X-n && REDUCE="pnmscale -yscale 0.5"

    tifftopnm "$1" |\
	pnmscale -xysize 1728 2000 |\
	$REDUCE |\
	ppmtopgm |\
	pgmtopbm |\
	pbm2g3 >"$2".001
}

#
# convert HP laserjet input files
# needs Chris Lewis' hp2pbm package (ftp.uunet.ca)
#
fs_cvt_lj()
{
    if [ X"$3" = X-n ]
    then
        hp2log3 -r"$2" <"$1"
    else
        hp2hig3 -r"$2" <"$1"
    fi
}

#
# convert postscript data
# needs GNU GhostScript installed. 
# For driver selection, see definition of GS_DRIVER_{HI,LO} above.
#
fs_cvt_ps()
{
    driver="$GS_DRIVER_HI"
    test X"$3" = X-n && driver="$GS_DRIVER_LO"

    cat "$1" |
	gs $driver -sOutputFile="$2"%03d -dNOPAUSE -q -dSAFER -
}
       
#
# convert ASCII text files
# go via GhostScript and gslp.ps
# (could also used hp2hig3 or nenscript -> gs or pbmtext)
#
fs_cvt_ascii()
{
#####
# via Ghostscript:

    driver="$GS_DRIVER_HI"
    test X"$3" = X-n && driver="$GS_DRIVER_LO"

    gs $driver -sOutputFile="$2"%03d -dNOPAUSE \
			-- gslp.ps -fCourier-Bold10 -B "$1"

#####
# via hp2pbm:
# (convert "LF" to "CR+LF" via awk)

#    pgm=hp2hig3
#    test X"$3" = X-n && pgm=hp2log3
#
#    $AWK '{ printf "%s\r\n", $0 }' "$1" | $pgm -r"$2"

#####
# via pbmtext (not really recommended):
# use the "pgx" program in contrib/ to split pages

## Select appropriate font
#    font=$PBMFONT_BODY_F
#    test X"$3" = X-n && font=$PBMFONT_BODY_N
# Determine how many pages text will be split in to.  Uses default pagelen.
#    nr=`pgx < "$1"`
# Convert each page into a separate G3 file.  Uses default pagelen (60).
#    page=1
#    while [ $page -le $nr ]; do
#	pgx $page < "$1" | pbmtext -font $font | pbm2g3 >"$2".$page
#	page=`expr $page + 1`
#    done
}

#
# convert TeX DVI files
# needs GhostScript and dvips installed.
# alternatively, one could use dvialw.
#
fs_cvt_dvi()
{
    if [ X"$3" = X-n ]
    then 
	driver="$GS_DRIVER_LO" ; dvipscfg="-P dfaxlo"
    else
	driver="$GS_DRIVER_HI" ; dvipscfg="-P dfaxhigh"
    fi

# if you do not have the dfaxlo(w)/dfaxhigh dvips modes configured, 
# remove "$dvipscfg" from the dvips command line below [or configure them!]

    dvips $dvipscfg "$1" -o \
        !"gs $driver -sOutputFile="$2"%03d -dNOPAUSE -dSAFER -q -"

# for those that only have the old "dvialw":
#
#	dvialw <$file |
#	    gs $driver -sOutputFile="$2"%03d -dNOPAUSE -dSAFER -q -

# for those that have dvi2ps and not dvips:
#
#	dvi2ps -r -q $q |
#	    gs $driver -sOutputFile="$2"%03d -dNOPAUSE -dSAFER -q -

}

#
# convert pdf data
# needs GNU GhostScript and Adobe acroread installed.
# For driver selection, see definition of GS_DRIVER_{HI,LO} above.
#
# contributed by mfvm@gmx.de (Michael Fischer v. Mollard)
#
fs_cvt_pdf()
{
    driver="$GS_DRIVER_HI"
    test X"$3" = X-n && driver="$GS_DRIVER_LO"

    cat "$1" |
	gs $driver -sOutputFile="$2"%03d -dNOPAUSE -q -dSAFER -
}


	
#
#### "main" function
#

#
# setup defaults / get values
#

# user name (for fax header only! auth is done by faxq-helper via getuid())
##########
if user=`logname 2>/dev/null`
then :
else
    id=`id`
    user=`expr "$id" : "[^( ]*(\([^)]*\)"`
fi
test -z "$user" && user=$LOGNAME
test -z "$user" && user=$USER
test -z "$user" && user="nobody"

# fax ID (from sendfax.config)
##########
eval ` $AWK '$1=="fax-id" { printf "FAX_STATION_ID=\"%s", $2; \
			    for(i=3;i<=NF;i++) printf " %s", $i; print "\""; }
             $1=="port"   { exit }' /etc/mgetty/sendfax.config 2>/dev/null`

# everything else is initialized empty
##########
force_user=""
mail_to=""
poll_req=""
verbose_to=""
normal_res=""
TIME=""
copy_source=""
job_priority=""

#
# get command line arguments (overriding some of the values above)
##########
#
usage="Usage: $0 [options] [faxphone] [page data]
Options:
\t-p\t\tpoll request
\t-n\t\tnormal resolution
\t-C <pgm>\tset cover page program (\"-\" for none)
\t-D <dest>\tset verbose destination name
\t-F <full name>\tset full name of sender
\t-h <file>\ttext file for page header
\t-f <email>\tset address for status mail
\t-P <n>\t\tset job priority (1=lowest, 9=highest)
\t-c\t\tcopy source files
\t-t <hh:mm>\tset earliest possible send time
\t-t <hh:mm-hh:mm> set time range for sending fax
\t-A <txt>\tspecify free-form accounting information
\t-q\t\tshut up"

while :
do
    case "$1" in
# enable polling
	-p) poll_req=true ; shift
            ;;
# use normal resolution (as opposed to fine resolution)
        -n) normal_res="-n" ; shift
            ;;
# set cover page program to use
	-C) case "$2" in
	    '') $echo "$usage" >&2 ; exit 2 ;;
	    "-") MAKE_COVER_PG="" ;;
	    /*) MAKE_COVER_PG="$2" ;;
	    *) MAKE_COVER_PG=`pwd`/"$2" ;;
	    esac
	    shift ; shift
	    ;;
# set verbose destination address
        -D) case "$2" in
	    '') $echo "$usage" >&2 ; exit 2 ;;
            esac
            verbose_to="$2"
	    shift ; shift
	    ;;
# set verbose origination address ("fullname")
        -F) case "$2" in
	    '') $echo "$usage" >&2 ; exit 2 ;;
            esac
            FULLNAME="$2"
	    shift ; shift
	    ;;
# set job priority (1=lowest, 9=highest)
        -P) case "$2" in
	    [1-9]) ;;
	    *) $echo "$usage" >&2 ; exit 2 ;;
            esac
            job_priority="$2"
	    shift ; shift
	    ;;
# set page header text file
	-h) case "$2" in
	    '') $echo "$usage" >&2 ; exit 2 ;;
	    '-') FAX_HEADER="" ;;
	    /*) FAX_HEADER="$2" ;;
	    *) FAX_HEADER=`pwd`/"$2" ;;
	    esac
	    test ! -z "$FAX_HEADER" && \
	      if [ ! -f "$FAX_HEADER" ] ; then
		$echo "$0: no such fax header file: '$2'" >&2
		exit 2;
	    fi
	    shift ; shift
	    ;;
# set e-mail return address
	-f) case "$2" in
	    '') $echo "$usage" >&2 ; exit 2 ;;
            esac
	    mail_to="$2"
	    shift ; shift
	    ;;
# force user name (faxq-helper will verify "trusted" users)
	-u) case "$2" in
	    '') $echo "$usage" >&2 ; exit 2 ;;
            esac
	    force_user="$2"
	    shift ; shift
	    ;;
# set first time to send fax
	-t) if expr "$2" : "[0-2][0-9]:[0-9][0-9]$" >/dev/null
	    then
		h=`expr "$2" : "\(..\)"`
		m=`expr "$2" : "..:\(..\)"`
		if [ "$h" -gt 23 -o "$m" -gt 60 ]
		then
		    $echo "Invalid time specified: $h:$m" >&2; exit 2
		fi
		TIME="$h$m"
	    else
		$echo "Time must be in <hh:mm> format." >&2; exit 2
	    fi
	    shift ; shift
	    ;;
# set accounting handle (just a string that will be logged)
       -A) case "$2" in
           '') $echo "$usage" >&2 ; exit 2 ;;
	   esac
           ACCT_HANDLE="$2" ; shift ; shift
	   ;;
# shut up
       -q) exec >/dev/null ; shift
           ;;
# copy source file (for "some action" when transmission fails)
       -c) copy_source=TRUE; shift
	   ;;
# unknown options
       -*) $echo "unknown option: $1" >&2
           $echo "$usage" >&2
	   exit 1
	   ;;
# anything else: leave loop	
	*) break
    esac
done

#
# if not yet set, get full name from /etc/passwd (name only)
#
if [ -z "$FULLNAME" ]
then
    FULLNAME=`grep "^$user:" /etc/passwd | cut -f5 -d: | cut -f1 -d,`
fi

#
# check syntax
#

if [ $# -eq 0 ] ; then
    $echo "$0: phone number missing" >&2
    $echo "$usage" >&2
    exit 3
fi

phone="$1" ; shift
if expr "$phone" : "[-0-9TtPpWw,;]*$" >/dev/null ; then :
else
    alias="$phone"
    phone=""
    awkpgm='$1 == "'"$alias"'" { printf "phone=\"%s\"; vto=\"", $2;
                                 for ( i=3; i<=NF-1; i++ ) printf "%s ",$i;
				 printf "%s\"\n", $i; exit }'
    if [ -r $PRIVATE_PHONE_DIR ]
    then
	eval `$AWK "$awkpgm" $PRIVATE_PHONE_DIR`
    fi
    if [ -z "$phone" -a -r $GLOBAL_PHONE_DIR ]
    then
	eval `$AWK "$awkpgm" $GLOBAL_PHONE_DIR`
    fi
    if [ -z "$phone" ]
    then
	$echo "$0:\nNon-numeric characters in phone number and" >&2
	$echo "'$alias' not found in fax directories\n$usage" >&2
	exit 4
    fi

    [ -z "$verbose_to" ] && verbose_to="$vto"
    [ -z "$verbose_to" ] && verbose_to="$alias"

    $echo "sending fax to '$verbose_to' using phone number '$phone'..."
fi

#
# check, if all the file names are "clean", that the files exist & are readable
#

for file
do
    if [ `echo "$file" | tr -d ' \011\012\015\047\140"' ` != "$file" ]
    then
	$echo "$0: invalid characters in file name '$file'!" >&2 ; exit 5
    fi
    if [ ! -r $file -a x$file != x- ]
    then
	$echo "$0: cannot open '$file'!" >&2 ; exit 5
    fi
    if [ ! -s $file -a x$file != x- ]
    then
	$echo "$0: input file '$file' is empty (0 bytes)!" >&2 ; exit 5
    fi
done

#
# if no file specified on command line, use stdin
# (only if not polling and no cover page)
#

if [ $# -eq 0 -a -z "$poll_req" -a -z "$MAKE_COVER_PG" ]
then
    # if stdin is connected to a tty, notify user
    #
    tty -s && \
	$echo "spooling text from standard input. End typing with ^D" >&2
    set -- -
fi


#
# get unique directory name, using faxq-helper
#
new_seq=`$FAXQ_HELPER new`

if [ -z "$new_seq" ] ; then
    $echo "can't create new job directory, give up" >&2 ; exit 6
fi

#
# mkdir a directory in $TMP (or /tmp), convert input to G3 in there
#
spooldir=`mktemp -t -d`

if [ $? -eq 0 ]; then
    $echo "spooling to $spooldir (->$new_seq)..."
else
    $echo "ERROR: can't create work dir '$spooldir', giving up" >&2 ; exit 6
fi

#
# process all input files
#
input_data="$*"
F_NO=0001

for file
do
#
# if filename is "-", use stdin
#
    if [ x$file = x- ]
    then
	file=`mktemp /tmp/faxspool.XXXXXXXXX`
	if test -z $file
	then
		$echo "ERROR: can't create work file, giving up" >&2 ; exit 6
	fi
	trap "rm $file" 0
	cat - >$file
    else
	$echo "spooling $file..."
    fi

    format=""

#
# try to determine file type by extention (won't work for print spooler!)
#
    case $file in
	*.g3)	format="g3" ;;
	*.ps)	format="ps" ;;
	*.pdf)	format="pdf" ;;
	*alw)	format="ps" ;;
	*.dvi)	format="dvi";;
	*.pbm)	format="pbm";;
	*.pgm)	format="pgm";;
	*.ppm)	format="ppm";;
	*.t)	format="ascii";;
	*.txt)	format="ascii";;
	*.asc)	format="ascii";;
	*.lj)   format="lj" ;;
	*.pcl)  format="lj" ;;
	*.xwd)	format="xwd";;
	*.gif)	format="gif";;
	*.tif)	format="tif";;
	*.tiff)	format="tif";;
    esac

# if we don't know the file type now, let's look for some more
# extentions in the filter configuration file
    if [ -z "$format" -a -f $FS_FILTER ]
    then
	format=`$AWK ' BEGIN { f="'"$file"'"; sub( /^.*\./, "", f ); }
                   $1 == "SUFFIX" && $2 == "."f { print $3; exit }' $FS_FILTER`
    fi

# if we don't know the file type now, let's try something more esoteric

    if [ -z "$format" ]
    then
#
# ask "file"
# (extend /etc/magic if necessary!)
#
	case "`LANG=C file $file | cut -d':' -f2- `" in
	    *"nglish text"*)	format="ascii" ;;
	    *"ascii text"*)	format="ascii" ;;
	    *"ASCII text"*)	format="ascii" ;;
	    *"ews text"*)	format="ascii" ;;
	    *"ail text"*)	format="ascii" ;;
	    *"commands text"*)	format="ascii" ;;
	    *"c program text"*)	format="ascii" ;;
	    *"script text"*)	format="ascii" ;;
	    *PBM*)		format="pbm" ;;
	    *PGM*)		format="pgm" ;;
	    *PPM*)		format="ppm" ;;
	    *GIF*)		format="gif" ;;
	    *Digifax*)	format="g3" ;;
	    *DVI*)	format="dvi" ;;
	    *PCL*)	format="lj" ;;
	    *postscript*)	format="ps" ;;
	    *PostScript*)	format="ps" ;;
	    *TIF*)		format="tif" ;;
	esac

# if file told us, it's an ascii text, or if we still don't know, try
# looking at the first few bytes (do not use "head", it may break on
# binary data)

	if [ -z "$format" -o "$format" = "ascii" ]
	then
	    case "`dd if=$file bs=1 count=4 2>/dev/null`" in
		%!*)            format="ps" ;;
		%PDF)           format="pdf" ;;
		P1*|P4*)	format="pbm" ;;
		P2*|P5*)	format="pgm" ;;
		P3*|P6*)	format="ppm" ;;
		GIF*)		format="gif" ;;	# hmmm.
	    	II*)		format="tif" ;;
		MM*)		format="tif" ;;
	    esac
	fi
#
# detect dvi by directly looking at bytes 16...25
#
	if [ -z "$format" ]
	then
	    if [ "`dd if=$file bs=1 skip=16 count=11 2>/dev/null`" \
	         = "TeX output " ]
	    then
	        format="dvi"
	    fi
	fi
    fi

#
# ok, now we should *really* know what the file type is.
#
    if [ -z "$format" ] ; then
	$echo "$file: cannot determine file format (extend source)" >&2
#
# if stdin is a tty, ask the user for the file type
#
        if tty -s
	then
	    $echo "$file: please enter type: " >&2
	    read format
	else
	    exit 7
	fi
    fi

    $echo "$file is format: $format"

    target=$spooldir/i-$F_NO-

    # get & use external filter command (if set)
    filter=`$AWK '$1 == "FILTER" && $2 == "'"$format$normal_res"'" \
		    { $1=""; $2=""; print; exit; }' $FS_FILTER 2>/dev/null`
	
    if [ -n "$filter" ] 
    then
	$echo "calling external filter:\n$filter\n";
	set -- $file $target $normal_res
	eval "$filter"
    else
	case $format in
	    ps | ascii | pbm | pgm | ppm |\
	    g3 | dvi   | lj  | xwd | gif | tif | pdf) 
		    fs_cvt_$format $file $target $normal_res
		    ;;    
	    *) $echo "$0: no internal filter for format '$format!' found" >&2 
               exit 8 ;;
	esac 
    fi

    if [ $? -ne 0 ] 
    then
	$echo "\n$0: error spooling \"$file\" - aborting!" >&2 ; exit 8
    fi

    # next temporary file number (using awk 'cause expr can't do "%04d"!)
    F_NO=`echo $F_NO | $AWK '{ printf "%04d", $1 + 1 }' -`
done

#
# copy source files over (if requested)
#
if [ ! -z "$copy_source" ] ; then
    $echo "\nCopy source files (for printing, if faxing fails)..."
    for file in $input_data
    do
	b=`basename $file`
	$FAXQ_HELPER input $new_seq .source-files/$b <$file
    done
fi

#
# OK, all files are in the target directory now...
#
# Now let's create the work file
#

job=$spooldir/JOB

#
# conversion complete. Post-process G3 files
#

pages=`ls $spooldir | sed '/^\./d'`
#
# get list / number of pages
#
cd $spooldir

nr=0
maxnr=`echo $pages | wc -w | tr -d " "`

#
# generate cover page
#
# dispose arguments (-C "pgm args")
PGM=`expr "$MAKE_COVER_PG" : "\([^ 	]*\)"`
#
if [ ! -z "$PGM" ] 
  then
  if [ -x "$PGM" ]
  then
    $echo "\nGenerating cover page..."
    export normal_res

    maxnr=`expr $maxnr + 1`
    $MAKE_COVER_PG $maxnr "$FAX_STATION_ID" "$FULLNAME" \
	"$phone" "$verbose_to" "`date +%D`" "`date +%T`" >cover.g3
    if [ -s cover.g3 ]
    then
	pages="cover.g3 $pages"
    else
	$echo "generating cover page failed!" >&2
	maxnr=`expr $maxnr - 1`
    fi
  fi
fi

#	
# concatenate header with pages (optionally) and copy over to spool dir
#
if [ -n "$FAX_HEADER" ]
then
    $echo "\nPutting Header lines on top of pages..."

    hdrfont=$PBMFONT_HEADER_F
    test -z "$normal_res" || hdrfont=$PBMFONT_HEADER_N
fi

finalpg=""
for f in $pages
do
    nr=`expr $nr + 1`

    if [ -n "$FAX_HEADER" ]
    then
	cat $FAX_HEADER | sed -e "s;@T@;$phone;g" -e "s;@P@;$nr;g" \
			      -e "s;@M@;$maxnr;g" -e "s;@U@;$user;g" \
			      -e "s;@N@;$FULLNAME;g" \
			      -e "s;@D@;$verbose_to;g" \
			      -e "s;@ID@;$FAX_STATION_ID;g" \
			      -e "s;@S@;$new_seq;g" \
			      -e "s;@DATE@;`date`;g" \
	| pbmtext -font $hdrfont | pbm2g3 \
	| g3cat - $f | \
	$FAXQ_HELPER input $new_seq f$nr.g3 \
	&& rm $f
    else
	g3cat $f | \
	$FAXQ_HELPER input $new_seq f$nr.g3 \
	&& rm $f
    fi
    finalpg="$finalpg f$nr.g3"
done

if [ -z "$finalpg" -a -z "$poll_req" ]
then
    $echo "\nnothing to do (no cover page, no data)." >&2
    cd $FAX_SPOOL_OUT
    rmdir $spooldir
    exit 52
fi

#
# final step: create JOB file
#
$echo "phone $phone" >$job.q

[ -z "$force_user" ] || \
    $echo "user $force_user" >>$job.q

[ -z "$mail_to" ] || \
    $echo "mail $mail_to" >>$job.q

$echo "input $input_data" >>$job.q
$echo "pages " $finalpg >>$job.q

[ -z "$verbose_to" ] || \
    $echo "verbose_to $verbose_to" >>$job.q

[ -z "$poll_req" ] || \
    $echo "poll" >>$job.q

[ -z "$normal_res" ] || \
    $echo "normal_res" >>$job.q

[ -z "$job_priority" ] || \
    $echo "priority $job_priority" >>$job.q

[ -z "$TIME" ] || \
    $echo "time $TIME" >>$job.q

[ -z "$ACCT_HANDLE" ] || \
    $echo "acct_handle $ACCT_HANDLE" >>$job.q

$FAXQ_HELPER activate $new_seq < $job.q || exit 99

# clean up
rm $job.q
cd ..
rmdir $spooldir

if [ -z "`find $LAST_RUN -ctime -1 -print 2>/dev/null`" ]
then
    cat <<HERE

Fax queued successfully.

WARNING: faxrunq hasn't been run in the last 24 hours.
         Faxes only get sent out when faxrunq runs! Contact Fax administrator.

HERE
else
    if  [ -z "$TIME" ] ; then
	$echo "\nFax queued successfully. Will be sent at next \`\`faxrunq'' run.\n"
    else
	$echo "\nFax queued successfully. Will be sent at first \`\`faxrunq'' run\nafter the specified send time '$TIME' has been reached\n."
    fi
fi