#!/bin/sh
#
# faxspool - sample skript how to spool fax input data to a spool
#            directory, creating jobs to be run by faxrunq
#
# sccsid: @(#)faxspool	1.3 93/10/05 (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 pbmtog3)
#   .pgm -> PortableGrayMap (use pgmtopbm | pbmtog3)
#   .ppm -> PortablePixMap (use ppmtopgm | pgmtopbm | pbmtog3)
#   .g3  -> raw G3 fax data
#   .lj  -> HP Laserjet PCL4 (use hp2pbm)
#
# 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)

FAX_SPOOL=/usr/spool/fax
FAX_SPOOL_OUT=$FAX_SPOOL/outgoing
FAX_SEQ=$FAX_SPOOL_OUT/.Sequence

# fax phone directories - format: <alias> <fax phone number>
GLOBAL_PHONE_DIR=/usr/local/lib/fax/aliases
PRIVATE_PHONE_DIR=$HOME/.faxnrs

# permissions - see the "crontab" manual page for a description
#               how the files have to be set up
FAX_ALLOW=/usr/local/lib/fax/fax.allow
FAX_DENY=/usr/local/lib/fax/fax.deny

# you have to adapt this to your local system!
#
# for creating the fax page header, pbmtext is used, and this specifies
# the font file to use
#
PBMFONT=/u/gert/mgetty/fax/cour24i.pbm

#
# validate user
#
user=`logname` || user=$LOGNAME
[ -z "$user" ] && user=$USER

if [ -r $FAX_ALLOW ]
then
    if grep "^$user$" $FAX_ALLOW >/dev/null
    then :
    else
	echo "You are not allowed to use the fax service. Sorry." >&2
	exit 1
    fi
elif [ -r $FAX_DENY ]
then
    if grep "^$user$" $FAX_DENY >/dev/null
    then
	echo "You are not allowed to use the fax service. Sorry." >&2
	exit 2
    fi
elif [ "$user" != "root" ]
then
    echo "Neither $FAX_ALLOW nor $FAX_DENY exist," >&2
    echo "so only root may use the fax service. Sorry." >&2
    exit 2
fi

#
# above was for authentification purposes (->use logname), this is
# for mailing back the result
#

test -z "$USER" && USER=$LOGNAME
test -z "$USER" && USER=$user

#
# check syntax
#

usage="Usage: $0 [-t time] phone file ..."
if [ $# -lt 2 ] ; then
    echo "$usage" >&2 ; exit 3 ; fi

phone=$1 ; shift
if expr "$phone" : "[-0-9TtPpW]*$" >/dev/null ; then :
else
    alias="$phone"
    phone=""
    if [ -r $PRIVATE_PHONE_DIR ]
    then
	phone=`grep "^$alias " $PRIVATE_PHONE_DIR | cut -f2 -d" "`
    fi
    if [ -z "$phone" -a -r $GLOBAL_PHONE_DIR ]
    then
	phone=`grep "^$alias " $GLOBAL_PHONE_DIR | cut -f2 -d" "`
    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
    echo "sending fax to '$alias' using phone number '$phone'..."
fi

#
# check, if all the files exist & are readable
#

for file
do
    if [ ! -r $file ] ; then
	echo "$0: cannot open '$file'!" ; exit 5
    fi
done

#
# check spool directory permissions
#

if [ ! -w $FAX_SPOOL_OUT ] ; then
    echo "cannot write to $FAX_SPOOL_OUT!" ; exit 6 ; fi

if [ ! -f $FAX_SEQ ] ; then echo 000000 > $FAX_SEQ ; chmod 666 $FAX_SEQ ; fi

if [ -f $FAX_SEQ -a ! -w $FAX_SEQ ] ; then
    echo "cannot write to $FAX_SEQ!" ; exit 6 ; fi

#
# get unique directory name (well, at least: try to)
# FIXME: one should worry about concurrent processes here! This is a clear
#        race condition!
#

new_seq=`awk '{ printf "%06d", $1 + 1 >"'$FAX_SEQ'";
		printf "%06d", $1 + 1 }' $FAX_SEQ `

spooldir=$FAX_SPOOL_OUT/F$new_seq.$$

if [ -d $spooldir ] ; then
    echo "Ummm, strange - $spooldir exists (FIX THE SOURCE!)" ; exit 6 ; fi

umask 022
if mkdir $spooldir ; then :
else echo "Cannot make $spooldir" ; exit 6 ; fi

#
# now: spool all the files to $spooldir
#

echo "spooling to $spooldir..."

for file
do
    echo "spooling $file..."
    format=""
    base=`basename $file`

#
# try to determine file type by extention (won't work for print spooler!)
#
    case $file in
	*.g3)	format="g3" ; base=`basename $file .g3` ;;
	*.ps)	format="ps" ; base=`basename $file .ps` ;;
	*alw)	format="ps" ; base=`basename $file alw` ;;
	*.dvi)	format="dvi"; base=`basename $file .dvi`;;
	*.pbm)	format="pbm"; base=`basename $file .pbm`;;
	*.pgm)	format="pbm"; base=`basename $file .pgm`;;
	*.ppm)	format="pbm"; base=`basename $file .ppm`;;
	*.t)	format="ascii"; base=`basename $file .t`;;
	*.lj)   format="lj"; base=`basename $file .lj`;;
    esac

