#! /bin/bash

# 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.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307  USA
#

## Brother CUPS Fax filter
# Copyright (C) 2005-  Brother Industries, Ltd.


LOGFILE="/dev/null"
DEBUG=0
LOG_LATESTONLY=1
FAXNUMLABEL="fax-number="
FAXNUMSLABEL="fax-numbers="
BROADCASTDBLABEL="fax-broadcast="



BASEDIR="/opt/brother/fax/brmfcfax"
LPDDIR="${BASEDIR}/lpd"
CONFIGFILE="/${BASEDIR}/config/brmfcfax.config"
LISTSDIR="/${BASEDIR}/lists"

if [ "$DEBUGLPDDIR" != "" ];then
   LPDDIR=$DEBUGLPDDIR
fi
if [ "$DEBUGCONFIGFILE" != "" ];then
   CONFIGFILE=$DEBUGCONFIGFILE
fi
if [ "$DEBUGLISTSDIR" != "" ];then
   LISTSDIR=$DEBUGLISTSDIR
fi

source "$CONFIGFILE"

errorcode=0
lpderrcode=0

if [ "$brpcfaxdebug" != '' ];then
    DEBUG=$brpcfaxdebug
fi


if [ "$(echo "$5" | grep brpcfax-debugoption=1)" !=  "" ]; then
    DEBUG=1
fi
if [ "$(echo "$5" | grep brpcfax-debugoption=2)" !=  "" ]; then
    DEBUG=2
fi
if [ "$(echo "$5" | grep brpcfax-debugoption=3)" !=  "" ]; then
    DEBUG=3
fi
if [ "$(echo "$5" | grep brpcfax-debugoption=4)" !=  "" ]; then
    DEBUG=4
fi

mkdir -p /tmp/brother_brpcfax

if [ $DEBUG != 0 ]; then
    LOGFILE=/tmp/br_faxfilter_debug_log
    if [ $DEBUG = 1 ]; then
	rm /tmp/br_faxfilter_debug_log 2>/dev/null
    fi
fi


if [ "$PPD" = "" ]; then
    PPDC=$(printenv | grep "PPD=")
    PPD=$(echo "$PPDC" | cut -d= -f2)
    if [ "$PPD" = "" ]; then
	PPD="/usr/share/cups/model/brfax_cups.ppd"
    fi
fi

if [ $LOGFILE != "/dev/null" ]; then
  if [ $LOG_LATESTONLY = "1" ]; then
    rm -f $LOGFILE
    date                                                       >$LOGFILE
  else
    if [ -e $LOGFILE ]; then
	    date                                               >>$LOGFILE
    else
	    date                                               >$LOGFILE
    fi
  fi
    echo "args = $*"                                           >>$LOGFILE
    echo "arg0 = $0"                                           >>$LOGFILE
    echo "arg1 = $1"                                           >>$LOGFILE
    echo "arg2 = $2"                                           >>$LOGFILE
    echo "arg3 = $3"                                           >>$LOGFILE
    echo "arg4 = $4"                                           >>$LOGFILE
    echo "arg5 = $5"                                           >>$LOGFILE
    echo "arg6 = $6"                                           >>$LOGFILE

    echo "arg7 = $7"                                           >>$LOGFILE
    echo "arg8 = $8"                                           >>$LOGFILE
    echo "arg9 = $9"                                           >>$LOGFILE

    echo "PPD  = $PPD"                                         >>$LOGFILE
fi

INPUT_TEMP_PS=$(mktemp /tmp/brother_brpcfax/br_input_faxps.XXXXXX)
INPUT_TEMP_DT=$(mktemp /tmp/brother_brpcfax/br_input_faxdata.XXXXXX)
INPUT_TEMP_WK=$(mktemp /tmp/brother_brpcfax/br_input_work.XXXXXX)
INPUT_TEMP_LG=$(mktemp /tmp/brother_brpcfax/br_input_log.XXXXXX)
INPUT_TEMP_NM=$(mktemp /tmp/brother_brpcfax/br_input_nums.XXXXXX)

