#! /bin/sh
#
# Brother Print filter
# Copyright 2003-2024 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=""dcpt735dw""
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/HL/HL-/' -e 's/FAX/FAX-/'`
pcfilename=`echo $printer_name | tr -d '[A-Z]'`
device_model="Printers"
ldpwrapper=/opt/brother/${device_model}/${printer_model}/cupswrapper/brother_lpdwrapper_${printer_model}

if [ -d "/usr/share/cups/model" ]; then
  mkdir -p /usr/share/cups/model/Brother
else
  mkdir -p /usr/share/ppd/Brother
fi

if [ -d "/usr/share/ppd" ]; then
  mkdir -p /usr/share/ppd/Brother       
else
  mkdir -p /usr/share/cups/model/Brother
fi



if [ "$1" = '-e' ]; then
  lpadmin -x ${printer_name}
  rm -f /usr/share/cups/model/Brother/brother_${printer_model}_printer_en.ppd
  rm -f /usr/share/ppd/Brother/brother_${printer_model}_printer_en.ppd
  rm -f /usr/lib/cups/filter/brother_lpdwrapper_${printer_model}
  rm -f /usr/lib64/cups/filter/brother_lpdwrapper_${printer_model}
#  rm -f /opt/brother/${device_model}/${printer_model}/cupswrapper/brother_lpdwrapper_${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 "/opt/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/Brother/brother_${printer_model}_printer_en.ppd
if [ -d "/usr/share/cups/model" ]; then
  ppd_file_name=/usr/share/cups/model/Brother/brother_${printer_model}_printer_en.ppd
else
  ppd_file_name=/usr/share/ppd/Brother/brother_${printer_model}_printer_en.ppd
fi

if [ -e "/opt/brother/${device_model}/${printer_model}/cupswrapper/brother_${printer_model}_printer_en.ppd" ];	then
cp "/opt/brother/${device_model}/${printer_model}/cupswrapper/brother_${printer_model}_printer_en.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/Brother/brother_${printer_model}_printer_en.ppd
	chmod 644 /usr/share/ppd/Brother/brother_${printer_model}_printer_en.ppd
fi
fi


#
#	check /usr/lib/cups/filter
#
if [ -d /usr/lib/cups/filter ]; then
	brotherlpdwrapper=/usr/lib/cups/filter/brother_lpdwrapper_${printer_model}
	rm -f  $brotherlpdwrapper
	ln -s $ldpwrapper $brotherlpdwrapper
fi

#
#	check /usr/lib64/cups/filter
#
if [ -e /usr/lib64/cups/filter ]; then
	brotherlpdwrapper64=/usr/lib64/cups/filter/brother_lpdwrapper_${printer_model}
	rm -f  $brotherlpdwrapper64
	ln -s $ldpwrapper $brotherlpdwrapper64
fi

#chmod a+w /opt/brother/${device_model}/${printer_model}/inf/br${printer_model}rc
#chmod a+w /opt/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

uris=$(lpinfo -v)

for uri in $uris
do
	URI=$(echo $uri | grep ${device_name} | grep usb)
	if [ "$URI" != '' ];then
		break;
	fi
done

if [ "$URI" = '' ];then
	for uri in $uris
	do
		URI=$(echo $uri | grep ${device_name} )
		if [ "$URI" != '' ];then
			break;
		fi
	done
fi

if [ "$URI" = '' ];then
	for uri in $uris
	do
		URI=$(echo $uri | grep -i Brother | grep usb )
		if [ "$URI" != '' ];then
			break;
		fi
	done
fi	

if [ "$URI" = '' ];then
	for uri in $uris
	do
		URI=$(echo $uri | grep usb )
		if [ "$URI" != '' ];then
			break;
		fi
	done
fi


if [ "$URI" = '' ];then
	URI="usb://dev/usb/lp0"
fi

echo lpadmin -p ${printer_name} -E -v $URI -P $ppd_file_name
lpadmin -p ${printer_name} -E -v $URI -P $ppd_file_name
								
exit 0

