#! /bin/sh
#
# Brother Print filter
# Copyright (C) 2005 Brother. Industries, Ltd.

# 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
#

printer_model="hl4050cdn"
printer_name=`echo $printer_model | tr '[a-z]' '[A-Z]'`
device_name=`echo $printer_name | eval sed -e 's/MFC/MFC-/' -e 's/DCP/DCP-/' -e 's/FAX/FAX-/'`
pcfilename=`echo $printer_name | tr -d '[A-Z]'`
device_model="Printer"
tmp_filter=/var/tmp/brlpdwrapper${printer_model}

if [ "$1" = '-e' ]; then
  lpadmin -x ${printer_name}
  rm -f /usr/share/cups/model/br${printer_model}.ppd
  rm -f /usr/share/ppd/br${printer_model}.ppd
  rm -f /usr/lib/cups/filter/brlpdwrapper${printer_model}
  rm -f /usr/lib64/cups/filter/brlpdwrapper${printer_model}
  rm -f /usr/local/Brother/${device_model}/${printer_model}/cupswrapper/brcupsconfpt1
if [  -e /etc/init.d/cups ]; then
   /etc/init.d/cups restart
elif [  -e /etc/init.d/cupsys ]; then
   /etc/init.d/cupsys restart
fi
#  /etc/init.d/cups restart
  exit 0
fi
if [ "$1" = "-r" ]; then
  lpadmin -x ${printer_name}
if [  -e /etc/init.d/cups ]; then
   /etc/init.d/cups restart
elif [  -e /etc/init.d/cupsys ]; then
   /etc/init.d/cupsys restart
fi
#  /etc/init.d/cups restart
  exit 0
fi
if [ "$1" = "help" ] || [ "$1" = "-h" ]; then
  echo   'option -h : help'
  echo   '       -i : install'
  echo   '       -e : uninstall'
  echo   '       -r : remove printer'
  exit 0
fi
#mkdir -p /usr/local/Brother/${device_model}/${printer_model}/filter
#mkdir -p /usr/lib/cups/filter

if [ -e "/usr/local/Brother/${device_model}/${printer_model}/lpd/filter${printer_model}" ]; then
  :
else
  echo "ERROR : Brother LPD filter is not installed."
fi
rm -f /usr/share/cups/model/br${printer_model}.ppd
if [ -d "/usr/share/cups/model" ]; then
  ppd_file_name=/usr/share/cups/model/br${printer_model}.ppd
else
  ppd_file_name=/usr/share/ppd/br${printer_model}.ppd
fi

if [ -e "/usr/local/Brother/${device_model}/${printer_model}/cupswrapper/br${printer_model}.ppd" ];	then
cp "/usr/local/Brother/${device_model}/${printer_model}/cupswrapper/br${printer_model}.ppd" $ppd_file_name
fi

chmod 644 $ppd_file_name

if [ -d /usr/share/ppd ]
then
if [ -d /usr/share/cups/model ]
then
	cp $ppd_file_name /usr/share/ppd/br${printer_model}.ppd
	chmod 644 /usr/share/ppd/br${printer_model}.ppd
fi
fi

#
#	create temporary CUPS Filter
#

cat <<!ENDOFWFILTER! >$tmp_filter
#! /bin/sh
#
# Brother Print filter  >>
# Copyright (C) 2005 Brother. Industries, Ltd.
#                                    Ver1.10

# 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
#
LOGFILE="/dev/null"
LOGLEVEL="1"
LOGCLEVEL="7"
DEBUG=0
NUPENABLE=0
LOG_LATESTONLY=1
errorcode=0

if [ \$DEBUG != 0 ]; then
    LOGFILE=/tmp/br${printer_model}_cupsfilter_debug_log
fi