if [ $# -ge 7 ]; then
       cat "$6"> "$INPUT_TEMP_PS"
else
       cat     > "$INPUT_TEMP_PS"
fi



if [ "$(grep "\*DefaultQuality:" $PPD | grep -i "FINE")" != "" ]; then
    faxquality="FINE"
else
    faxquality="STD"
fi

tmp="$(grep "\*DefaultPageSize:" $PPD)"
if [ "$(echo "$tmp" | grep A4)" != "" ]; then
    paper=A4
elif [ "$(echo "$tmp" | grep -i Letter)" != "" ]; then
    paper=LETTER
elif [ "$(echo "$tmp" | grep -i Legal)" != "" ]; then
    paper=LEGAL
else
    paper=A4
fi


if [ "$(echo "$5" | tr ' ' '\n' | grep -i 'PageSize=A4')" != '' ]; then
    paper=A4
fi
if [ "$(echo "$5" | tr ' ' '\n' | grep -i 'PageSize=LETTER')" != '' ]; then
    paper=LETTER
fi
if [ "$(echo "$5" | tr ' ' '\n' | grep -i 'PageSize=LEGAL')" != '' ]; then
    paper=LEGAL
fi



if [ "$(echo "$5" | tr ' ' '\n' | grep -i 'Quality' | grep -i  'Fine')" != '' ]; then
    faxquality="FINE"
fi
if [ "$(echo "$5" | tr ' ' '\n' | grep -i 'Quality' | grep -i  'Standard')" != '' ]; then
    faxquality="STD"
fi
if [ "$(echo "$5" | tr ' ' '\n' | grep -i 'Quality' | grep -i  'STD')" != '' ]; then
    faxquality="STD"
fi

num='&&'
broadcastdb='&&'

if [ "$(echo -n "$5" |  grep ${FAXNUMLABEL})" != '' ];then
    num=$(echo -n "$5" | sed s/"^.*${FAXNUMLABEL}"//g |  sed s/' '.*$//g)
elif [ "$(echo -n "$5" |  grep BRFaxNumber=Custom\.)" != '' ];then
    num=$(echo -n "$5" | sed s/"^.*BRFaxNumber=Custom\."//g  |  sed s/' '.*$//g)
elif [ "$(echo -n "$5" |  grep BRFaxNumber)" != '' ];then
    num=$(echo -n "$5" | sed s/"^.*BRFaxNumber="//g  |  sed s/' '.*$//g)
else
   tmp="$(grep ^\*DefaultBRFaxNumber: $PPD)"
   if [ "$tmp" != '' ];then
       num=$(echo -n "$tmp" | sed s/"^.*:"//g  |\
             tr -d '\n' | tr -d '\r' | tr -d ' ' |\
             sed s/"Custom."//g)

       if [ "$num" = 'Custom' ];then
	   num='&&'
       fi
   fi
fi



echo "FAX number : $num"  >>$LOGFILE

if [ "$(echo -n "$5" |  grep ${BROADCASTDBLABEL})" != '' ];then
    broadcastdb=$(echo -n "$5" | sed s/"^.*${BROADCASTDBLABEL}"//g |\
                                  sed s/' '.*$//g)
else 
    if [ "$num" = "Broadcast" ];then
	num=''
    elif [ "$(echo -n "$5" |  grep BRFaxBroadcast)" != '' ];then
	broadcastdb=$LISTSDIR/$(echo -n "$5" | sed s/"^.*BRFaxBroadcast="//g  |\
                                  sed s/' '.*$//g)
    else
	tmp="$(grep ^\*DefaultBRFaxBroadcast: $PPD)"
	if [ "$tmp" != '' ];then
	    broadcastdb=$LISTSDIR/$(echo -n "$tmp" | sed s/"^.*:"//g  |\
             tr -d '\n' | tr -d '\r' | tr -d ' ' |\
             sed s/"Custom."//g)
	fi
    fi 
fi


nums="$(echo "$5" | tr ' ' '\n' | grep -i "$FAXNUMSLABEL" | sed s/"$FAXNUMSLABEL"//g)"
if [ "$nums" != "" ];then
    num=""
    echo "$nums" | tr ',' '\n' > "$INPUT_TEMP_NM"
    broadcastdb=$INPUT_TEMP_NM
fi


if [ "$num" = "" ];then
    num="&&"
fi



if [ -e "$LPDDIR"/filterBRFAX ]; then
    if [ $DEBUG != 0 ]; then
     echo     bash "$LPDDIR"/filterBRFAX        \
	-faxoption-papersize     $paper        \
        -faxoption-quality       $faxquality   \
	-faxoption-faxnumber     $num          \
	-faxoption-faxnumlist    $broadcastdb  \
	-faxoption-ignorercfile  ON            \
	-faxoption-debug         $DEBUG        \
	-faxoption-logfile       "$LOGFILE"    \
	-faxoption-title         "$1:$2:$3"    \
	< "$INPUT_TEMP_PS"                                    >>$LOGFILE
     echo "----------------- lpd filter -----------------"   >>$LOGFILE
    fi

    cat   "$INPUT_TEMP_PS"  |                    \
    bash "$LPDDIR"/filterBRFAX                  \
	-faxoption-papersize     $paper        \
        -faxoption-quality       $faxquality   \
	-faxoption-faxnumber     $num          \
	-faxoption-faxnumlist    $broadcastdb  \
	-faxoption-ignorercfile  ON            \
	-faxoption-debug         $DEBUG        \
	-faxoption-logfile       $LOGFILE      \
	-faxoption-title         "$1:$2:$3"    
    lpderrcode=$?
    if [ "$lpderrcode" != 0 ];then
	errorcode=-134
    fi
else
    errorcode=-32
fi


rm -f  "$INPUT_TEMP_PS"
rm -f  "$INPUT_TEMP_DT"
rm -f  "$INPUT_TEMP_WK"
rm -f  "$INPUT_TEMP_LG"
rm -f  "$INPUT_TEMP_NM"


if [ "$errorcode" != 0 ];then
    echo "ERROR: BRFAX $0 err=$errorcode,$lpderrcode"  >&2
    if [ "$TXLOG" = YES ] || [ "$TXLOG" = Yes ][ "$TXLOG" = yes ];then
	touch "$TXLOGDIR"/"$TXLOGFILE"
	cat "$TMPCUPSLOGFILE" >> "$TXLOGDIR"/"$TXLOGFILE"
    fi

    exit 1
fi
exit 0


