#! /bin/sh
#
# /var/spool/hylafax/bin/faxrcvd
# Noel Burton-Krahn <noel@burton-krahn.com>
# Sept 4, 1999
#
# 
# faxrcvd file devID commID error-msg
#
# modified faxrcvd from hylafax which calls faxrcvd-mail
#
if [ $# != 4 ]; then
    echo "Usage: $0 file devID commID error-msg"
    exit 1
fi
FILE="$1"
DEVICE="$2"
COMMID="$3"
MSG="$4"
TRUNCFILE=`echo $FILE | sed -e 's/\.tif//'`

test -f etc/setup.cache || {
    SPOOL=`pwd`
    cat<<EOF

FATAL ERROR: $SPOOL/etc/setup.cache is missing!

The file $SPOOL/etc/setup.cache is not present.  This
probably means the machine has not been setup using the faxsetup(1M)
command.  Read the documentation on setting up HylaFAX before you
startup a server system.

EOF
    exit 1
}
. etc/setup.cache

/usr/bin/tiff2ps -a -O $SPOOL/$TRUNCFILE.ps $SPOOL/$FILE
cp /$SPOOL/$FILE /home/tmpfaxbackup
# linea originale
# /usr/bin/ps2pdf $SPOOL/$TRUNCFILE.ps $SPOOL/$TRUNCFILE.pdf
# aggiunta da dado per impaginare in a4
/usr/bin/ps2pdf -sPAPERSIZE=a4 $SPOOL/$TRUNCFILE.ps $SPOOL/$TRUNCFILE.pdf
# aggiunta da dado per spostare il file in un file system condiviso
# con SAMBA e poi dare le permissione
cp $SPOOL/$TRUNCFILE.pdf /home/tmpfax
chmod 777 /home/tmpfax/*

# cp $SPOOL/$FILE /home/tmpfaxbackup
# copio il file tiff originale in /tmpfaxbackup
# cp $SPOOL/$FILE /home/tmpfaxbackup
# copio il file .ps in /home/tmpfaxbackup
# chmod 777 /home/tmpfaxbackup/*


#TOADDR=FaxMaster
TOADDR=faxmaster@dado.eldata
PATH="$SPOOL/sbin:$SPOOL/bin:$PATH"

#
# If you're using faxcron and would like to have the recvq file removed if
# mail delivery was successful, then uncomment this if clause and comment
# the line(s) following.
#
#if faxrcvd-mail "$FILE" "$DEVICE" "$COMMID" "$MSG" "$TOADDR" fax "$TRUNCFILE.pdf" | \
#/usr/sbin/sendmail $TOADDR; then
#	rm -f $SPOOL/$TRUNCFILE*
#fi

if faxrcvd-mail "$FILE" "$DEVICE" "$COMMID" "$MSG" "$TOADDR" fax "$TRUNCFILE.pdf" | \
/usr/sbin/sendmail $TOADDR; then
	rm -f $SPOOL/$TRUNCFILE.pdf
fi