PPDC=\`printenv | grep "PPD="\`
PPDC=\`echo \$PPDC | sed -e 's/PPD=//'\`

if [ "\$PPDC" = "" ]; then
    PPDC="/usr/share/cups/model/br${printer_model}.ppd"
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 "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 "PPD  = \$PPD"                                         >>\$LOGFILE
fi

INPUT_TEMP_PS=\`mktemp /tmp/br_input_ps.XXXXXX\`

nup="cat"
if [ "`echo \$5 | grep 'Nup='`" != '' ] && [ \$NUPENABLE != 0 ]; then

	if   [ "`echo \$5 | grep 'Nup=64'`" != '' ]; then
		nup="psnup -64"
	elif [ "`echo \$5 | grep 'Nup=32'`" != '' ]; then
		nup="psnup -32"
	elif [ "`echo \$5 | grep 'Nup=25'`" != '' ]; then
		nup="psnup -25"
	elif [ "`echo \$5 | grep 'Nup=16'`" != '' ]; then
		nup="psnup -16"
	elif [ "`echo \$5 | grep 'Nup=8'`" != '' ]; then
		nup="psnup -8"
	elif [ "`echo \$5 | grep 'Nup=6'`" != '' ]; then
		nup="psnup -6"
	elif [ "`echo \$5 | grep 'Nup=4'`" != '' ]; then
		nup="psnup -4"
	elif [ "`echo \$5 | grep 'Nup=2'`" != '' ]; then
		nup="psnup -2"
	elif [ "`echo \$5 | grep 'Nup=1'`" != '' ]; then
		nup="cat"
	fi
	echo   "NUP=\$nup"                                      >>\$LOGFILE
   if [ -e /usr/bin/psnup ]; then
       if [ \$# -ge 7 ]; then
	       cat \$6  | \$nup > \$INPUT_TEMP_PS
       else
	       cat       | \$nup > \$INPUT_TEMP_PS
       fi
   else
       if [ \$# -ge 7 ]; then
	       cp \$6  \$INPUT_TEMP_PS
       else
	       cat    > \$INPUT_TEMP_PS
       fi
   fi
else
   if [ \$# -ge 7 ]; then
      cp \$6  \$INPUT_TEMP_PS
   else
      cat    > \$INPUT_TEMP_PS
   fi
fi

if [ -e "/usr/local/Brother/Printer/${printer_model}/lpd/filter${printer_model}" ]; then
       :
else
    echo "ERROR: /usr/local/Brother/Printer/${printer_model}/lpd/filter${printer_model} does not exist"   >>\$LOGFILE
    errorcode=30
    exit
fi

CUPSOPTION=\`echo "\$5 Copies=1" | sed -e 's/multiple-document-handling/Collate/' -e 's/separate-documents-collated-copies/ON/' -e 's/separate-documents-uncollated-copies/OFF/'\`

if [ -e "/usr/local/Brother/Printer/${printer_model}/cupswrapper/brcupsconfcl1" ]; then
  if [ \$DEBUG = 0 ]; then
     /usr/local/Brother/Printer/${printer_model}/cupswrapper/brcupsconfcl1  ${printer_model}  \$PPD 0 "\$CUPSOPTION" >> /dev/null
  else
     /usr/local/Brother/Printer/${printer_model}/cupswrapper/brcupsconfcl1  ${printer_model}  \$PPD \$LOGCLEVEL "\$CUPSOPTION" >>\$LOGFILE
  fi
fi


if [ \$DEBUG -lt 10 ]; then
    cat    \$INPUT_TEMP_PS | /usr/local/Brother/Printer/${printer_model}/lpd/filter${printer_model}

    if [ \$LOGLEVEL -gt 2 ];  then
	   if [ \$LOGFILE != "/dev/null" ]; then
	     echo ""                                                >>\$LOGFILE
	     echo "    ------PostScript Data-------"                >>\$LOGFILE
	     cat    \$INPUT_TEMP_PS                                  >>\$LOGFILE
	   fi
    fi
fi
rm -f  \$INPUT_TEMP_PS

exit $errorcode


!ENDOFWFILTER!

chmod 755 $tmp_filter

#
#	check /usr/lib/cups/filter
#
if [ -d /usr/lib/cups/filter ]; then
	brotherlpdwrapper=/usr/lib/cups/filter/brlpdwrapper${printer_model}
	rm -f  $brotherlpdwrapper
	cp $tmp_filter	$brotherlpdwrapper
fi

#
#	check /usr/lib64/cups/filter
#
if [ -e /usr/lib64/cups/filter ]; then
	brotherlpdwrapper64=/usr/lib64/cups/filter/brlpdwrapper${printer_model}
	rm -f  $brotherlpdwrapper64
	cp $tmp_filter	$brotherlpdwrapper64
fi

#
#	remove temporary script file
#
rm -f  $tmp_filter

chmod a+w /usr/local/Brother/${device_model}/${printer_model}/inf/br${printer_model}rc
chmod a+w /usr/local/Brother/${device_model}/${printer_model}/inf
if [ -e /etc/init.d/lpd ]; then
   /etc/init.d/lpd stop
fi
if [  -e /etc/init.d/lprng ]; then
   /etc/init.d/lprng stop
fi


if [  -e /etc/init.d/cups ]; then
   /etc/init.d/cups restart
elif [  -e /etc/init.d/cupsys ]; then
   /etc/init.d/cupsys restart
fi

sleep 2s


port2=`lpinfo -v | grep -i 'usb://Brother/${device_name}' | head -1`
if [ "$port2" = '' ];then
    port2=`lpinfo -v | grep 'usb://Brother' | head -1`
fi

if [ "$port2" = '' ];then
    port2=`lpinfo -v | grep 'usb://' | head -1`
fi
port=`echo $port2| sed s/direct//g`

if [ "$port" = '' ];then
    port=usb:/dev/usb/lp0
fi
#lpadmin -p ${printer_name} -E -v $port -m br${printer_model}.ppd
lpadmin -p ${printer_name} -E -v $port -P $ppd_file_name

exit 0
