#! /bin/sh
#----------------------------------------------------------------------------
# new_capifax
#
# Copyright (c) 2002-2004 Stefan Krister <stefan.krister@creative.chaos.de>
# Copyright (c) 2005      Frank Meyer <frank@eisfair(dot)org>
#
# Last Update: $Id$
#
# Parameters: $1 - Result Code
#             $2 - CAPI Disconnect Reason
#             $3 - Sender ID
#             $4 - Called Number or DDI
#             $5 - Num of Pages
#             $6 - File
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

# set -x

#
# read eisfax configuration
#
. /var/run/eisfax

#
# set file names and pathes
#
fax_spoolpath=/var/spool/fax/incoming
fax_logfile=/var/spool/fax/log/fax-empfang-journal.txt
fax_tmppath=/tmp
fax_newlogfile="/var/spool/fax/log/new_capifax.log"

#
# set parameters
#
curr_date=`/bin/date +%d%m%Y%H%M%S`
curr_longdate=`/bin/date +%d.%m.%Y`
curr_longtime=`/bin/date +%H:%M:%S`
filedate=`/bin/date +%Y-%m-%d-%H-%M-%S`

rmcommand ()
{
   rm -f $fax_tmppath/$$newfax.prn
   rm -f $fax_tmppath/$$newfax.ps
}

{
 echo "============================================================================"
 echo "`date` Start new_capifax"
 echo "`date` Received vars:"
 echo "`date` Result Code is $1"
 echo "`date` CAPI Disconnect Reason is $2"
 echo "`date` Sender ID is $3"
 echo "`date` Called Number or DDI is $4"
 echo "`date` Num of Pages is $5"
 echo "`date` File is $6"
 echo "----------------------------------------------------------------------------"
} >> $fax_newlogfile

cd /var/spool/fax/incoming

#
# number of sender
#
# - delete leading spaces
# - delete trailing spaces
# - replace ' ', '_', '.' with '-'
fax_sender=`echo "$3" | sed -e 's/ *//' -e 's/ *$//' -e 's/[ _\.]/-/g'`

if [ "$fax_sender" = "" ]
then
    fax_sender="unknown"
fi

echo "`date` fax_sender is $fax_sender" >> $fax_newlogfile

#
# number of pages
#
fax_pages="$5"
echo "`date` fax_pages is $fax_pages" >> $fax_newlogfile

neudatei=`echo "$6" | sed -e 's/ *//' -e 's/ *$//' -e 's/[ _]/-/g'`
echo "`date` neudatei is $neudatei" >> $fax_newlogfile

if [ -f /var/spool/fax/incoming/"$6" ]
then
    echo "`date` file /var/spool/fax/incoming/"$6" exists, moving to $neudatei" >> $fax_newlogfile
    mv /var/spool/fax/incoming/"$6" $neudatei >> $fax_newlogfile 2>&1
else
    echo "`date` file /var/spool/fax/incoming/"$6" don't exists, cannot move to $neudatei" >> $fax_newlogfile
fi

fax_file=$neudatei
fax_pdffile="$fax_spoolpath/$filedate.$fax_pages.$fax_sender.pdf"

{
 echo "`date` fax_file is $fax_file"
 echo "`date` fax_pdffile is $fax_pdffile" 
} >> $fax_newlogfile

if [ "$EISFAX_RECEIVE_AUTOPRINT" == "yes" ]
then
    # ein Postscriptfile draus machen
    echo "`date` autoprinting $fax_file" >> $fax_newlogfile
    cd $fax_spoolpath
    /usr/bin/efix -o ps $fax_file > $fax_tmppath/$$newfax.ps 2>> $fax_newlogfile

    if [ $? = 0 ]
    then
        {
         echo "`date` ps file $fax_tmppath/$$newfax.ps created"
         echo "`date` converting $fax_tmppath/$$newfax.ps to $EISFAX_RECEIVE_PRINTERDRIVER format"
        } >> $fax_newlogfile

        /usr/local/bin/gs -q                   \
        -sDEVICE=$EISFAX_RECEIVE_PRINTERDRIVER \
        -dNOPAUSE                              \
        -sPAPERSIZE=a4                         \
        -sOutputFile=$fax_tmppath/$$newfax.prn \
        -- $fax_tmppath/$$newfax.ps            \
        >> $fax_newlogfile 2>&1
            
        if [ $? = 0 ]
        then
            echo "`date` printer file $fax_tmppath/$$newfax.prn created" >> $fax_newlogfile
        else
            echo "`date` cannot create printer file $fax_tmppath/$$newfax.prn ..." >> $fax_newlogfile
        fi
    else
        echo "`date` cannot create ps file $fax_tmppath/$$newfax.ps, exiting..." >> $fax_newlogfile
    fi

    echo "`date` printing $fax_tmppath/$$newfax.prn on $EISFAX_RECEIVE_PRINTERQUEUE" >> $fax_newlogfile
    /usr/bin/lpr -P$EISFAX_RECEIVE_PRINTERQUEUE $fax_tmppath/$$newfax.prn  >> $fax_newlogfile 2>&1

    if [ $? = 0 ]
    then
        echo "`date` printed $fax_tmppath/$$newfax.prn on $EISFAX_RECEIVE_PRINTERQUEUE" >> $fax_newlogfile
        /usr/bin/logger -is FAX gedruckt
        rmcommand
    else
        echo "`date` cannot print $fax_tmppath/$$newfax.prn on $EISFAX_RECEIVE_PRINTERQUEUE ..." >> $fax_newlogfile
    fi
fi

#
# convert from TIFF to PDF
#
echo "`date` converting $fax_file to pdf" >> $fax_newlogfile
/usr/bin/fax2pdf "$fax_file" $fax_pdffile >> $fax_newlogfile 2>&1

if [ -f "$fax_pdffile" ]
then
    echo "`date` $fax_file converted to $fax_pdffile" >> $fax_newlogfile

    if [ "$EISFAX_DO_DEBUG" != "yes" ]
    then
        # pdf file has been created - delete files
        rm $fax_file
    fi

    chmod 666 $fax_pdffile
else
    echo "`date` cannot convert $fax_file to $fax_pdffile ..." >> $fax_newlogfile
fi

#
# write log entry
#
echo $curr_longdate                        \
     $curr_longtime                        \
     $fax_sender                           \
     $fax_pages                            \
     $curr_date.$fax_pages.$fax_sender.pdf \
     >> $fax_logfile

if [ "$EISFAX_RECEIVE_AUTOMAIL" = "yes" ]
then
    echo "`date` mailing $fax_pdffile" >> $fax_newlogfile

    # send fax as mail
    /usr/bin/faxrcvd-mail-pdf "$EISFAX_RECEIVE_AUTOMAIL_MAILADDR" \
                              "$fax_pdffile"|/usr/sbin/sendmail   \
                              "$EISFAX_RECEIVE_AUTOMAIL_MAILADDR" \
                               >> $fax_newlogfile 2>&1

    if [ $? = 0 ]
    then
        echo "`date` mailed $fax_pdffile" >> $fax_newlogfile
    else
        echo "`date` cannot mail $fax_pdffile ..." >> $fax_newlogfile
    fi
fi

exit 0