# 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 "`file $file`" in
	    *"English text"*)	format="ascii" ;;
	    *"ascii text"*)	format="ascii" ;;
	    *"News 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" ;;
	    *Digifax*)	format="g3" ;;
	    *DVI*)	format="dvi" ;;
	    *postscript*)	format="ps" ;;
	    *PostScript*)	format="ps" ;;
	esac

# if file told us, it's an ascii text, or if we still don't know, try
# looking at the first line

	if [ -z "$format" -o "$format" = "ascii" ]
	then
	    case "`head -1 $file`" in
		"%!*")	format="ps" ;;
		P1*|P4*)	format="pbm" ;;
		P2*|P5*)	format="pgm" ;;
		P3*|P6*)	format="ppm" ;;
	    esac
	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
	exit 7
    fi

    echo "$file is format: $format"

    target=$spooldir/$base

    if
    case $format in
	ps)	cat $file |
		gs -sDEVICE=dfaxhigh -sOutputFile=$target%02d -dNOPAUSE \
		    -dSAVER - ;;
	ascii)	gs -sDEVICE=dfaxhigh -sOutputFile=$target%02d -dNOPAUSE \
		    -dSAVER -r204x196 -- gslp.ps $file ;;
	pbm)	pbmtog3 $file >$target-1 ;;
	pgm)	pgmtopbm $file |
		pbmtog3 >$target-1 ;;
	ppm)	ppmtopgm $file |
		pgmtopbm |
		pbmtog3 >$target-1 ;;
	g3)	cp $file $target-1 ;;
#	dvi)    dvialw <$file |
#		gs -sDEVICE=dfaxhigh -sOutputFile=$target%02d -dNOPAUSE \
#                    -dSAVER - ;;
	dvi)	dvips $file -o \
		!"gs -sDEVICE=dfaxhigh -sOutputFile=$target%02d -dNOPAUSE -"
		;;
	lj)	cat $file | hp2hig3 -r$target
		;;
	*) echo "$0: unknown format: $format!" ;;
    esac 
    then : ; else
	echo "Error spooling $file - aborting!" ; exit 8
    fi

done

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

job=$spooldir/JOB

pages=`ls -rt $spooldir`
#
# concatenate header with pages
#
cd $spooldir
set $pages
nr=0
for f
do
    nr=`expr $nr + 1`
    echo "     FAX  FROM:  EDV-BERATUNG GERT DOERING  +49 89 3243328     TO: $phone    PAGE: $nr OF $#" \
    | pbmtext -font $PBMFONT | pbmtog3 \
    | g3cat - $f > f$nr.g3 \
    && rm $f
done

pages=`ls -rt $spooldir`

# obsolete
#echo "           Fax FROM "${LOGNAME:=`id`}"@"`hostname`"   +49-89-3243328   TO   $phone    PAGES: "`echo $pages | wc -l` | pbmtext -font $PBMFONT |pbmtog3 >$header
#echo "header HEADER" >>$job

echo "phone $phone" >$job
echo "user $USER" >>$job
echo "pages " $pages >>$job
