#!/bin/bash
# 
# Faxing with HylaFax through a faxprinter
# 
# This script is heavily depending (just about copied) from the work of:
#     -------------------------------------------------
#     Faxfilter für MS-Windows und lpd und mgetty-paket
#     Author     : Wundrig Roland <roland@cygnus.muc.de>    
#     -------------------------------------------------
# Installation:
# Create in your smb.conf something like:
# [Faxserver]
#        comment = Fax server
#        print command = /var/spool/fax/bin/sambafax -n %u %s
#        path = /tmp/
#        create mask = 0700
#        printable = Yes
#        read only = yes
# 
# 2. Store this file as /var/spool/fax/bin/sambafax or in the bin
#    directory of your hyla tree
# 
# 3. restart samba
# 
# 4. On your samba client, install a new printer with a postscript
#    driver (like an apple laserwriter) to print to \\YOURSERVER\Faxserver
# 
# 5. Create a fax using your favorite wordchewer and take up a line like:
#        Fax : +31 20 123456
#    and print to your new faxprinter
#    We expect a country code first!
#    Adjust the IPREFIX variable below to point to the international
#    dialing prefix. 
#
# The intellectual (c) remains with Wundrig Roland
# Previous rev created by ignace.suy@reeel.nl, september 2000
# This rev created by dleeuw@made-it.com, may 2002
# DL: added support for identical lines for send and receive fax FAXLINE
# DL: added canonical phone number support as used by hylafax
# DL: added alway send mail option through SEND_MAIL_ON_SUCCESS

# constants
SENDMAIL="/usr/sbin/sendmail"
SENDFAX="/usr/bin/sendfax"
FAXLINE="(Fax:" 		   # How do we detect that we have a PostScript line
		           # that holds the fax number.
IPREFIX="00"  		   # International Dialing Prefix
CPREFIX="31"               # Local Country Prefix
APREFIX="20"               # Area Prefix
LPREFIX="0"                # Long Distance Prefix
SEND_MAIL_ON_SUCCESS=      # Switch to -D to always receive mail and not only
			   # on failure
DOMAIN="domain.nl"         # Domain after the @-sign for e-mail
COMPANY="Company name"     # Company name
FAXMASTER="postmaster"     # Who get's mail when we don't have a user

# retrieve the username and hostname from the paramaters
while :
do
    case "$1" in
        -n) Username="$2"
            shift ; shift
            ;;
        -h) Hostname="$2"
            shift ; shift
            ;;
       -*)  shift
            ;;
        *) break
    esac
done

FAXFILE="/tmp/$1"

if [ "x$Username" = "x" ]; then
	Username="nobody"
fi

# if the samba user is anonymous then send mails to the postmaster
if [ "$Username" = "nobody" ];
   then
    MailTo="$FAXMASTER"
   else
    MailTo=${Username}
fi

# Let's parse the ps file to see if we can substract the fax-number.
FAXDETECT="0"
IFS=$'\n'
for LINE in $(<$FAXFILE); do
        CMPLINE=${LINE/$FAXLINE/}
        if [ ${#LINE} -gt ${#CMPLINE} ]; then
                FAXNUM=${LINE%)*}
                FAXNUM=${FAXNUM//[![:digit:]]/}
		# From now on we should have the fax number
                # Let's create a canonical form
                # +<country><area><number>

                # Replace the Int. Prefix by +
                FAXNUM=${FAXNUM//#$IPREFIX/+}
                # Replace the Area Prefix by +Country code
                FAXNUM=${FAXNUM//#${LPREFIX}${APREFIX}/+${CPREFIX}${APREFIX}}
                FAXDETECT=$(($FAXDETECT +1))

		FAXDETECT=$(($FAXDETECT +1))
	# This is a counter that detects how often the FAXLINE is found
	# If you have identical strings voor send fax number and receive 
	# fax number you can set the next elif to 2, else set it to 1
	elif [ $FAXDETECT = "1" ]; then
                break;
        fi
done
unset IFS

# if faxnumber is found fax the tempfile
# we donot check the validity of the faxnumber, let sendfax do this...
if [ "${FAXNUM}" = "$IPREFIX" ] ; then
    (echo "To: ${MailTo}@$DOMAIN"
     echo "From: $COMPANY Sambafaxserver <fax>"
     echo "Subject: Your fax attempt failed"
     echo ""                                                                 
     echo "You probably didn't supply a faxnumber in your fax of `date`"
     echo ""
     echo "Make sure someting like this is in your faxfile:"
     echo "   Fax : +31 (26) 123456"
    ) | 2>&1 $SENDMAIL -ffax -oi ${MailTo}@$DOMAIN
else
   $SENDFAX $SEND_MAIL_ON_SUCCESS -n -h faxserver -f "${MailTo}@$DOMAIN" -d ${FAXNUM} ${FAXFILE}
fi

# remove the temp file
rm -f ${FAXFILE}

# end of show

