#!/bin/sh
###########################################################################
#                                                                         #
# faxfilter for mgetty+sendfax                                            #
#                                                                         #
#                                                                         #
# Author     : Wundrig Roland <roland@cygnus.muc.de>                      #
#                                                                         #
# changes    :  26.08.1995 Programm erstellt                              #
# 		16.02.1999 modified for use with (nearly)                 #
#                          all word-processors printing PostScript        #
#			   <Christoph.Meier@fernuni-hagen.de>             #	
#                                                                         #
###########################################################################
#
#
# Installation (SuSE):
#
# you must have been installed:
#
# serie a		gawk
# serie ap		gs, gs_lib, gs_fonts
# serie n		mgetty, sendfax	
#
# add to your /etc/printcap:
#
# faxlp|fax_now:\
#	:lp=/dev/null:\
#	:sd=/var/spool/lpd/faxspool:\
#	:lf=/var/spool/lpd/faxspool/err.log:\
#	:if=/var/spool/faxspool/faxfilter:\
#	:sh:\
#	:sf:\
#	:mx#0:
#
# place this (faxfilter) scripts in /var/spool/lpd/faxspool 
# and make it executable.
#
###########################################################################
#
# How to fax:
#
# Add to your Text the following text:
#      Fax-Nr : 012-3456-789
# where 012-3456-789 is the fax number (without spaces!).
#
# You can use either upper- or lowercase characters for 'Fax-Nr :'. 
# The Text 'Fax-Nr:' have to be as is, with or without blanks.
#
# Now print it as Postscript to 'faxlp'
# 
# The filter converts the PS-file to ASCII and scans the ASCII-file
# for the 'Fax-Nr : '... .
# If a fax-number is found the PS-file will be processed by faxspool with
# that fax-number.
#
# The sender will receive a mail, wether the fax was sent or an error
# ocurred while scanning the fax-number.
# 
# If you have installed SAMBA you can add the fax-printer in SMB.conf
# described by /usr/doc/packages/samba/textdocs/Faxing.txt.
# Then you are able to use your fax-modem with MS-Windows.
#
# have much fun :-)
#
# P.S.: You may install two fax-devices. One for sending immediately
#       and one for delayed sending of faxes.
#
###########################################################################
#
# Settings
#

# uncomment for delayed sending after 18:00 (for example)
# and add '-t $SENDTIME' to the faxspool-command at the end of this script 
#SENDTIME="18:00"

# use mail for sending status back
#
MAILER="/bin/mail"
PATH="$PATH:/usr/X11/bin"

# store temporary files to:
#
FAXFILE=/tmp/tmp_faxlp.$$
FAXTEXT=/tmp/tmp_faxtxt.$$
USERNAME=""
HOSTNAME=""

umask 027

# get parameters
#
while :
do
    case "$1" in
# catch username 
        -n) USERNAME="$2"
	    shift ; shift
	    ;;
# catch hostname
        -h) HOSTNAME="$2"
	    shift ; shift
	    ;;
# ignore anything else
       -*)  shift
            ;;
# exit loop
	*) break
    esac
done

# If no username is given use Postmaster as mailadress
#
if [ "$USERNAME" = "nobody" ];
   then
    MAILNAME="postmaster"
   else
    MAILNAME=$USERNAME
fi

# save fax to temp-folder
#
cat > $FAXFILE

# convert PS to ASCII for fax-number checking
# and save to temp-folder
#
/usr/bin/ps2ascii $FAXFILE $FAXTEXT 

# scanning fax-text for phone-number
#
TELEPHONE=`awk '{ IGNORECASE=1 } /FAX-NR ?: ?[0-9-]*/ \
         {  gsub(/-/,""); \
            anfang=match($0,/ ?: ?/); \
            anfang=anfang+match(substr($0,anfang),/[0-9]/)-1; \
            ende=match(substr($0,anfang),/\>/)-1; \
            printf ("%s",substr($0,anfang,ende)) \
         }' $FAXTEXT`

# remove ASCII-File
#
rm $FAXTEXT

# fax-number found? -> send fax
# else send error-message
#
if [ "$TELEPHONE" = "" ];
   then
       # send error-message
       #
    	(
	echo "Error! Can't find fax-number in the text!"
	echo
	echo "1) Try again with (maybe in a seperate line):"
	echo "        FAX-NR : XXXX-XXXX"
	echo "   where the X are the numbers of the destination fax-number."
	echo "2) Don't use any characters between the numbers."
	echo "3) The Fax-Nr should appear only once in the text."
	echo
	echo "If nothing works, contact your fax-administrator."
	) | \
	$MAILER -s "FAX ERROR" $MAILNAME
   else

       # SENDING THE FAX
       # add ' -t $SENDTIME' after 'faxspool' to the following line
       # if you want to sent delayed
       # be sure you uncommented SENDTIME="18:00" at the beginning
       # of the script
 	
	faxspool -u $USERNAME -f $USERNAME $TELEPHONE $FAXFILE 2>&1 | \
	$MAILER -s "Fax sent to # $TELEPHONE #" $MAILNAME
fi

# remove temporary FAXFILE
#
rm $FAXFILE

exit 0
###########################################################################
